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

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. 在本文中,使用 C# 創建群集和資料庫。In this article, you create a cluster and a database by using C#.

PrerequisitesPrerequisites

安裝 C# NuGetInstall C# NuGet

驗證Authentication

為了運行本文中的示例,我們需要一個可以訪問資源的 Azure AD 應用程式和服務主體。For running the examples in this article, we need an Azure AD Application and service principal that can access resources. 檢查創建 Azure AD 應用程式以創建免費的 Azure AD 應用程式,並在訂閱範圍內添加角色指派。Check create an Azure AD application to create a free Azure AD Application and add role assignment at the subscription scope. 它還演示如何獲取Directory (tenant) IDApplication IDClient SecretIt also shows how to get the Directory (tenant) ID, Application ID, and Client Secret.

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

  1. 使用下列程式碼建立您的叢集:Create your cluster by using the following code:

    var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";//Directory (tenant) ID
    var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";//Application ID
    var clientSecret = "xxxxxxxxxxxxxx";//Client Secret
    var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
    var authenticationContext = new AuthenticationContext($"https://login.windows.net/{tenantId}");
    var credential = new ClientCredential(clientId, clientSecret);
    var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.core.windows.net/", clientCredential: credential);
    
    var credentials = new TokenCredentials(result.AccessToken, result.AccessTokenType);
    
    var kustoManagementClient = new KustoManagementClient(credentials)
    {
        SubscriptionId = subscriptionId
    };
    
    var resourceGroupName = "testrg";
    var clusterName = "mykustocluster";
    var location = "Central US";
    var skuName = "Standard_D13_v2";
    var tier = "Standard";
    var capacity = 5;
    var sku = new AzureSku(skuName, tier, capacity);
    var cluster = new Cluster(location, sku);
    await kustoManagementClient.Clusters.CreateOrUpdateAsync(resourceGroupName, clusterName, cluster);
    
    設定Setting 建議的值Suggested value 欄位描述Field description
    clusterNameclusterName mykustoclustermykustocluster 所需的叢集名稱。The desired name of your cluster.
    skuNameskuName Standard_D13_v2Standard_D13_v2 將用於叢集的 SKU。The SKU that will be used for your cluster.
    tiertier 標準Standard SKU 層。The SKU tier.
    處理能力capacity 數量number 群集的實例數。The number of instances of the cluster.
    resourceGroupNameresourceGroupName testrgtestrg 將在其中建立叢集的資源群組名稱。The resource group name where the cluster will be created.

    注意

    創建群集是一項長時間運行的操作,因此強烈建議使用 CreateOrUpdateAsync,而不是 CreateOrUpdate。Create a cluster is a long running operation, so it's highly recommended to use CreateOrUpdateAsync, instead of CreateOrUpdate.

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

    kustoManagementClient.Clusters.Get(resourceGroupName, clusterName);
    

如果結果中包含有 Succeeded 值的 ProvisioningState,表示已成功建立叢集。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 code:

    var hotCachePeriod = new TimeSpan(3650, 0, 0, 0);
    var softDeletePeriod = new TimeSpan(3650, 0, 0, 0);
    var databaseName = "mykustodatabase";
    var database = new ReadWriteDatabase(location: location, softDeletePeriod: softDeletePeriod, hotCachePeriod: hotCachePeriod);
    
    await kustoManagementClient.Databases.CreateOrUpdateAsync(resourceGroupName, clusterName, databaseName, database);
    
     [!NOTE]
     If you are using C# version 2.0.0 or below, use Database instead of ReadWriteDatabase.
    
    設定Setting 建議的值Suggested value 欄位描述Field description
    clusterNameclusterName mykustoclustermykustocluster 將在其中建立資料庫的叢集名稱。The name of your cluster where the database will be created.
    databaseNamedatabaseName 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:

    kustoManagementClient.Databases.Get(resourceGroupName, clusterName, databaseName) as ReadWriteDatabase;
    

您此時有一個叢集和一個資料庫。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:

    kustoManagementClient.Clusters.Delete(resourceGroupName, clusterName);
    

後續步驟Next steps