Create an Event Hubs data connection for Azure Data Explorer by using C#
Azure Data Explorer is a fast and highly scalable data exploration service for log and telemetry data. Azure Data Explorer offers ingestion (data loading) from event hubs, IoT hubs, and blobs written to blob containers.
In this article, you create an Event Hubs data connection for Azure Data Explorer by using C#.
Prerequisites
- Visual Studio 2019, download and use the free Visual Studio 2019 Community Edition. Enable Azure development during the Visual Studio setup.
- An Azure subscription. Create a free Azure account.
- Create a cluster and database.
- Create table and column mapping.
- Set database and table policies (optional).
- Create an event hub with data for ingestion.
Install C# NuGet
- Install the Microsoft.Azure.Management.Kusto NuGet package.
Authentication
To run the following example, you need an Azure Active Directory (Azure AD) application and service principal that can access resources. To create a free Azure AD application and add role assignment at the subscription level, see Create an Azure AD application. You also need the directory (tenant) ID, application ID, and client secret.
Add an Event Hubs data connection
The following example shows you how to add an Event Hubs data connection programmatically. See connect to the event hub for adding an Event Hubs 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 = "PlaceholderClientSecret";//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";
//The cluster and database that are created as part of the Prerequisites
var clusterName = "mycluster";
var databaseName = "mydatabase";
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 = "Central US";
//The table and column mapping are created as part of the Prerequisites
var tableName = "StormEvents";
var mappingRuleName = "StormEvents_CSV_Mapping";
var dataFormat = EventHubDataFormat.CSV;
var compression = "None";
var databaseRouting = "Multi";
await kustoManagementClient.DataConnections.CreateOrUpdateAsync(resourceGroupName, clusterName, databaseName, dataConnectionName,
new EventHubDataConnection(eventHubResourceId, consumerGroup, location: location, tableName: tableName, mappingRuleName: mappingRuleName, dataFormat: dataFormat, compression: compression, databaseRouting: databaseRouting));
| Setting | Suggested value | Field description |
|---|---|---|
| tenantId | xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx | Your tenant ID. Also known as directory ID. |
| subscriptionId | xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx | The subscription ID that you use for resource creation. |
| clientId | xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx | The client ID of the application that can access resources in your tenant. |
| clientSecret | PlaceholderClientSecret | The client secret of the application that can access resources in your tenant. |
| resourceGroupName | testrg | The name of the resource group containing your cluster. |
| clusterName | mykustocluster | The name of your cluster. |
| databaseName | mykustodatabase | The name of the target database in your cluster. |
| dataConnectionName | myeventhubconnect | The desired name of your data connection. |
| tableName | StormEvents | The name of the target table in the target database. |
| mappingRuleName | StormEvents_CSV_Mapping | The name of your column mapping related to the target table. |
| dataFormat | csv | The data format of the message. |
| eventHubResourceId | Resource ID | The resource ID of your event hub that holds the data for ingestion. |
| consumerGroup | $Default | The consumer group of your event hub. |
| location | Central US | The location of the data connection resource. |
| compression | Gzip or None | The type of data compression. |
| databaseRouting | Multi or Single | The database routing for the connection. If you set the value to Single, the data connection will be routed to a single database in the cluster as specified in the databaseName setting. If you set the value to Multi, you can override the default target database using the Database ingestion property. For more information, see Events routing. |
Generate data
See the sample app that generates data and sends it to an event hub.
An event can contain one or more records, up to its size limit. In the following sample we send two events, each has five records appended:
var events = new List<EventData>();
var data = string.Empty;
var recordsPerEvent = 5;
var rand = new Random();
var counter = 0;
for (var i = 0; i < 10; i++)
{
// Create the data
var metric = new Metric { Timestamp = DateTime.UtcNow, MetricName = "Temperature", Value = rand.Next(-30, 50) };
var data += JsonConvert.SerializeObject(metric) + Environment.NewLine;
counter++;
// Create the event
if (counter == recordsPerEvent)
{
var eventData = new EventData(Encoding.UTF8.GetBytes(data));
events.Add(eventData);
counter = 0;
data = string.Empty;
}
}
// Send events
eventHubClient.SendAsync(events).Wait();
Clean up resources
To delete the data connection, use the following command:
kustoManagementClient.DataConnections.Delete(resourceGroupName, clusterName, databaseName, dataConnectionName);
الملاحظات
إرسال الملاحظات وعرضها المتعلقة بـ