Azure Event Hub Management Client

11.2.0 · active · verified Thu Apr 09

The `azure-mgmt-eventhub` library provides a client for managing Azure Event Hub resources, including Event Hub namespaces, Event Hubs, Consumer Groups, and Disaster Recovery configurations. It is part of the Azure SDK for Python, currently at version 11.2.0, and receives regular updates for bug fixes, new features, and API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with `DefaultAzureCredential` and use the `EventHubManagementClient` to list existing Event Hub namespaces and Event Hubs within a specified resource group. Ensure you have an existing Azure resource group and Event Hub namespace, and appropriate environment variables set for authentication or log in via Azure CLI (`az login`).

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.eventhub import EventHubManagementClient

# Set these environment variables or replace with actual values
# AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID
# Or log in via Azure CLI: `az login`
SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "<your-subscription-id>")
RESOURCE_GROUP_NAME = os.environ.get("AZURE_RESOURCE_GROUP_NAME", "<existing-resource-group>")
EVENTHUB_NAMESPACE_NAME = os.environ.get("AZURE_EVENTHUB_NAMESPACE_NAME", "<existing-eventhub-namespace>")

def main():
    if SUBSCRIPTION_ID == "<your-subscription-id>" or \
       RESOURCE_GROUP_NAME == "<existing-resource-group>" or \
       EVENTHUB_NAMESPACE_NAME == "<existing-eventhub-namespace>":
        print("Please set AZURE_SUBSCRIPTION_ID, AZURE_RESOURCE_GROUP_NAME, and AZURE_EVENTHUB_NAMESPACE_NAME environment variables or replace placeholders.")
        return

    credential = DefaultAzureCredential()
    client = EventHubManagementClient(credential, SUBSCRIPTION_ID)

    print(f"Listing Event Hub Namespaces in resource group '{RESOURCE_GROUP_NAME}':")
    try:
        namespaces = client.namespaces.list_by_resource_group(RESOURCE_GROUP_NAME)
        for namespace in namespaces:
            print(f"- {namespace.name} (Location: {namespace.location})")
            
            # List Event Hubs within this namespace
            print(f"  Listing Event Hubs in namespace '{namespace.name}':")
            event_hubs = client.event_hubs.list_by_namespace(RESOURCE_GROUP_NAME, namespace.name)
            for event_hub in event_hubs:
                print(f"  - {event_hub.name} (Partitions: {event_hub.partition_count})")
    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure the resource group and namespace exist and your credentials have access.")

if __name__ == "__main__":
    main()

view raw JSON →