Azure Event Hubs Tutorials

Consuming Events from Azure Event Hubs

This tutorial will guide you through the process of consuming events from an Azure Event Hub using a simple consumer application. We'll cover setting up the necessary prerequisites, writing the consumer code, and running it to process incoming events.

Prerequisites

Step 1: Obtain Connection Information

You'll need the connection string for your Event Hubs namespace. You can find this in the Azure portal under your Event Hubs namespace -> Shared access policies. Ensure the policy has 'Listen' permissions.

It's highly recommended to use a dedicated consumer group for your application to avoid interfering with other consumers. You can create a consumer group in the Azure portal under your Event Hub -> Consumer groups.

Step 2: Create a Consumer Application

We'll use a C# console application for this example. You can create a new project using the .NET CLI:


dotnet new console -o EventHubsConsumer
cd EventHubsConsumer

Next, add the necessary Azure Event Hubs client library to your project:


dotnet add package Azure.Messaging.EventHubs
dotnet add package Azure.Messaging.EventHubs.Consumer

Step 3: Write the Consumer Code

Replace the contents of your Program.cs file with the following C# code:


using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;

class Program
{
    // Replace with your Event Hub connection string and consumer group name
    private const string EventHubConnectionString = "YOUR_EVENT_HUB_CONNECTION_STRING";
    private const string ConsumerGroupName = "$Default"; // Or your custom consumer group name
    private const string EventHubName = "YOUR_EVENT_HUB_NAME"; // Name of your Event Hub

    static async Task Main(string[] args)
    {
        Console.WriteLine("Starting Event Hubs consumer...");

        // Create a consumer client using the connection string, hub name, and consumer group name
        await using var consumer = new EventHubConsumerClient(
            EventHubConsumerClient.DefaultConsumerGroupName, // Use DefaultConsumerGroupName for $Default
            EventHubConnectionString,
            EventHubName);

        Console.WriteLine("Listening for events...");

        try
        {
            // Begin receiving events from the Event Hub
            await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync())
            {
                Console.WriteLine($"\nReceived event: Sequence number {partitionEvent.Data.SequenceNumber}, Offset {partitionEvent.Data.Offset}");
                Console.WriteLine($"Partition ID: {partitionEvent.Partition.Id}");
                Console.WriteLine($"Enqueued time: {partitionEvent.Data.EnqueuedTime}");
                Console.WriteLine($"Body: {Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray())}");

                // Optional: Add your custom logic here to process the event body
                // For example, parsing JSON, storing in a database, etc.
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        finally
        {
            Console.WriteLine("Consumer stopped.");
        }
    }
}

Important:

Step 4: Run the Consumer Application

Build and run your application from the terminal:


dotnet run

Your application will now start listening for events on the specified Event Hub and consumer group. Any events published to the Event Hub will be displayed in the console output.

Handling Checkpoints (Advanced)

For production scenarios, it's crucial to implement checkpointing. Checkpointing allows your consumer to keep track of which events it has successfully processed. This prevents duplicate processing if the consumer restarts or crashes. The Azure.Messaging.EventHubs.Consumer library provides mechanisms for this, often involving Azure Blob Storage or other persistent storage.

Refer to the official Azure Event Hubs documentation on checkpointing for detailed implementation guidance.

Troubleshooting and Best Practices

Congratulations! You have successfully learned how to consume events from Azure Event Hubs. You can now integrate this consumer logic into your applications to process streaming data.