0
0
Azurecloud~5 mins

Event Hubs for streaming data in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Event Hubs lets you collect and process large streams of data in real time. It solves the problem of handling many messages from devices or apps quickly and reliably.
When you want to collect sensor data from many devices at once.
When you need to process logs or telemetry from multiple servers in real time.
When you want to build a live dashboard that updates as new data arrives.
When you need to send data streams to analytics or storage systems continuously.
When you want to decouple data producers and consumers for better scalability.
Config File - eventhub_namespace.bicep
eventhub_namespace.bicep
param location string = 'eastus'
param eventHubNamespaceName string = 'example-eh-namespace'
param eventHubName string = 'example-eventhub'

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2022-10-01-preview' = {
  name: eventHubNamespaceName
  location: location
  sku: {
    name: 'Standard'
    tier: 'Standard'
    capacity: 1
  }
  properties: {
    isAutoInflateEnabled: false
    maximumThroughputUnits: 0
  }
}

resource eventHub 'Microsoft.EventHub/namespaces/eventhubs@2022-10-01-preview' = {
  name: '${eventHubNamespaceName}/${eventHubName}'
  properties: {
    messageRetentionInDays: 1
    partitionCount: 2
  }
  dependsOn: [eventHubNamespace]
}

This Bicep file creates an Event Hubs namespace and an Event Hub inside it.

eventHubNamespace: The container for your Event Hubs, with a Standard pricing tier.

eventHub: The actual Event Hub that receives streaming data, with 2 partitions and 1 day retention.

Commands
This command deploys the Event Hubs namespace and Event Hub to the Azure resource group named example-rg using the Bicep template.
Terminal
az deployment group create --resource-group example-rg --template-file eventhub_namespace.bicep
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Resources/deployments/deploymentName", "name": "deploymentName", "properties": { "provisioningState": "Succeeded", "outputs": {} } }
--resource-group - Specifies the Azure resource group to deploy to
--template-file - Specifies the Bicep template file to use for deployment
This command checks the details of the Event Hub to confirm it was created successfully.
Terminal
az eventhubs eventhub show --resource-group example-rg --namespace-name example-eh-namespace --name example-eventhub
Expected OutputExpected
{ "name": "example-eventhub", "partitionCount": 2, "messageRetentionInDays": 1, "status": "Active" }
--resource-group - Specifies the resource group where the Event Hub exists
--namespace-name - Specifies the Event Hubs namespace name
--name - Specifies the Event Hub name
This command creates a consumer group to read data from the Event Hub, allowing multiple independent readers.
Terminal
az eventhubs eventhub consumer-group create --resource-group example-rg --namespace-name example-eh-namespace --eventhub-name example-eventhub --name example-consumer-group
Expected OutputExpected
{ "name": "example-consumer-group", "userMetadata": null }
--eventhub-name - Specifies the Event Hub to create the consumer group for
--name - Specifies the consumer group name
This command retrieves the connection string needed to send or receive data from the Event Hub.
Terminal
az eventhubs namespace authorization-rule keys list --resource-group example-rg --namespace-name example-eh-namespace --name RootManageSharedAccessKey
Expected OutputExpected
{ "keyName": "RootManageSharedAccessKey", "primaryConnectionString": "Endpoint=sb://example-eh-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=", "secondaryConnectionString": "Endpoint=sb://example-eh-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY=", "rights": [ "Listen", "Send", "Manage" ] }
--name - Specifies the authorization rule name to get keys for
Key Concept

If you remember nothing else from this pattern, remember: Event Hubs lets you collect and process many messages in real time by creating namespaces, event hubs, and consumer groups.

Common Mistakes
Trying to send or receive data before creating a consumer group.
Without a consumer group, clients cannot read data independently, causing conflicts or failures.
Always create a consumer group for each independent reader before consuming data.
Using the wrong resource group or namespace name in CLI commands.
Commands fail because the specified resources do not exist or are not found.
Double-check resource group and namespace names exactly match what was created.
Not retrieving the connection string before trying to connect to Event Hubs.
Clients cannot authenticate or send/receive data without the connection string.
Use the authorization rule keys list command to get the connection string first.
Summary
Deploy an Event Hubs namespace and Event Hub using a Bicep template.
Verify the Event Hub creation with the Azure CLI show command.
Create consumer groups to allow multiple independent readers.
Retrieve connection strings to connect clients for sending or receiving data.