The topic of our discussion today in this article is Azure Event Hub. Now we know that for any business application to run successfully, the communication between the processes is elementary. And that is why choosing the wise medium of communication becomes important. So here incase of Azure event hub, the medium of communication is event. Event is a report or an indicator that indicates any task or actions which are the part of that particular project. Azure Event Hub is a data streaming platform that provides an event ingestion service. It is efficient enough to process the millions of events per second with high reliability & low latency. It is referred to as an ingress event handler and is totally maintained by the Microsoft. Azure event hub has an inbuilt Kafka to handle the events in the form of packets. This is the reason why it can be thought of as collaboration between event producers and event consumers.
Use Cases
1. Log analysis
2. Transaction processing
3. Device telemetry streaming
Steps to create the Event Hub
1. If you have your Azure account ready, the first thing you need to do is to create the namespace for the Event hub. Azure event hub namespace is a unique container in which you can create one or more event hubs. It acts like a box in which we can store the group of namespaces.
2. While creating the namespace, make sure that the EnableKafka checkbox is checked. Doing this will facilitate the use of Apache Kafka message protocol and API to communicate with Kafka through the event hub without changing the protocol.
3. The next step will be to create the actual event hub inside the event hub namespace panel you just created above. Click on the EventHub link under the entity tag.
4. Provide the Event hub name, partition count, and message retention as per the requirement.
5. Then, click on the create button to create the event hub.
Test the Event Hub flow using the .Net message producer program.
1. Create a new console application project in .net core.
2. Use the below C# code to send the events on the event hub.
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;private const string connectionString = “<EVENT HUBS NAMESPACE — CONNECTION STRING>”;
private const string eventHubName = “<EVENT HUB NAME>”;
static async Task Main()
{
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(“First event”)));
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(“Second event”)));
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(“Third event”)));
await producerClient.SendAsync(eventBatch);
Console.WriteLine(“A batch of 3 events has been published.”);
}
}
3. You will get the CONNECTION STRING information through shared access policy tab.
4. Run the program and wait for the output.
5. Once the console window gets closed, open the Azure Event Hub Dashboard.
6. Through the Monitoring section of the dashboard you can check number of messages received by the EventHub. Sometimes it might take a while to reflect that number on board. You can refresh the Metric section using refresh button available on the top tabs.
Bulk testing using Rest API Method. (Postman Service)
Prerequisites:-
1. Azure Event Hub Namespace and Azure Event Hub
2. Postman Application
Step to setup the Postman:-
Let us have a glimpse at the thorough steps to setup the Postman
1. Create the URL
To create the URL, you require the below parameters from Event Hub.
a. EventHub Hostname
b. Event Hub Name
c. Messages
Format of the URL is
https://<eventhub namespace>.servicebus.windows.net/<EventHub Name>/messages
Format of the service bus URL
https://<service namespace>.servicebus.windows.net/<topic name or queue>/messages
2. To Send the Event on EventHub we have to use the POST method.
3. Create the required headers
a. Content-Type :- application/xml
b. Authorization :- SAS key
c. Hostname :- Azure event hub URL
4. Steps to create the SAS token using Powershell
a. To create the SAS token refer to the below code
[Reflection.Assembly]::LoadWithPartialName(“System.Web”)| out-null$URI=” “$Access_Policy_Name=” “$Access_Policy_Key=” “$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+3000$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ “`n” + [string]$Expires$HMAC = New-Object System.Security.Cryptography.HMACSHA256$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))$Signature = [Convert]::ToBase64String($Signature)$SASToken = “SharedAccessSignature sr=” + [System.Web.HttpUtility]::UrlEncode($URI) + “&sig=” + [System.Web.HttpUtility]::UrlEncode($Signature) + “&se=” + $Expires + “&skn=” + $Access_Policy_Name
$SASToken
b. To use this code you require three parameters
- URI — which we mention in the Postman URL Eg.
<eventhub namespace>.servicebus.windows.net/<EventHub Name>
2. Access_Policy_Name: Name which comes under the shared access Tab, mostly its “RootManageSharedAccessKey”.
3. Access_Policy_Key : You have to use the primary key from the shared access key tab.
Click on the Run button from the Postman application to test the connection.
If you receive the code 201, that means the test is successful.
Yay!! Now you are all set to use Azure Event Hub for your business applications. Good luck!