Azure Event Hubs SDK References

Comprehensive API and client library documentation for developers.

SDK References

Getting Started

This section provides an overview of the Azure Event Hubs SDKs, covering installation, authentication, and basic usage patterns for common scenarios.

Core Concepts

Event Hubs is a highly scalable data streaming platform and event ingestion service. The SDKs allow you to interact with Event Hubs for sending and receiving events.

Installation

Refer to the specific language documentation for the latest installation instructions:

  • .NET: Install the Azure.Messaging.EventHubs NuGet package.
  • Java: Add the Maven dependency for com.azure:azure-messaging-eventhubs.
  • Python: Install the azure-eventhub package using pip.
  • JavaScript: Install the @azure/event-hubs package.
  • Go: Use Go Modules to add the github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs module.

Authentication

Securely authenticate your application using connection strings or Azure Active Directory (Azure AD) credentials.

// Example: .NET Connection String Authentication
var connectionString = "Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=your-key-name;SharedAccessKey=your-key";
var client = new EventHubProducerClient(connectionString, "your-event-hub-name");

Event Producers (Sending Events)

Learn how to create and send batches of events to an Event Hub. Efficient batching is crucial for performance.

Creating an Event Producer Client

Initialize the client with your Event Hub namespace and name.

Sending Events

Events can be sent individually or as part of an EventDataBatch for efficiency.

// Example: Python Sending a Batch
from azure.eventhub import EventHubProducerClient

connection_str = "..."
consumer_group = "$Default"
eventhub_name = "..."

producer = EventHubProducerClient(fully_qualified_namespace=connection_str.split(";")[0].split("=")[1], event_hub_name=eventhub_name)

events = ["Event 1", "Event 2"]

with producer:
    batch = producer.create_batch()
    for event_data in events:
        try:
            batch.add(EventData(event_data.encode('utf-8'))
        except ValueError:
            producer.send_batch(batch)
            batch = producer.create_batch()
            batch.add(EventData(event_data.encode('utf-8'))
    producer.send_batch(batch)

Event Consumers (Receiving Events)

Discover how to efficiently read events from an Event Hub using consumer groups. This includes managing checkpoints for reliable processing.

Creating an Event Hub Consumer Client

Initialize a client that can create event processors to read from partitions.

Processing Events

Implement callback functions to handle incoming events and update checkpoints.

// Example: JavaScript Event Processor Host
const { EventHubConsumerClient, Subscription } = require("@azure/event-hubs");

const connectionString = "...";
const eventHubName = "...";

const consumerClient = new EventHubConsumerClient(connectionString, eventHubName);

async run() {
    const subscription = consumerClient.subscribe({
        async processEvents(events, context) {
            for (const event of events) {
                console.log(`Received event: ${event.body}`);
            }
            // Update checkpoint after processing
            await context.updateCheckpoint(events[events.length - 1]);
        },
        async processError(err, context) {
            console.error(`Error occurred: ${err}`);
        }
    });
    // Keep the process running
    await new Promise(resolve => setTimeout(resolve, 60000));
    await subscription.close();
}

run().catch(console.error);

Management Operations

Programmatically manage Event Hubs entities, such as creating, deleting, or updating Event Hubs, consumer groups, and authorization rules.

These operations are typically performed using separate SDKs or the Azure portal.

Error Handling

Implement robust error handling to gracefully manage transient errors, network issues, and unexpected events. The SDKs provide specific exception types for common scenarios.

Common Errors:

  • EventHubsException: Base class for Event Hubs-related errors.
  • PartitionUnavailableException: Indicates a partition is temporarily unavailable.
  • QuotaExceededException: When sending events exceeds configured limits.

Best Practices

  • Batching: Always batch events when sending for optimal throughput and reduced latency.
  • Checkpointing: Regularly update checkpoints for event consumers to ensure reliable delivery and avoid reprocessing.
  • Connection Pooling: Reuse producer and consumer clients to benefit from connection pooling.
  • Error Handling: Implement retry logic with exponential backoff for transient errors.
  • Monitoring: Utilize Azure Monitor to track Event Hubs metrics and diagnose issues.
  • Partition Key: Use partition keys strategically to ensure related events are processed in order on the same partition.