使用 Python 为 Azure 数据资源管理器创建事件中心数据连接Create an Event Hub data connection for Azure Data Explorer by using Python

Azure 数据资源管理器是一项快速且高度可缩放的数据探索服务,适用于日志和遥测数据。Azure Data Explorer is a fast and highly scalable data exploration service for log and telemetry data. Azure 数据资源管理器提供了从事件中心、IoT 中心和写入 blob 容器的 blob 引入数据(数据加载)的功能。Azure Data Explorer offers ingestion (data loading) from Event Hubs, IoT Hubs, and blobs written to blob containers. 在本文中,你将使用 Python 为 Azure 数据资源管理器创建事件中心数据连接。In this article, you create an Event Hub data connection for Azure Data Explorer by using Python.

先决条件Prerequisites

安装 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 the following command:

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

身份验证Authentication

若要运行以下示例,需要可以访问资源的 Azure Active Directory (Azure AD) 应用程序和服务主体。To run the following example, you need an Azure Active Directory (Azure AD) application and service principal that can access resources. 若要创建免费的 Azure AD 应用程序并在订阅级别添加角色分配,请参阅创建 Azure AD 应用程序To create a free Azure AD application and add role assignment at the subscription level, see Create an Azure AD application. 还需要目录(租户)ID、应用程序 ID 和客户端密码。You also need the directory (tenant) ID, application ID, and client secret.

添加事件中心数据连接Add an Event Hub data connection

以下示例演示如何以编程方式添加事件中心数据连接。The following example shows you how to add an Event Hub data connection programmatically. 请参阅连接到事件中心,了解如何使用 Azure 门户添加事件中心数据连接。See connect to the event hub for adding an Event Hub data connection using the Azure portal.

from azure.mgmt.kusto import KustoManagementClient
from azure.mgmt.kusto.models import EventHubDataConnection
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
    )
kusto_management_client = KustoManagementClient(credentials, subscription_id)

resource_group_name = "testrg";
#The cluster and database that are created as part of the Prerequisites
cluster_name = "mykustocluster";
database_name = "mykustodatabase";
data_connection_name = "myeventhubconnect";
#The event hub that is created as part of the Prerequisites
event_hub_resource_id = "/subscriptions/xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.EventHub/namespaces/xxxxxx/eventhubs/xxxxxx";
consumer_group = "$Default";
location = "China East 2";
#The table and column mapping that are created as part of the Prerequisites
table_name = "StormEvents";
mapping_rule_name = "StormEvents_CSV_Mapping";
data_format = "csv";
#Returns an instance of LROPoller, check https://docs.microsoft.com/python/api/msrest/msrest.polling.lropoller?view=azure-python
poller = kusto_management_client.data_connections.create_or_update(resource_group_name=resource_group_name, cluster_name=cluster_name, database_name=database_name, data_connection_name=data_connection_name,
                                        parameters=EventHubDataConnection(event_hub_resource_id=event_hub_resource_id, consumer_group=consumer_group, location=location,
                                                                            table_name=table_name, mapping_rule_name=mapping_rule_name, data_format=data_format))
设置Setting 建议的值Suggested value 字段说明Field description
tenant_idtenant_id xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 租户 ID。Your tenant ID. 也称为目录 ID。Also known as directory ID.
subscriptionIdsubscriptionId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 用于创建资源的订阅 ID。The subscription ID that you use for resource creation.
client_idclient_id xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 可以访问租户中资源的应用程序的客户端 ID。The client ID of the application that can access resources in your tenant.
client_secretclient_secret xxxxxxxxxxxxxxxxxxxxxxxxxxxx 可以访问租户中资源的应用程序的客户端密码。The client secret of the application that can access resources in your tenant.
resource_group_nameresource_group_name testrgtestrg 包含群集的资源组的名称。The name of the resource group containing your cluster.
cluster_namecluster_name mykustocluster mykustocluster 群集的名称。The name of your cluster.
database_namedatabase_name mykustodatabase mykustodatabase 群集中目标数据库的名称。The name of the target database in your cluster.
data_connection_namedata_connection_name myeventhubconnectmyeventhubconnect 所需的数据连接名称。The desired name of your data connection.
table_nametable_name StormEventsStormEvents 目标数据库中目标表的名称。The name of the target table in the target database.
mapping_rule_namemapping_rule_name StormEvents_CSV_MappingStormEvents_CSV_Mapping 与目标表相关的列映射的名称。The name of your column mapping related to the target table.
data_formatdata_format csvcsv 消息的数据格式。The data format of the message.
event_hub_resource_idevent_hub_resource_id 资源 IDResource ID 包含要引入的数据的事件中心的资源 ID。The resource ID of your Event Hub that holds the data for ingestion.
consumer_groupconsumer_group $Default$Default 事件中心的使用者组。The consumer group of your Event Hub.
locationlocation 中国东部 2China East 2 数据连接资源的位置。The location of the data connection resource.

清理资源Clean up resources

若要删除数据连接,请使用以下命令:To delete the data connection, use the following command:

kusto_management_client.data_connections.delete(resource_group_name=resource_group_name, cluster_name=kusto_cluster_name, database_name=kusto_database_name, data_connection_name=kusto_data_connection_name)