Azure Event Hubs

Reliable, scalable, real-time data streaming

Getting Started with Azure Event Hubs

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

1. Create an Azure Event Hubs Namespace

An Event Hubs namespace is a logical container for your Event Hubs instances. To create one:

  1. Go to the Azure portal.
  2. Search for "Event Hubs" and select it.
  3. Click "Create".
  4. Choose your subscription, resource group, and a region.
  5. Provide a unique name for your namespace (e.g., `my-eventhub-ns-unique`).
  6. Select a pricing tier (Basic, Standard, Premium). For getting started, Standard is often sufficient.
  7. Click "Review + create", then "Create".

2. Create an Event Hub

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

  1. Navigate to your newly created Event Hubs namespace in the Azure portal.
  2. Under "Entities", click on "Event Hubs".
  3. Click "+ Event Hub".
  4. Enter a name for your Event Hub (e.g., `my-data-stream`).
  5. Configure the settings for message retention and partitions as needed. For testing, default settings are usually fine.
  6. Click "Create".
Note: The number of partitions determines the throughput and concurrency you can achieve. You can change this later, but it's often easier to get it right initially.

3. Obtain Connection Strings

To connect your applications to Event Hubs, you'll need connection strings. These contain the necessary authentication and endpoint information.

  1. In your Event Hubs namespace, go to "Shared access policies" under "Settings".
  2. You'll see a "RootManageSharedAccessKey" policy. Click on it.
  3. Copy the "Primary Connection String". This string contains keys for both sending and listening.

Keep this connection string secure, as it grants access to your Event Hubs namespace.

Tip: For production applications, consider using Azure Active Directory (Azure AD) authentication for enhanced security instead of shared access signatures (SAS) keys.

4. Send Your First Message

You can send messages using various tools and SDKs. Here's a simple example using Azure CLI:

Using Azure CLI

First, ensure you have the Azure CLI installed and logged in (`az login`).


az eventhubs send --namespace-name <your-namespace-name> \
                  --event-hub-name <your-event-hub-name> \
                  --resource-group <your-resource-group-name> \
                  --body "Hello, Event Hubs!"
            

Replace the placeholders with your actual namespace, event hub, and resource group names.

Using SDKs

For programmatic sending and receiving, you'll use the Event Hubs SDK for your preferred language (.NET, Java, Python, JavaScript). Here's a conceptual snippet for Python:


from azure.eventhub import EventHubProducerClient

# Replace with your actual connection string and hub name
connection_str = "<YOUR_EVENTHUBS_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 = {"message": "This is a test message."}
    producer.send_event(event_data)
    print("Message sent successfully!")
            

5. Receive Messages

Receiving messages involves creating a consumer or using an Event Hubs consumer library. The process typically involves specifying a consumer group and starting to receive events.

Event Hubs automatically stores messages for a configurable retention period. Consumer groups allow multiple applications or services to read from the same Event Hub independently.

Continue exploring the documentation to learn about advanced features like:

Note: For production workloads, carefully consider your partitioning strategy and throughput requirements.