You are currently viewing Azure Event Hub and Capture Demo

Azure Event Hub and Capture Demo

In last article, we have seen some key concepts about the Azure event hub. In this article, let’s have a look at how to create event hub and write a simple .net application to interact with event hub.


In this article, we will create an event hub instance. Then we will create a .Net console application which will send real-time events to the event hub. We will enable capture while creating event hub.

We will also try to create a sample sender/receiver applications which interact with event hub. The complete source code is available at my GitHub repository.

For following steps in this article, you will need access to Azure Portal. If you don’t have an Azure subscription, create a free account before you begin.

Create Event Hub

Every event hub is part of one event hub namespace. In this section, we will see how to create event hub namespace and an event hub inside it.

Create Event Hub Namespace

Search for “event hub” under “All services”. You should be able to see “Event Hubs” option in the results. Click on that.

Then you will see Event Hubs blade which shows all event hub namespaces available under your subscription. After this you can click on “+ Add” button at the top to open Create namespace panel.

On create namespace panel you have to provide a unique name for your event hub namespace. You also need to provide the pricing tier, subscription, resource group and location where this resource should be created.

There are few check boxes on this panel:

  • Enable Kafka – is for applications which were earlier talking to Apache Kafka. If this option is enabled, those applications can talk to event hub without any major modifications.
  • Make this namespace zone redundant – as it suggests, it will create multiple instances of your event hub namespace in multiple Azure zones.

Throughput units is the pre-purchased unit of capacity of your event hub namespace.

As per MSDN article, a single throughput lets you:

  • Ingress: Up to 1 MB per second or 1000 events per second (whichever comes first).
  • Egress: Up to 2 MB per second or 4096 events per second.

Depending on your usage, you can decide the throughput units required for your application.

The Auto-inflate feature of Event Hubs automatically scales up by increasing the number of throughput units, to meet usage needs.  If you check this check box, another input control will be shown to select maximum number of throughput units allowed in this namespace.

Once all these inputs are provided, click on create button. Creation of namespace will take few seconds.

Create Event Hub

Once the namespace is created, you can open the namespace and then click on “+ Add Event Hub” button highlighted in below snapshot. It will open create event hub panel.

On create event hub, you need to provide name to your event hub.

Event Hubs provides message streaming through a partitioned consumer pattern in which each consumer only reads a specific subset, or partition, of the message stream.  A partition is an ordered sequence of events that is held in an event hub. Generally, there should be 1:1 mapping from throughput unit to partition count.

You can also configure number of days for which the message should be retained in event hub.

Event Hubs Capture enables you to automatically capture the streaming data in Event Hubs and save it to your choice of either a Blob storage account, or an Azure Data Lake Service account. I have kept it on as you can see in below snapshot.

Once it is set On, you can select the Azure Data Lake Service or Storage Service account / container by clicking on select button. I already have a container with name “democontainer” which is a block container inside a demo storage account. I have selected that for the demo purpose. Rest of the inputs are kept as defaults.

When capture is enabled, you can specify the time / max. size after which the data from event hub should be written to selected storage. I have kept it 5 min and 300 MB. Then you can click on create button.

Sender – .Net Console Application

You can create a .Net core console application in Visual Studio. Then install “Microsoft.Azure.EventHubs” nuget package in the project.

The sender application creates the EventHubClient instance from the connection string.

Then it uses SendAsync method to send data to event hub. Once all messages are sent, the connection can be closed by calling CloseAsync method.

var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
                 EntityPath = EventHubName

EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

var message = $"Message {i}";
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));

await eventHubClient.CloseAsync();

You can download the complete source code from this GitHub repository. After download you will have to change couple of variables:

private const string EventHubConnectionString = "{primary-connection-string-of-namespace}"

private const string EventHubName = "{event-hub-name}";

Viewing Captured Events

You can run the message sender console app and then open the Storage Explorer.

In storage explorer, you can find the storage account and container under your subscription. After you click on container, you should be able to see the event hub namespace data.

Please note that every message is written in .avro format.

In this article, we have seen how to create event hub and how to send messages to event hub. The messages from event hub can further be written to SQL database or they can be used in PowerBI using Stream Analytics. Please do comment and let me know your thoughts and opinions.

Leave a ReplyCancel reply