使用 Python 创建 Azure 数据资源管理器群集和数据库Create an Azure Data Explorer cluster and database by using Python

本文将使用 Python 创建 Azure 数据资源管理器群集和数据库。In this article, you create an Azure Data Explorer cluster and database by using Python. 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, first create a cluster, and create one or more databases in that cluster. 然后将数据引入或加载到数据库,以便针对其运行查询。Then ingest, or load, data into a database so that you can run queries against it.

必备条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.

安装 Python 包Install Python package

要为 Azure 数据资源管理器 (Kusto) 安装 Python 包,请打开其路径中包含 Python 的命令提示符。To install the Python package for Azure Data Explorer (Kusto), open a command prompt that has Python in its path. 运行以下命令:Run this command:

pip install azure-common
pip install azure-mgmt-kusto

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 command:

    from azure.mgmt.kusto import KustoManagementClient
    from azure.mgmt.kusto.models import Cluster, AzureSku
    from azure.common.credentials import ServicePrincipalCredentials
    
    #Directory (tenant) ID
    tenant_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
    #Application ID
    client_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
    #Client Secret
    client_secret = "xxxxxxxxxxxxxx"
    subscription_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
    credentials = ServicePrincipalCredentials(
        client_id=client_id,
        secret=client_secret,
        tenant=tenant_id
    )
    
    location = 'China East'
    sku_name = 'Standard_D13_v2'
    capacity = 5
    tier = "Standard"
    resource_group_name = 'testrg'
    cluster_name = 'mykustocluster'
    cluster = Cluster(location=location, sku=AzureSku(name=sku_name, capacity=capacity, tier=tier))
    
    kustoManagementClient = KustoManagementClient(credentials, subscription_id)
    
    cluster_operations = kustoManagementClient.clusters
    
    poller = cluster_operations.create_or_update(resource_group_name, cluster_name, cluster)
    
    设置Setting 建议的值Suggested value 字段说明Field description
    cluster_namecluster_name mykustocluster mykustocluster 所需的群集名称。The desired name of your cluster.
    sku_namesku_name 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.
    resource_group_nameresource_group_name testrgtestrg 将在其中创建群集的资源组名称。The resource group name where the cluster will be created.

    备注

    创建群集是一个长时间运行的操作。Create a cluster is a long running operation. create_or_update 方法返回 LROPoller 的实例,请参阅 LROPoller 类获取详细信息。Method create_or_update returns an instance of LROPoller, see LROPoller class to get more information.

  2. 运行以下命令,检查群集是否已成功创建:Run the following command to check whether your cluster was successfully created:

    cluster_operations.get(resource_group_name = resource_group_name, cluster_name= clusterName, custom_headers=None, raw=False)
    

如果结果包含带 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 command:

    from azure.mgmt.kusto.models import Database
    from datetime import timedelta
    
    softDeletePeriod = timedelta(days=3650)
    hotCachePeriod = timedelta(days=3650)
    databaseName="mykustodatabase"
    
    database_operations = kusto_management_client.databases 
    _database = ReadWriteDatabase(location=location,
                        soft_delete_period=softDeletePeriod,
                        hot_cache_period=hotCachePeriod)
    
    #Returns an instance of LROPoller, see https://docs.microsoft.com/python/api/msrest/msrest.polling.lropoller?view=azure-python
    poller =database_operations.create_or_update(resource_group_name = resource_group_name, cluster_name = clusterName, database_name = databaseName, parameters = _database)
    
     [!NOTE]
     If you are using Python version 0.4.0 or below, use Database instead of ReadWriteDatabase.
    
    设置Setting 建议的值Suggested value 字段说明Field description
    cluster_namecluster_name mykustocluster mykustocluster 将在其中创建数据库的群集的名称。The name of your cluster where the database will be created.
    database_namedatabase_name mykustodatabase mykustodatabase 数据库名称。The name of your database.
    resource_group_nameresource_group_name testrgtestrg 将在其中创建群集的资源组名称。The resource group name where the cluster will be created.
    soft_delete_periodsoft_delete_period 3650 天,0:00:00 3650 days, 0:00:00 供查询使用的数据的保留时间。The amount of time that data will be kept available to query.
    hot_cache_periodhot_cache_period 3650 天,0:00:00 3650 days, 0: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:

    database_operations.get(resource_group_name = resource_group_name, cluster_name = clusterName, database_name = databaseName)
    

现在,你有了一个群集和一个数据库。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:

    cluster_operations.delete(resource_group_name = resource_group_name, cluster_name = clusterName)
    

后续步骤Next steps