Creating Event Hubs

This guide walks you through the process of creating Event Hubs within an Azure Event Hubs namespace. Event Hubs are the central entities within a namespace where events are sent and received.

Prerequisites

Creating an Event Hub using the Azure Portal

The Azure Portal provides a user-friendly interface for creating and managing Event Hubs.

1

Navigate to your Event Hubs namespace in the Azure Portal.

Azure Portal Namespace Overview
2

In the left-hand menu, under Entities, select Event Hubs.

3

Click the + Event Hub button at the top of the Event Hubs list.

Add Event Hub Button
4

Enter the name for your Event Hub in the Name field. This name must be unique within the namespace. You can also configure the following optional settings:

  • Message time to live (minutes): The duration for which messages are retained in the Event Hub.
  • Partition count: The number of partitions for the Event Hub. Partitions are ordered, immutable sequences of events.
  • Capture: Enable to automatically capture Event Hub data to Azure Blob Storage or Azure Data Lake Storage.
Create Event Hub Form
5

Click Create.

Creating an Event Hub using Azure CLI

You can also create Event Hubs programmatically using the Azure Command-Line Interface (CLI).

First, ensure you have the Azure CLI installed and are logged in to your Azure account.

# Log in to your Azure account
az login

# Set your subscription (if you have multiple)
az account set --subscription "Your Subscription Name or ID"

# Create an Event Hub
az eventhubs event-hubs create --namespace-name  \
                               --resource-group  \
                               --name  \
                               --partition-count  \
                               --message-retention-in-days 

Replace the placeholders with your specific values:

Important: The partition count is set at the time of Event Hub creation and cannot be changed later. Consider your expected throughput and scaling needs when choosing this value.

Creating an Event Hub using Azure SDKs

You can also create Event Hubs using the Azure SDKs for various programming languages. Here's an example using the Azure SDK for Python:

from azure.eventhub import EventHubClient, EventHubProducer

CONNECTION_STR = "YOUR_EVENTHUBS_CONNECTION_STRING"
NAMESPACE = "YOUR_NAMESPACE_NAME"
EVENTHUB_NAME = "YOUR_EVENTHUB_NAME"

def create_eventhub(connection_string, namespace_name, eventhub_name):
    try:
        # For creating Event Hubs, you typically use the management client,
        # but a producer can be used to check existence and potentially
        # implicitly create if the API allows or to interact with it.
        # For explicit creation, use the Azure management SDK or ARM templates.

        # Example using the management library for explicit creation:
        # from azure.mgmt.eventhub import EventHubManagementClient
        # from azure.identity import DefaultAzureCredential

        # credential = DefaultAzureCredential()
        # eventhub_management_client = EventHubManagementClient(credential, "YOUR_SUBSCRIPTION_ID")

        # eventhub_management_client.event_hubs.create_or_update(
        #     resource_group_name="YOUR_RESOURCE_GROUP",
        #     namespace_name=namespace_name,
        #     event_hub_name=eventhub_name,
        #     parameters={} # Add partition_count, message_retention_in_days if needed
        # )
        # print(f"Event Hub '{eventhub_name}' created successfully.")

        # For simplicity and direct interaction, we'll assume the namespace exists
        # and focus on how you'd get a producer to an *existing* Event Hub.
        # Creating an Event Hub is a management operation.

        print(f"To create an Event Hub '{eventhub_name}' programmatically, "
              f"please use the Azure Resource Manager (ARM) templates or "
              f"the Azure management SDK for Python, Java, C#, etc. "
              f"This code snippet demonstrates producer creation for an existing hub.")

        # This part shows how to connect to an existing Event Hub
        client = EventHubClient.from_connection_string(connection_string, event_hub_path=eventhub_name)
        producer = client.create_producer()
        print(f"Successfully created producer for Event Hub: {eventhub_name}")
        # You would then use 'producer' to send events.
        # producer.close()
        # client.close()

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    # Replace with your actual connection string, namespace, and desired Event Hub name
    # Note: The connection string for creating the hub might need specific permissions.
    # Best practice is to use ARM templates or management SDKs for creation.
    # create_eventhub(CONNECTION_STR, NAMESPACE, EVENTHUB_NAME)

    print("Please refer to Azure documentation for programmatic Event Hub creation using management APIs or ARM templates.")
    print("This example focuses on interacting with an *existing* Event Hub.")
Tip: For robust infrastructure management, consider using Azure Resource Manager (ARM) templates or Terraform to define and create your Event Hubs and namespaces. This ensures repeatability and version control.

Next Steps

Once your Event Hub is created, you can start configuring its properties or sending and receiving events. The following guides will help you: