Azure Event Hubs Tutorials

Unlock the power of real-time data streaming

Get Started with Azure Event Hubs

1. What is Azure Event Hubs?

Azure Event Hubs is a big data streaming platform and event-ingestion service. It can receive and process millions of events per second. Use Event Hubs to process real-time analytics workloads that are built on your streaming data.

Key features include:

2. Prerequisites

Before you begin, ensure you have the following:

3. Creating an Event Hubs Namespace and Event Hub

An Event Hubs namespace is a unique scoping container for your Event Hubs. You'll need to create this first, and then create an Event Hub within it.

Using Azure CLI:

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

# Log in to Azure
az login

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

# Create a resource group
az group create --name MyEventHubsResourceGroup --location "East US"

# Create an Event Hubs namespace
az eventhubs namespace create --name MyEventHubsNamespace --resource-group MyEventHubsResourceGroup --location "East US" --sku Standard

# Create an Event Hub within the namespace
az eventhubs eventhub create --name myeventhub --namespace-name MyEventHubsNamespace --resource-group MyEventHubsResourceGroup

Note: Replace YOUR_SUBSCRIPTION_ID with your actual Azure subscription ID and choose a unique name for your namespace and event hub.

4. Sending and Receiving Events

Now that you have your Event Hub set up, you can start sending and receiving data.

Using Azure SDKs (Python Example):

Install the necessary Python packages:

pip install azure-eventhub
Sending Events:

Create a Python file (e.g., send_events.py):

import os
from azure.eventhub import EventHubProducerClient, EventData

# Replace with your connection string and event hub name
CONNECTION_STR = os.environ.get("EVENTHUB_CONNECTION_STR") # Recommended: Use environment variables
EVENT_HUB_NAME = "myeventhub" # Your event hub name

def send_event_data(events):
    producer = EventHubProducerClient.from_connection_string(CONNECTION_STR, event_hub_name=EVENT_HUB_NAME)
    try:
        with producer:
            event_data_batch = producer.create_batch()
            for event in events:
                event_data_batch.add(EventData(event))
            producer.send_batch(event_data_batch)
            print(f"Sent {len(events)} events.")
    except Exception as e:
        print(f"Error sending events: {e}")

if __name__ == "__main__":
    sample_events = [
        "Hello, Event Hubs!",
        "This is a test message.",
        "Another event."
    ]
    send_event_data(sample_events)

To run this, set the EVENTHUB_CONNECTION_STR environment variable with your Event Hubs connection string found in the Azure portal. Then run: python send_events.py

Receiving Events:

Create a Python file (e.g., receive_events.py):

import os
from azure.eventhub import EventHubConsumerClient

# Replace with your connection string and event hub name
CONNECTION_STR = os.environ.get("EVENTHUB_CONNECTION_STR") # Recommended: Use environment variables
EVENT_HUB_NAME = "myeventhub" # Your event hub name
CONSUMER_GROUP = "$Default" # Or your custom consumer group

def process_event(event):
    print(f"Received event: {event.body_as_str()}")

def main():
    consumer_client = EventHubConsumerClient.from_connection_string(
        connection_str=CONNECTION_STR,
        consumer_group=CONSUMER_GROUP,
        event_hub_name=EVENT_HUB_NAME
    )
    try:
        with consumer_client:
            consumer_client.receive(on_event=process_event)
    except Exception as e:
        print(f"Error receiving events: {e}")

if __name__ == "__main__":
    main()

To run this, set the EVENTHUB_CONNECTION_STR environment variable. Then run: python receive_events.py

Ready to explore more?

Dive deeper into advanced scenarios and best practices.

Explore More Tutorials