Getting Started Guide
Welcome to the getting started guide for Azure Event Hubs. This section will walk you through the essential steps to set up and start using Event Hubs for real-time data streaming.
Before you begin, ensure you have the following:
An Event Hubs namespace is a container for all your Event Hubs instances. You can create a namespace using the Azure portal or the Azure CLI.
Open your terminal or command prompt and run the following commands:
# Log in to your Azure account
az login
# Set your Azure subscription (replace with your subscription ID)
az account set --subscription ""
# Create a resource group (if you don't have one)
az group create --name MyResourceGroup --location "East US"
# Create an Event Hubs namespace
az eventhubs namespace create --resource-group MyResourceGroup --name MyEventHubsNamespace --location "East US" --sku Basic
<your-subscription-id> with your actual Azure subscription ID.
The --sku can be Basic, Standard, or Premium depending on your needs.
An Event Hub is the actual entity within a namespace where you send and receive events.
# Create an Event Hub within the namespace
az eventhubs eventhub create --resource-group MyResourceGroup --namespace-name MyEventHubsNamespace --name MyEventHub --partition-count 2 --message-retention-in-days 7
Here:
--partition-count determines the number of parallel streams for processing.--message-retention-in-days specifies how long events are stored.To connect your applications to Event Hubs, you'll need connection strings. These contain authorization credentials.
Get the primary connection string for your namespace:
az eventhubs namespace authorization-rule list --resource-group MyResourceGroup --namespace-name MyEventHubsNamespace --query "[?KeyName=='RootManageSharedAccessKey'].PrimaryConnectionString" -o tsv
RootManageSharedAccessKey grants full access. For production environments, it's recommended to create specific authorization rules with minimal required permissions.
Now that your Event Hub is set up, you can start sending and receiving events. This typically involves using one of the Azure Event Hubs SDKs.
Add the following NuGet package:
dotnet add package Azure.Messaging.EventHubs
Here's a simplified example of sending an event:
using Azure.Messaging.EventHubs;
using System;
using System.Text;
using System.Threading.Tasks;
public class EventSender
{
private const string connectionString = "";
private const string eventHubName = "MyEventHub";
private EventHubProducerClient producer;
public EventSender()
{
producer = new EventHubProducerClient(connectionString, eventHubName);
}
public async Task SendEventAsync(string eventData)
{
using EventData data = new EventData(Encoding.UTF8.GetBytes(eventData));
try
{
await producer.SendAsync(new EventData[] { data });
Console.WriteLine($"Sent event: {eventData}");
}
catch (Exception ex)
{
Console.WriteLine($"Error sending event: {ex.Message}");
}
}
}
And receiving events:
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using System;
using System.Text;
using System.Threading.Tasks;
public class EventReceiver
{
private const string connectionString = "";
private const string eventHubName = "MyEventHub";
private EventHubConsumerClient consumer;
public EventReceiver()
{
consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, eventHubName);
}
public async Task StartReceivingAsync()
{
await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync())
{
string data = Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray());
Console.WriteLine($"Received event: {data} from partition {partitionEvent.Partition.Id}");
}
}
}