Introduction
In this article we are going to see how we can send data to an Azure event hub and how we can check if the event hub has received our data on the Azure portal. Consider a scenario where we are sending data from a weather station collected via sensors to an event hub for later processing.
Roadmap
- Azure Event Hubs Overview.
- Creating Azure event hub on azure portal.
- Writing code to send data to the even hub.
Overview
Microsoft has provided the Eventhub service on their cloud platform as a big streaming palform and event ingestion service. Eventhubs can receive and process millions of event data per second. Events of event data sent to the eventhubs can be transformed and stored by many real time analytics services or storage adapters.
Creating Azure event hub on Azure portal
Follow these steps to create an Event Hub on the Azure portal.
- Sign in to the Azure Portal.
- On the portal, click +New > Internet of Things > Event Hubs.
- In the "Create Namespace" blade, enter the name of your Event Hub in the name field, then choose the Standard Pricing Tier, and choose the desired subscription to create the Event Hub under it. For Resource group, select the "Create new" option and enter the resource group name as you wish. Select the location that is nearest to you and press "Create".
- After the "deployment succeeded" message on the Notification bar, it opens our namespace to show the details of the Event Hub. Press +Event Hub to add an event hub to the namespace.
- In the "Create Event Hub" blade, enter the name of the hub and click the "Create" button.
- Wait a few minutes. Our Event Hub is created and shown under the "Event Hubs" blade. For connection purposes, click the "Shared access policies" and choose "RootManageSharedAccesskey". It opens the SAS policy that consists of a connection string. Copy and paste the connection string on the clipboard.
Sending data to Event hub via Code
Considering the scenario we created for a weather station, we will be writing a console app, a service that records data and sends it to the event hub. For recording weather we will be using a random function for now that oscillates between a certain temperature and wind speed, and wind direction.
Make a .Net Core Console App.
Once you have made the app you will need to create a Class Library project which you will use as a means of sending the data. While the main project or the driver simply calls functions from that class library.
Add a class Library project named “Sender”.
Create a file by the name of WeatherData.Sender.cs. Create a Folder, name it Models. This folder will contains the class model which we will use to map our data before it is sent. Create a class in the Models folder named WeatherData.
Code for WeatherData.Sender.cs
- namespace Sender
- {
- public interface IWeatherDataSender
- {
- Task SendDataAsync(WeatherData data);
- }
- public class WeatherDataSender : IWeatherDataSender
- {
- private EventHubClient _eventHubClient;
-
- public WeatherDataSender(string eventHubConnectionString)
- {
- _eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString);
- }
- public async Task SendDataAsync(WeatherData data)
- {
- EventData eventData = CreateEventData(data);
- await _eventHubClient.SendAsync(eventData);
- }
-
- private static EventData CreateEventData(WeatherData data)
- {
- var dataAsJson = JsonConvert.SerializeObject(data);
- var eventData = new EventData(Encoding.UTF8.GetBytes(dataAsJson));
- return eventData;
- }
- }
- }
Code for WeatherData.cs
- namespace Sender.Models
- {
- public class WeatherData
- {
- public int Temperature { get; set; }
- public int WindSpeed { get; set; }
- public Direction WindDirection { get; set; }
-
- public enum Direction
- {
- North, South, East, West
- }
-
- public override string ToString()
- {
- return "{ Temp: " + Temperature.ToString() + " *C "
- + "| Windspeed: " + WindSpeed.ToString() + " km/h "
- + "| Wind Direction: " + WindDirection.ToString() + " }";
- }
- }
- }
Now add a class to the main driver project named SenderHelper.cs.
Code for SenderHelper.cs
- namespace WeatherApp
- {
- public class SenderHelper
- {
- WeatherDataSender weatherDataSender;
-
- public SenderHelper(string conn)
- {
- weatherDataSender = new WeatherDataSender(conn);
- }
-
- public async Task SendDataAsync(WeatherData WeatherData)
- {
- try
- {
- await weatherDataSender.SendDataAsync(WeatherData);
- Console.WriteLine($"Sent data: {WeatherData}");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Exception: {ex.Message}");
- }
- }
-
- public async void DataSender()
- {
- await SendDataAsync(CreateWeatherData());
- }
-
- public WeatherData CreateWeatherData()
- {
- return new WeatherData
- {
- Temperature = 35,
- WindSpeed = 300,
- WindDirection = WeatherData.Direction.North
- };
- }
- }
- }
This class will be used as controller in the Main function to send data to the Event Hub.
Add the following code to your Program.cs
- public class Program
- {
- public static string ConnectionString = "<your event hub connection string>";
-
- static void Main(string[] args)
- {
- SenderHelper helper = new SenderHelper(ConnectionString);
-
- for(int i = 0; i < 5; i++)
- {
- helper.DataSender();
- }
- }
- }
This will send 5 events to the event hub. Now before the messages are sent to the eventhub your eventhub on the azure portal will look something like this,
Once you send events these graphs will change and they will look like this,
You have successfully sent data to your eventhub.
Use cases
The following scenarios are some of the scenarios where you can use Event Hubs,
- Anomaly detection (fraud/outliers)
- Application logging
- Analytics pipelines, such as clickstreams
- Live dashboarding
- Archiving data
- Transaction processing
- User telemetry processing
- Device telemetry streaming