Azure Event Hubs SDK Reference
This section provides detailed reference information for the various SDKs available for interacting with Azure Event Hubs. Explore the core components, classes, and methods used to build powerful event-driven applications.
Core Concepts in SDK Interaction
Understanding these fundamental concepts will help you effectively use the SDKs to send and receive events.
EventData Objects
The EventData class is the primary representation of an event within the SDK. It encapsulates the event's body, properties, system properties, and metadata.
- Body: The actual payload of the event, typically represented as a byte array.
- Properties: Custom key-value pairs that you can attach to an event.
- System Properties: Metadata added by Event Hubs, such as sequence number, offset, and partition key.
Partition Clients
PartitionClient instances are used to interact with a specific partition within an Event Hub. This allows for ordered processing within a partition.
- Used for sending events to a specific partition.
- Used for receiving events from a specific partition.
Event Hub Clients
EventHubClient (or its equivalent in different SDKs) serves as the main entry point for interacting with an Event Hub. It manages connections and allows for operations across partitions.
Common SDK Operations
Sending Events
Sending events is a fundamental operation. The SDKs provide methods to send single events or batches of events efficiently.
Sending a Single Event (Conceptual Example)
// C# Example (Conceptual)
var eventHubClient = new EventHubClient("YOUR_EVENTHUB_CONNECTION_STRING");
var eventData = new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"));
await eventHubClient.SendAsync(eventData, "partition_id_optional");
Sending a Batch of Events
Batching events improves throughput and reduces costs. The SDKs provide mechanisms to create and send event batches.
// Java Example (Conceptual)
EventHubProducerClient producerClient = new EventHubClientBuilder()
.connectionString("YOUR_EVENTHUB_CONNECTION_STRING")
.buildProducerClient();
List<EventData> events = new ArrayList<>();
events.add(new EventData("Event 1".getBytes()));
events.add(new EventData("Event 2".getBytes()));
producerClient.send(events);
Receiving Events
Receiving events involves creating consumers that listen for new events in partitions.
Receiving Events with an Event Processor
The Event Processor pattern is recommended for robust event consumption, handling checkpointing and load balancing.
# Python Example (Conceptual)
from azure.eventhub import EventHubConsumerClient
consumer_client = EventHubConsumerClient.from_connection_string(
"YOUR_EVENTHUB_CONNECTION_STRING",
consumer_group="$Default",
event_hub_name="your_event_hub_name"
)
def on_event(partition_context, event):
print(f"Received event: {event.body}")
# Process the event and optionally checkpoint
partition_context.update_checkpoint(event)
consumer_client.receive(on_event)
Key Classes and Interfaces
| SDK | Client Class | Event Object | Producer/Sender | Consumer/Receiver | Processor |
|---|---|---|---|---|---|
| .NET | EventHubClient, PartitionClient |
EventData |
EventHubProducerClient |
EventHubConsumerClient |
EventProcessorClient |
| Java | EventHubClient |
EventData |
EventHubProducerClient |
EventHubConsumerClient |
EventProcessorClient |
| JavaScript | EventHubClient |
EventData |
EventHubProducerClient |
EventHubConsumerClient |
EventProcessorClient |
| Python | EventHubClient |
EventData |
EventHubProducerClient |
EventHubConsumerClient |
EventProcessorClient |
| Go | Client |
Event |
NewSender |
NewReceiver |
N/A (Manual checkpointing) |
Error Handling and Retries
The SDKs implement robust error handling and retry mechanisms. It's important to understand how these work to ensure reliable event processing.
- Transient Errors: The SDKs automatically retry operations that fail due to transient network issues or service unavailability.
- Configurable Retries: Options exist to configure the number of retries and the delay between them.
- Custom Error Handling: Implement logic to handle non-retriable errors gracefully.