Create an IoT Hub data connection for Azure Data Explorer with SDKs

This article shows you how to ingest data into Azure Data Explorer from IoT Hub, a big data streaming platform and IoT ingestion service.

To learn how to create the connection using the Kusto SDKs, see Create an IoT Hub data connection with SDKs.

For general information about ingesting into Azure Data Explorer from IoT Hub, see Connect to IoT Hub.

Note

Only events enqueued after you create the data connection are ingested.

For code samples based on previous SDK versions, see the archived article.

Prerequisites

Create an IoT Hub data connection

In this section, you'll establish a connection between the IoT Hub and your Azure Data Explorer table. As long as this connection is in place, data is transmitted from the IoT Hub into your target table.

  1. Install the Microsoft.Azure.Management.Kusto NuGet package.

  2. Create a Microsoft Entra application principal to use for authentication. You'll need the directory (tenant) ID, application ID, and client secret.

  3. Run the following code.

    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 credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
    var resourceManagementClient = new ArmClient(credentials, subscriptionId);
    var resourceGroupName = "testrg";
    //The cluster and database that are created as part of the Prerequisites
    var clusterName = "mykustocluster";
    var databaseName = "mykustodatabase";
    var subscription = await resourceManagementClient.GetDefaultSubscriptionAsync();
    var resourceGroup = (await subscription.GetResourceGroupAsync(resourceGroupName)).Value;
    var cluster = (await resourceGroup.GetKustoClusterAsync(clusterName)).Value;
    var database = (await cluster.GetKustoDatabaseAsync(databaseName)).Value;
    var dataConnections = database.GetKustoDataConnections();
    var iotHubConnectionName = "myiothubconnect";
    //The IoT hub that is created as part of the Prerequisites
    var iotHubResourceId = new ResourceIdentifier("/subscriptions/<iotHubSubscriptionId>/resourceGroups/<iotHubResourceGroupName>/providers/Microsoft.Devices/IotHubs/<iotHubName>");
    var sharedAccessPolicyName = "iothubforread";
    var consumerGroup = "$Default";
    var location = AzureLocation.CentralUS;
    //The table and column mapping are created as part of the Prerequisites
    var tableName = "StormEvents";
    var mappingRuleName = "StormEvents_CSV_Mapping";
    var dataFormat = KustoIotHubDataFormat.Csv;
    var databaseRouting = KustoDatabaseRouting.Multi;
    var iotHubConnectionData = new KustoIotHubDataConnection {
        IotHubResourceId = iotHubResourceId, ConsumerGroup = consumerGroup, SharedAccessPolicyName = sharedAccessPolicyName,
        Location = location, TableName = tableName, MappingRuleName = mappingRuleName,
        DataFormat = dataFormat, DatabaseRouting = databaseRouting
    };
    await dataConnections.CreateOrUpdateAsync(WaitUntil.Completed, iotHubConnectionName, iotHubConnectionData);
    
    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.
    iotHubConnectionName myiothubconnect 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.
    iotHubResourceId Resource ID The resource ID of your IoT hub that holds the data for ingestion.
    sharedAccessPolicyName iothubforread The name of the shared access policy that defines the permissions for devices and services to connect to IoT Hub.
    consumerGroup $Default The consumer group of your event hub.
    location Central US The location of the data connection resource.
    databaseRouting Multi or Single The database routing for the connection. If you set the value to Single, the data connection is 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.

Remove an IoT Hub data connection

To remove the IoT Hub connection, run the following command:

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