Create an Event Hubs data connection for Azure Data Explorer with SDKs

Azure Data Explorer offers ingestion from Event Hubs, a big data streaming platform and event ingestion service. Event Hubs can process millions of events per second in near real time.

In this article, you connect to an event hub and ingest data into Azure Data Explorer. For an overview on ingesting from Event Hubs, see Azure Event Hubs data connection.

To learn how to create the connection in the Azure Data Explorer web UI, Azure portal, or with an ARM template, see Create an Event Hubs data connection.

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

Prerequisites

Create an event hub data connection

In this section, you establish a connection between the event hub and your Azure Data Explorer table. As long as this connection is in place, data is transmitted from the event hub into your target table. If the event hub is moved to a different resource or subscription, you need to update or recreate the connection.

  1. Install the required libraries.

    pip install azure-common
    pip install azure-mgmt-kusto
    
  2. Create a Microsoft Entra application principal to use for authentication. You need the directory (tenant) ID, application ID, and client secret.

  3. Run the following code.

    from azure.mgmt.kusto import KustoManagementClient
    from azure.mgmt.kusto.models import EventHubDataConnection
    from azure.identity import ClientSecretCredential
    
    #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 = "myresourcegroup"
    #The cluster and database that are created as part of the Prerequisites
    cluster_name = "mycluster"
    database_name = "mydatabase"
    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/myresourcegroup/providers/Microsoft.EventHub/namespaces/myeventhubnamespace/eventhubs/myeventhub"";
    consumer_group = "$Default"
    location = "China East 2"
    #The table and column mapping that are created as part of the Prerequisites
    table_name = "mytable"
    mapping_rule_name = "mytablemappingrule"
    data_format = "csv"
    database_routing = "Multi"
    #Returns an instance of LROPoller, check https://learn.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,
                    database_routing=database_routing
                )
            )
    poller.wait()
    print(poller.result())
    
    Setting Suggested value Field description
    tenant_id 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.
    client_id xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx The client ID of the application that can access resources in your tenant.
    client_secret xxxxxxxxxxxxxx The client secret of the application that can access resources in your tenant.
    resource_group_name myresourcegroup The name of the resource group containing your cluster.
    cluster_name mycluster The name of your cluster.
    database_name mydatabase The name of the target database in your cluster.
    data_connection_name myeventhubconnect The desired name of your data connection.
    table_name mytable The name of the target table in the target database.
    mapping_rule_name mytablemappingrule The name of your column mapping related to the target table.
    data_format csv The data format of the message.
    event_hub_resource_id Resource ID The resource ID of your event hub that holds the data for ingestion.
    consumer_group $Default The consumer group of your event hub.
    location China East 2 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 event hub data connection

To remove the event hub connection, run the following command:

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