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

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. 在本文中,你将使用 C# 为 Azure 数据资源管理器创建事件中心数据连接。In this article, you create an Event Hub data connection for Azure Data Explorer by using C#.

先决条件Prerequisites

安装 C# nugetInstall C# nuget

身份验证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.

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";
//The cluster and database that are created as part of the Prerequisites
var clusterName = "mykustocluster";
var databaseName = "mykustodatabase";
var dataConnectionName = "myeventhubconnect";
//The event hub that is created as part of the Prerequisites
var eventHubResourceId = "/subscriptions/xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.EventHub/namespaces/xxxxxx/eventhubs/xxxxxx";
var consumerGroup = "$Default";
var location = "China East 2";
//The table and column mapping are created as part of the Prerequisites
var tableName = "StormEvents";
var mappingRuleName = "StormEvents_CSV_Mapping";
var dataFormat = DataFormat.CSV;
await kustoManagementClient.DataConnections.CreateOrUpdateAsync(resourceGroupName, clusterName, databaseName, dataConnectionName, 
    new EventHubDataConnection(eventHubResourceId, consumerGroup, location: location, tableName: tableName, mappingRuleName: mappingRuleName, dataFormat: dataFormat));
设置Setting 建议的值Suggested value 字段说明Field description
tenantIdtenantId 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.
clientIdclientId xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx 可以访问租户中资源的应用程序的客户端 ID。The client ID of the application that can access resources in your tenant.
clientSecretclientSecret xxxxxxxxxxxxxxxxxxxxxxxxxxxx 可以访问租户中资源的应用程序的客户端密码。The client secret of the application that can access resources in your tenant.
resourceGroupNameresourceGroupName testrgtestrg 包含群集的资源组的名称。The name of the resource group containing your cluster.
clusterNameclusterName mykustocluster mykustocluster 群集的名称。The name of your cluster.
databaseNamedatabaseName mykustodatabase mykustodatabase 群集中目标数据库的名称。The name of the target database in your cluster.
dataConnectionNamedataConnectionName myeventhubconnectmyeventhubconnect 所需的数据连接名称。The desired name of your data connection.
tableNametableName StormEventsStormEvents 目标数据库中目标表的名称。The name of the target table in the target database.
mappingRuleNamemappingRuleName StormEvents_CSV_MappingStormEvents_CSV_Mapping 与目标表相关的列映射的名称。The name of your column mapping related to the target table.
dataFormatdataFormat csvcsv 消息的数据格式。The data format of the message.
eventHubResourceIdeventHubResourceId 资源 IDResource ID 包含要引入的数据的事件中心的资源 ID。The resource ID of your Event Hub that holds the data for ingestion.
consumerGroupconsumerGroup $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:

kustoManagementClient.DataConnections.Delete(resourceGroupName, clusterName, databaseName, dataConnectionName);