Getting Started with Azure Event Hubs

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

Create an Azure Event Hubs Namespace

An Event Hubs namespace is a logical container that holds your Event Hubs. All Event Hubs within a namespace share a common namespace endpoint and security tokens.

  1. Sign in to the Azure portal.
  2. Search for "Event Hubs" and select it.
  3. Click "Create" to start the namespace creation process.
  4. Choose your subscription, resource group, and select a region.
  5. Provide a unique name for your Event Hubs namespace.
  6. Select a pricing tier (Standard is recommended for most use cases).
  7. Review and create the namespace.

It might take a few minutes for the namespace to be provisioned.

Create an Event Hub

Once your namespace is ready, you need to create an Event Hub within it. This is where your event data will be ingested.

  1. Navigate to your newly created Event Hubs namespace in the Azure portal.
  2. Click on "+ Event Hub" in the overview page.
  3. Enter a name for your Event Hub (e.g., my-event-hub).
  4. Configure settings like the number of partitions and message retention period. For starters, default values are usually fine.
  5. Click "Create".

Get Connection Strings

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

  1. In your Event Hubs namespace, navigate to "Shared access policies" under "Settings".
  2. You'll see a "RootManageSharedAccessKey" policy. Click on it.
  3. Copy the "Primary Connection String" or "Secondary Connection String". Store this securely; it grants full access to the namespace.
  4. Alternatively, you can create more granular policies with specific permissions (Listen, Send, Manage) for better security.
Security Alert: Never embed connection strings directly in client-side code or commit them to public repositories. Use environment variables or secure secret management services.

Install Azure Event Hubs SDK

To interact with Event Hubs programmatically, you'll need the Azure SDK for your preferred language.

Example for .NET:


dotnet add package Azure.Messaging.EventHubs
                

Example for Python:


pip install azure-eventhub
                

Example for Node.js:


npm install @azure/event-hubs
                

Refer to the official documentation for other languages.

Write Your First Application

Now you can start sending and receiving events. Here's a conceptual example of how you might send a message.

Conceptual Example (Python):


from azure.eventhub import EventHubProducerClient, EventData

connection_str = "YOUR_EVENT_HUB_CONNECTION_STRING"
event_hub_name = "YOUR_EVENT_HUB_NAME"

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

with producer:
    event_data_batch = producer.create_batch()
    event_data_batch.add(EventData("Hello, Event Hubs!"))
    producer.send_batch(event_data_batch)
    print("Message sent successfully!")
                

Remember to replace placeholder values with your actual connection string and Event Hub name.

For detailed examples on sending and receiving, please refer to the Sending & Receiving Events tutorial.

Next Steps

Congratulations! You've successfully set up Azure Event Hubs and are ready to stream data. Here are some recommended next steps:

Learn to Send & Receive Events