使用 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#.

必备条件Prerequisites

  • 如果尚未安装 Visual Studio 2019,可以下载并使用免费的 Visual Studio 2019 Community EditionIf you don't have Visual Studio 2019 installed, you can download and use the free Visual Studio 2019 Community Edition. 在安装 Visual Studio 的过程中,请确保启用“Azure 开发”。 Make sure that you enable Azure development during the Visual Studio setup.
  • 如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.

安装 C# NuGetInstall C# NuGet

AuthenticationAuthentication

为了运行本文中的示例,我们需要可以访问资源的 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.chinacloudapi.cn/{tenantId}");
    var credential = new ClientCredential(clientId, clientSecret);
    var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.core.chinacloudapi.cn/", 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 = "China East 2";
    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 mykustocluster mykustocluster 所需的群集名称。The desired name of your cluster.
    skuNameskuName Standard_D13_v2Standard_D13_v2 将用于群集的 SKU。The SKU that will be used for your cluster.
    tier StandardStandard 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);
    

    备注

    如果使用的是 C# 版本 2.0.0 或更低版本,请使用 Database 而不是 ReadWriteDatabase。If you are using C# version 2.0.0 or below, use Database instead of ReadWriteDatabase.

    设置Setting 建议的值Suggested value 字段说明Field description
    clusterNameclusterName mykustocluster**mykustocluster 将在其中创建数据库的群集的名称。The name of your cluster where the database will be created.
    databaseNamedatabaseName mykustodatabase**mykustodatabase 数据库名称。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