Azure Event Hubs Client Library
The Microsoft Azure Event Hubs Client Library for Python facilitates sending and receiving events to and from Azure Event Hubs. It follows the Azure SDK 'Track 2' design principles, offering an async-first API for high-throughput data streaming scenarios. The current version is 5.15.1, and it receives regular updates, typically monthly or bi-monthly, in alignment with other Azure SDKs.
Warnings
- breaking Version 5.0.0 introduced a complete rewrite following Azure SDK 'Track 2' guidelines. This includes an async-first API, new client class names (`EventHubProducerClient`, `EventHubConsumerClient`, `EventProcessorClient`), and removal of older classes like `EventHubClient`. Code written for v1-v4 will not work with v5+ without significant refactoring.
- gotcha The `EventProcessorClient` (used for distributed event processing with checkpointing) requires an *additional* storage provider package for checkpointing (e.g., `azure-eventhub-checkpointstoreblob` for Azure Blob Storage). Without this, checkpointing will not work, and the client will not save its progress.
- gotcha This library is async-first. Most operations (sending, receiving, creating batches, closing clients) are coroutines and *must* be `await`ed. Forgetting `await` will result in `RuntimeWarning: coroutine '...' was never awaited` or `TypeError: object NoneType can't be used in 'await' expression`.
- gotcha Python version compatibility has changed. While earlier v5.x versions supported Python >=3.6, `azure-eventhub>=5.14.0` requires `Python >=3.9`. Attempting to install or run on older Python versions (e.g., 3.7, 3.8) will fail during installation or at runtime.
Install
-
pip install azure-eventhub -
pip install azure-eventhub-checkpointstoreblob
Imports
- EventHubProducerClient
from azure.eventhub import EventHubProducerClient
- EventHubConsumerClient
from azure.eventhub import EventHubConsumerClient
- EventData
from azure.eventhub import EventData
- EventProcessorClient
from azure.eventhub import EventProcessorClient
- EventHubClient
from azure.eventhub import EventHubProducerClient, EventHubConsumerClient
Quickstart
import os
import asyncio
from azure.eventhub import EventHubProducerClient, EventData
# Retrieve connection string and Event Hub name from environment variables.
# For local development, replace 'os.environ.get(...)' with your actual values.
# Connection string for Event Hubs Namespace, NOT a specific Event Hub.
CONNECTION_STR = os.environ.get("EVENT_HUB_CONNECTION_STR", "Endpoint=sb://<NAMESPACE>.servicebus.windows.net/;SharedAccessKeyName=<KEY_NAME>;SharedAccessKey=<KEY>")
# The specific Event Hub name within the namespace
EVENTHUB_NAME = os.environ.get("EVENT_HUB_NAME", "<YOUR_EVENT_HUB_NAME>")
async def send_events():
producer = EventHubProducerClient.from_connection_string(
conn_str=CONNECTION_STR,
eventhub_name=EVENTHUB_NAME
)
async with producer:
event_data_batch = await producer.create_batch()
event_data_batch.add(EventData("Hello Azure Event Hubs!"))
event_data_batch.add(EventData("This is my second event."))
await producer.send_batch(event_data_batch)
print("Sent a batch of two events successfully.")
if __name__ == "__main__":
# Ensure you have set the EVENT_HUB_CONNECTION_STR and EVENT_HUB_NAME
# environment variables or replaced the placeholders.
if not CONNECTION_STR or '<NAMESPACE>' in CONNECTION_STR:
print("Please set the EVENT_HUB_CONNECTION_STR and EVENT_HUB_NAME environment variables.")
print("Example: export EVENT_HUB_CONNECTION_STR='Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...'")
print("Example: export EVENT_HUB_NAME='myhub'")
else:
asyncio.run(send_events())