使用 PowerShell 建立 Azure 資料總管叢集與資料庫Create an Azure Data Explorer cluster and database by using PowerShell

Azure 資料總管是快速、完全受控的資料分析服務,可即時分析來自應用程式、網站、IoT 裝置等的大量資料流。Azure Data Explorer is a fast, fully managed data analytics service for real-time analysis on large volumes of data streaming from applications, websites, IoT devices, and more. 若要使用 Azure 資料總管,請先建立叢集,然後在該叢集中建立一或多個資料庫。To use Azure Data Explorer, you first create a cluster, and create one or more databases in that cluster. 然後將資料內嵌 (載入) 至資料庫,讓您可以對資料執行查詢。Then you ingest (load) data into a database so that you can run queries against it. 在本文中,您會使用 Powershell 來建立叢集和資料庫。In this article, you create a cluster and a database by using Powershell. 您可以使用 Az.Kusto 在 Windows 和 Linux 上或在 Azure Cloud Shell 中執行 PowerShell Cmdlet 和指令碼,以建立及設定 Azure 資料總管叢集與資料庫。You can run PowerShell cmdlets and scripts on Windows, Linux, or in Azure Cloud Shell with Az.Kusto to create and configure Azure Data Explorer clusters and databases.

PrerequisitesPrerequisites

注意

本文已更新為使用新的 Azure PowerShell Az 模組。This article has been updated to use the new Azure PowerShell Az module. AzureRM 模組在至少 2020 年 12 月之前都還會持續收到錯誤 (Bug) 修正,因此您仍然可以持續使用。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要深入了解新的 Az 模組和 AzureRM 的相容性,請參閱新的 Azure PowerShell Az 模組簡介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 如需 Az 模組安裝指示,請參閱安裝 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

如果您沒有 Azure 訂用帳戶,請在開始之前先建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

要啟動 Azure Cloud Shell:To start Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Start Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼貼到 Cloud Shell 工作階段中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 選取 Enter 鍵執行程式碼。Select Enter to run the code.

如果您選擇在本機安裝和使用 Azure CLI,本文會要求 Azure CLI 版本2.0.4 版或更新版本。If you choose to install and use the Azure CLI locally, this article requires the Azure CLI version 2.0.4 or later. 執行 az --version 來檢查您的版本。Run az --version to check your version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

設定參數Configure parameters

如果您在 Azure Cloud Shell 中執行命令,則不需要執行下列步驟。The following steps are not required if you're running commands in Azure Cloud Shell. 如果您在本機執行 CLI,請依照步驟 1 和 2 登入 Azure 並設定您目前的訂用帳戶:If you're running the CLI locally, follow steps 1 & 2 to sign in to Azure and to set your current subscription:

  1. 執行下列命令以登入 Azure:Run the following command to sign in to Azure:

    Connect-AzAccount
    
  2. 設定要建立叢集的訂用帳戶:Set the subscription where you want your cluster to be created:

     Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    
  3. 在本機或 Azure Cloud Shell 中執行 Azure CLI 時,您需要在裝置上安裝 Az.Kusto 模組:When running Azure CLI locally or in the Azure Cloud Shell, you need to install the Az.Kusto module on your device:

     Install-Module -Name Az.Kusto
    

建立 Azure 資料總管叢集Create the Azure Data Explorer cluster

  1. 使用下列命令建立您的叢集:Create your cluster by using the following command:

     New-AzKustoCluster -ResourceGroupName testrg -Name mykustocluster -Location 'Central US' -Sku D13_v2 -Capacity 10
    
    設定Setting 建議的值Suggested value 欄位描述Field description
    名稱Name mykustoclustermykustocluster 所需的叢集名稱。The desired name of your cluster.
    SKUSku D13_v2D13_v2 將用於叢集的 SKU。The SKU that will be used for your cluster.
    resourceGroupNameResourceGroupName testrgtestrg 將在其中建立叢集的資源群組名稱。The resource group name where the cluster will be created.

    有其他選擇性參數可供您使用,例如叢集的容量。There are additional optional parameters that you can use, such as the capacity of the cluster.

  2. 執行下列命令來檢查是否已成功建立叢集:Run the following command to check whether your cluster was successfully created:

    Get-AzKustoCluster -Name mykustocluster -ResourceGroupName testrg
    

如果結果中包含有 provisioningState 值的 Succeeded,表示已成功建立叢集。If the result contains provisioningState with the Succeeded value, then the cluster was successfully created.

在 Azure 資料總管叢集中建立資料庫Create the database in the Azure Data Explorer cluster

  1. 使用下列命令建立您的資料庫:Create your database by using the following command:

    New-AzKustoDatabase -ResourceGroupName testrg -ClusterName mykustocluster -Name mykustodatabase -SoftDeletePeriod 3650:00:00:00 -HotCachePeriod 3650:00:00:00
    
    設定Setting 建議的值Suggested value 欄位描述Field description
    ClusterNameClusterName mykustoclustermykustocluster 將在其中建立資料庫的叢集名稱。The name of your cluster where the database will be created.
    名稱Name mykustodatabasemykustodatabase 您的資料庫名稱。The name of your database.
    resourceGroupNameResourceGroupName testrgtestrg 將在其中建立叢集的資源群組名稱。The resource group name where the cluster will be created.
    SoftDeletePeriodSoftDeletePeriod 3650:00:00:003650:00:00:00 將保留資料以供查詢的時間長度。The amount of time that data will be kept available to query.
    HotCachePeriodHotCachePeriod 3650:00:00:003650:00:00:00 資料將保留在快取中的時間長度。The amount of time that data will be kept in cache.
  2. 執行下列命令以查看您所建立的資料庫:Run the following command to see the database that you created:

    Get-AzKustoDatabase -ClusterName mykustocluster -ResourceGroupName testrg -Name mykustodatabase
    

您此時有一個叢集和一個資料庫。You now have a cluster and a database.

清除資源Clean up resources

  • 如果您打算遵循其他文章,請保留您建立的資源。If you plan to follow our other articles, keep the resources you created.

  • 若要清除資源,請刪除叢集。To clean up resources, delete the cluster. 您刪除叢集時,也會刪除其中的所有資料庫。When you delete a cluster, it also deletes all the databases in it. 使用下列命令刪除您的叢集:Use the following command to delete your cluster:

    Remove-AzKustoCluster -ResourceGroupName testrg -Name mykustocluster
    

後續步驟Next steps