Getting Started with Azure Event Hubs

Welcome to Azure Event Hubs! This guide will walk you through the essential steps to get started with publishing and consuming events using this powerful real-time data streaming service.

Azure Event Hubs is a Big Data Streaming Platform and event-ingestion service. It can receive and process millions of events per second. Applications that produce or consume Event Hubs data can do so in a variety of ways. Event Hubs is designed for high-throughput data streaming and real-time analytics.

Prerequisites

Before you begin, ensure you have the following:

  • An active Azure subscription. If you don't have one, you can create a free account.
  • The Azure CLI installed and configured, or access to the Azure portal.

Step 1: Create an Event Hubs Namespace

An Event Hubs namespace is a logical container for your Event Hubs. All Event Hubs are deployed within a namespace. A namespace provides a unique DNS name, through which you can access your Event Hubs.

Using Azure Portal:

  1. Sign in to the Azure portal.
  2. Search for "Event Hubs" and select "Event Hubs Namespaces".
  3. Click "+ Create".
  4. Fill in the required details: Subscription, Resource Group, Region, Namespace name, and Pricing tier.
  5. Click "Review + create" and then "Create".

Using Azure CLI:


az eventhubs namespace create --name  --resource-group  --location  --sku Standard
                    

Replace <your-namespace-name>, <your-resource-group>, and <your-region> with your desired values.

Step 2: Create an Event Hub

Within your Event Hubs namespace, you create Event Hubs. An Event Hub is the entity that holds the data streams.

Using Azure Portal:

  1. Navigate to your newly created Event Hubs namespace.
  2. In the left-hand menu, select "Event Hubs".
  3. Click "+ Event Hub".
  4. Enter a name for your Event Hub (e.g., myeventhub).
  5. Configure settings like Partition count and Message retention if needed, or use the defaults.
  6. Click "Create".

Using Azure CLI:


az eventhubs eventhub create --name  --namespace-name  --resource-group 
                    

Replace <your-eventhub-name>, <your-namespace-name>, and <your-resource-group>.

Step 3: Obtain Connection String

To connect your applications to Event Hubs, you'll need a connection string. This string contains authorization credentials.

Using Azure Portal:

  1. Navigate to your Event Hubs namespace.
  2. In the left-hand menu, select "Shared access policies".
  3. Choose an existing policy (e.g., "RootManageSharedAccessKey") or create a new one with appropriate permissions (Listen, Send, or Manage).
  4. Copy the "Primary Connection String" or "Secondary Connection String".

Using Azure CLI:


az eventhubs authorization-rule list --namespace-name  --resource-group  --output table
az eventhubs authorization-rule keys list --resource-group  --namespace-name  --name  --output json
                    

You'll typically use the connection string for the root policy or a custom policy you create for sending and listening.

Step 4: Send Events

Now, let's send some events to your Event Hub. You can use various SDKs (e.g., .NET, Java, Python, Node.js) or tools.

Example using Azure SDK for Python:


from azure.eventhub import EventHubProducerClient, EventData

connection_str = "<your-connection-string>"
eventhub_name = "<your-eventhub-name>"

producer = EventHubProducerClient.from_connection_string(connection_str, eventhub_name=eventhub_name)

events_to_send = [
    EventData("Event 1"),
    EventData("Event 2"),
    EventData("Event 3")
]

with producer:
    producer.send_batch(events_to_send)
    print("Events sent successfully!")
                    

Remember to replace <your-connection-string> and <your-eventhub-name>.

Explore Sending Tutorials

Step 5: Receive Events

Applications can consume events from an Event Hub using Consumer Groups. A consumer group allows multiple applications or instances of the same application to read from an Event Hub independently.

Example using Azure SDK for Python:


from azure.eventhub import EventHubConsumerClient

connection_str = "<your-connection-string>"
eventhub_name = "<your-eventhub-name>"
consumer_group = "$Default" # Or your custom consumer group name

consumer = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)

def on_event(partition_context, event):
    print(f"Received event: {event.body_as_str()}")
    # Process the event here

with consumer:
    consumer.receive(on_event)
                    

Replace placeholders as before. You might need to create a custom consumer group if you don't want to use the default one.

Learn about Consumer Groups

Next Steps

Congratulations! You've successfully set up and sent/received your first events with Azure Event Hubs. Here are some resources to help you go further: