Sending Occasion Information To Azure Occasion Hub
Introduction
On this article we’re going to see how we will ship information to an Azure occasion hub and the way we will verify if the occasion hub has obtained our information on the Azure portal. Contemplate a situation the place we’re sending information from a climate station collected through sensors to an occasion hub for later processing.
Roadmap
- Azure Occasion Hubs Overview.
- Creating Azure occasion hub on azure portal.
- Writing code to ship information to the even hub.
Overview
Microsoft has offered the Eventhub service on their cloud platform as a giant streaming palform and occasion ingestion service. Eventhubs can obtain and course of hundreds of thousands of occasion information per second. Occasions of occasion information despatched to the eventhubs will be reworked and saved by many actual time analytics providers or storage adapters.
Creating Azure occasion hub on Azure portal
Comply with these steps to create an Occasion Hub on the Azure portal.
- Check in to the Azure Portal.
- On the portal, click on +New > Web of Issues > Occasion Hubs.
- Within the “Create Namespace” blade, enter the identify of your Occasion Hub within the identify subject, then select the Normal Pricing Tier, and select the specified subscription to create the Occasion Hub below it. For Useful resource group, choose the “Create new” choice and enter the useful resource group identify as you would like. Choose the situation that’s nearest to you and press “Create”.
- After the “deployment succeeded” message on the Notification bar, it opens our namespace to point out the main points of the Occasion Hub. Press +Occasion Hub so as to add an occasion hub to the namespace.
- Within the “Create Occasion Hub” blade, enter the identify of the hub and click on the “Create” button.
- Wait a couple of minutes. Our Occasion Hub is created and proven below the “Occasion Hubs” blade. For connection functions, click on the “Shared entry insurance policies” and select “RootManageSharedAccesskey”. It opens the SAS coverage that consists of a connection string. Copy and paste the connection string on the clipboard.
Sending information to Occasion hub through Code
Contemplating the situation we created for a climate station, we might be writing a console app, a service that information information and sends it to the occasion hub. For recording climate we might be utilizing a random operate for now that oscillates between a sure temperature and wind pace, and wind path.
Make a .Internet Core Console App.
After you have made the app you have to to create a Class Library undertaking which you’ll use as a method of sending the information. Whereas the principle undertaking or the motive force merely calls features from that class library.
Add a category Library undertaking named “Sender”.
Create a file by the identify of WeatherData.Sender.cs. Create a Folder, identify it Fashions. This folder will incorporates the category mannequin which we’ll use to map our information earlier than it’s despatched. Create a category within the Fashions folder named WeatherData.
Code for WeatherData.Sender.cs
- namespace Sender
- {
- public interface IWeatherDataSender
- {
- Process SendDataAsync(WeatherData information);
- }
- public class WeatherDataSender : IWeatherDataSender
- {
- non-public EventHubClient _eventHubClient;
- public WeatherDataSender(string eventHubConnectionString)
- {
- _eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString);
- }
- public async Process SendDataAsync(WeatherData information)
- {
- EventData eventData = CreateEventData(information);
- await _eventHubClient.SendAsync(eventData);
- }
- non-public static EventData CreateEventData(WeatherData information)
- {
- var dataAsJson = JsonConvert.SerializeObject(information);
- var eventData = new EventData(Encoding.UTF8.GetBytes(dataAsJson));
- return eventData;
- }
- }
- }
Code for WeatherData.cs
- namespace Sender.Fashions
- {
- public class WeatherData
- {
- public int Temperature { get; set; }
- public int WindSpeed { get; set; }
- public Route WindDirection { get; set; }
- public enum Route
- {
- North, South, East, West
- }
- public override string ToString()
- {
- return ” Windspeed: “ + WindSpeed.ToString() + ” km/h “
- + “”;
- }
- }
- }
Now add a category to the principle driver undertaking named SenderHelper.cs.
Code for SenderHelper.cs
- namespace WeatherApp
- {
- public class SenderHelper
- {
- WeatherDataSender weatherDataSender;
- public SenderHelper(string conn)
- {
- weatherDataSender = new WeatherDataSender(conn);
- }
- public async Process SendDataAsync(WeatherData WeatherData)
- {
- strive
- {
- await weatherDataSender.SendDataAsync(WeatherData);
- Console.WriteLine($“Despatched information: {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.Route.North
- };
- }
- }
- }
This class might be used as controller within the Fundamental operate to ship information to the Occasion Hub.
Add the next code to your Program.cs
- public class Program
- {
- public static string ConnectionString = “<your occasion hub connection string>”;
- static void Fundamental(string[] args)
- {
- SenderHelper helper = new SenderHelper(ConnectionString);
- for(int i = 0; i < 5; i++)
- {
- helper.DataSender();
- }
- }
- }
This may ship 5 occasions to the occasion hub. Now earlier than the messages are despatched to the eventhub your eventhub on the azure portal will look one thing like this,
When you ship occasions these graphs will change and they’re going to seem like this,
You might have efficiently despatched information to your eventhub.
Use instances
The next situations are among the situations the place you should use Occasion Hubs,
- Anomaly detection (fraud/outliers)
- Software logging
- Analytics pipelines, corresponding to clickstreams
- Reside dashboarding
- Archiving information
- Transaction processing
- Consumer telemetry processing
- Machine telemetry streaming