Azure Event Hubs client library for Python Samples
These are code samples that show common scenario operations with the Azure Event Hubs client library. Both sync version and async version of samples are provided.
client_creation.py (async version) - Examples to create EventHubProducerClient and EventHubConsumerClient:
- From a connection string
- From a shared access key
- Creation with configuration parameters
send.py (async version) - Examples to send events:
- Send event data batch
- Send event data batch within limited size
- Send event data batch to a specific partition determined by partition key
- Send event data batch to a specific partition by partition id
- Send event data batch with customized properties
send_stream.py (async version) - Examples to do streaming sending:
- Send in a stream
recv.py (async version) - Examples to receive events:
- Receive events
recv_track_last_enqueued_event_prop.py (async version) - Examples to get the latest enqueued event properties of a partition while receiving:
- Get the latest enqueued event properties of a partition while receiving
recv_with_custom_starting_position.py (async version) - Examples to start receiving from a specific position:
- Start receiving from a specific position
recv_with_checkpoint_store.py (async version) - Examples to receive events and do checkpoint using blob checkpoint store:
- Receive events and do checkpoint using blob checkpoint store
recv_with_checkpoint_by_event_count.py (async version) - Examples to receive events and do checkpoint by event count using blob checkpoint store:
- Receive events and do checkpoint every fixed amount of events (e.g. checkpoint every 20 events) using blob checkpoint store
receive_batch_with_checkpoint.py (async_version) - Examples to receive events in batches and do checkpoint by the batch:
- Receive events in batches by calling
EventHubConsumer.receive_batch
and do checkpoint with the last event of that batch.
- Receive events in batches by calling
recv_with_checkpoint_by_time_interval.py (async version) - Examples to receive events and do checkpoint by time interval using blob checkpoint store:
- Receive events and do checkpoint every fixed time interval (e.g. checkpoint every 20 seconds) using blob checkpoint store
recv_for_period.py (async version) - Examples to receive events for a period of time:
- Receive events for a period of time
client_identity_authentication.py (async version) - Examples for authentication by Azure Active Directory:
- Authenticating and creating the client utilizing the
azure.identity
library
- Authenticating and creating the client utilizing the
connection_string_authentication.py (async version) - Examples for authentication by connection string:
- Authenticating and creating the client utilizing a connection string.
proxy.py (async version) - Examples to send and receive events behind a proxy:
- Send and receive events behind a proxy
iot_hub_connection_string_receive_async.py - Examples to receive events from an IoT Hub:
- Convert an IoT Hub connection string to the built-in Event Hub endpoint and receive events from it
authenticate_with_sas_token.py (async_version) - Examples:
- Utilize a SAS token to authenticate when creating an Event Hub client.
authenticate_with_azure_sas_credential.py (async_version) - Examples:
- Utilize
azure.core.credentials.AzureSasCredential
to authenticate when creating an Event Hub client.
- Utilize
authenticate_with_azure_named_key_credential.py (async_version) - Examples:
- Utilize
azure.core.credentials.AzureNamedKeyCredential
to authenticate when creating an Event Hub client.
- Utilize
connection_to_custom_endpoint_address.py (async version) - Examples:
- Create EventHubProducerClient and EventHubConsumerClient that connect to a custom endpoint with a custom certificate.
send_and_receive_amqp_annotated_message.py (async_version) - Examples to send AMQPAnnotatedMessage to and receive events from an event hub and parse the body:
- Send AMQPAnnotatedMessage of different body types.
- Receive messages and parse the body according to the body type.
send_buffered_mode.py (async_version) - Examples to send events in buffered mode:
- Send single events, which will be automatically batched.
- Send a batch of events by enqueuing an EventDataBatch object to the buffer.
- Send events in buffer immediately by calling
flush
.
Prerequisites
Python 3.8 or later.
Microsoft Azure Subscription: To use Azure services, including Azure Event Hubs, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a free trial or use your MSDN subscriber benefits when you create an account.
Event Hubs namespace with an Event Hub: To interact with Azure Event Hubs, you'll also need to have a namespace and Event Hub available. If you are not familiar with creating Azure resources, you may wish to follow the step-by-step guide for creating an Event Hub using the Azure portal. There, you can also find detailed instructions for using the Azure CLI, Azure PowerShell, or Azure Resource Manager (ARM) templates to create an Event Hub.
Azure Storage Account (Optional): To run receiving samples with blob checkpoint store for persist checkpoint, you need to create an Azure Storage account and a Blob Container.
Setup
- Install the Azure Event Hubs client library for Python with pip:
pip install azure-eventhub
To run samples that utilize the Azure Active Directory for authentication, please install the azure-identity
library:
pip install azure-identity
To run receiving samples that utilize blob checkpoint store for persist checkpoint, please install the corresponding checkpoint store library:
pip install azure-eventhub-checkpointstoreblob # sync version
pip install azure-eventhub-checkpointstoreblob-aio # async version
- Clone or download this sample repository.
- Open the sample folder in Visual Studio Code or your IDE of choice.
Running the samples
- Open a terminal window and
cd
to the directory that the samples are saved in. - Set the environment variables specified in the sample file you wish to run.
- Follow the usage described in the file, e.g.
python send.py
.
Next steps
Check out the API reference documentation to learn more about what you can do with the Azure Event Hubs client library.