Azure Event Hubs - Reference

Getting Started Guide

Getting Started with Azure Event Hubs

Welcome to the getting started guide for Azure Event Hubs. This section will walk you through the essential steps to set up and start using Event Hubs for real-time data streaming.

1. Prerequisites

Before you begin, ensure you have the following:

2. Create an Azure Event Hubs Namespace

An Event Hubs namespace is a container for all your Event Hubs instances. You can create a namespace using the Azure portal or the Azure CLI.

Using Azure CLI:

Open your terminal or command prompt and run the following commands:


# Log in to your Azure account
az login

# Set your Azure subscription (replace with your subscription ID)
az account set --subscription ""

# Create a resource group (if you don't have one)
az group create --name MyResourceGroup --location "East US"

# Create an Event Hubs namespace
az eventhubs namespace create --resource-group MyResourceGroup --name MyEventHubsNamespace --location "East US" --sku Basic
        
Replace <your-subscription-id> with your actual Azure subscription ID. The --sku can be Basic, Standard, or Premium depending on your needs.

3. Create an Event Hub

An Event Hub is the actual entity within a namespace where you send and receive events.

Using Azure CLI:


# Create an Event Hub within the namespace
az eventhubs eventhub create --resource-group MyResourceGroup --namespace-name MyEventHubsNamespace --name MyEventHub --partition-count 2 --message-retention-in-days 7
        

Here:

4. Get Connection Strings

To connect your applications to Event Hubs, you'll need connection strings. These contain authorization credentials.

Using Azure CLI:

Get the primary connection string for your namespace:


az eventhubs namespace authorization-rule list --resource-group MyResourceGroup --namespace-name MyEventHubsNamespace --query "[?KeyName=='RootManageSharedAccessKey'].PrimaryConnectionString" -o tsv
        
The RootManageSharedAccessKey grants full access. For production environments, it's recommended to create specific authorization rules with minimal required permissions.

5. Sending and Receiving Events

Now that your Event Hub is set up, you can start sending and receiving events. This typically involves using one of the Azure Event Hubs SDKs.

Example with .NET SDK:

Add the following NuGet package:


dotnet add package Azure.Messaging.EventHubs
        

Here's a simplified example of sending an event:


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

public class EventSender
{
    private const string connectionString = "";
    private const string eventHubName = "MyEventHub";
    private EventHubProducerClient producer;

    public EventSender()
    {
        producer = new EventHubProducerClient(connectionString, eventHubName);
    }

    public async Task SendEventAsync(string eventData)
    {
        using EventData data = new EventData(Encoding.UTF8.GetBytes(eventData));
        try
        {
            await producer.SendAsync(new EventData[] { data });
            Console.WriteLine($"Sent event: {eventData}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error sending event: {ex.Message}");
        }
    }
}
        

And receiving events:


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

public class EventReceiver
{
    private const string connectionString = "";
    private const string eventHubName = "MyEventHub";
    private EventHubConsumerClient consumer;

    public EventReceiver()
    {
        consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, eventHubName);
    }

    public async Task StartReceivingAsync()
    {
        await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync())
        {
            string data = Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray());
            Console.WriteLine($"Received event: {data} from partition {partitionEvent.Partition.Id}");
        }
    }
}
        
For more comprehensive examples and other languages (Python, Java, Node.js), please refer to the Code Samples section.

6. Next Steps