Azure

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

  1. Azure Occasion Hubs Overview.
  2. Creating Azure occasion hub on azure portal.
  3. 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.

  1. Check in to the Azure Portal.
  2. On the portal, click on +New > Web of Issues > Occasion Hubs.

  3. 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”.

    Sending Event Data To Azure Event Hub

  4. 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.

    Sending Event Data To Azure Event Hub

  5. Within the “Create Occasion Hub” blade, enter the identify of the hub and click on the “Create” button.

    Sending Event Data To Azure Event Hub

  6. 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

  1. namespace Sender  
  2. {  
  3.     public interface IWeatherDataSender  
  4.     {  
  5.         Process SendDataAsync(WeatherData information);  
  6.     }  
  7.     public class WeatherDataSender : IWeatherDataSender  
  8.     {  
  9.         non-public EventHubClient _eventHubClient;  
  10.   
  11.         public WeatherDataSender(string eventHubConnectionString)  
  12.         {  
  13.             _eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString);  
  14.         }  
  15.         public async Process SendDataAsync(WeatherData information)  
  16.         {  
  17.             EventData eventData = CreateEventData(information);  
  18.             await _eventHubClient.SendAsync(eventData);  
  19.         }  
  20.   
  21.        non-public static EventData CreateEventData(WeatherData information)  
  22.         {  
  23.             var dataAsJson = JsonConvert.SerializeObject(information);  
  24.             var eventData = new EventData(Encoding.UTF8.GetBytes(dataAsJson));  
  25.             return eventData;  
  26.         }  
  27.     }  
  28. }  

Code for WeatherData.cs

  1. namespace Sender.Fashions  
  2. {  
  3.     public class WeatherData  
  4.     {  
  5.         public int Temperature { getset; }  
  6.         public int WindSpeed { getset; }  
  7.         public Route WindDirection { getset; }  
  8.   
  9.         public enum Route   
  10.         {  
  11.             North, South, East, West   
  12.         }  
  13.   
  14.         public override string ToString()  
  15.         {  
  16.             return ” Windspeed: “ + WindSpeed.ToString() + ” km/h “  
  17.                  + “”;  
  18.         }  
  19.     }  
  20. }  

Now add a category to the principle driver undertaking named SenderHelper.cs.

 

Code for SenderHelper.cs

  1. namespace WeatherApp  
  2. {  
  3.     public class SenderHelper  
  4.     {  
  5.         WeatherDataSender weatherDataSender;  
  6.           
  7.         public SenderHelper(string conn)  
  8.         {  
  9.             weatherDataSender = new WeatherDataSender(conn);  
  10.         }  
  11.           
  12.         public async Process SendDataAsync(WeatherData WeatherData)  
  13.         {  
  14.             strive  
  15.             {  
  16.                 await weatherDataSender.SendDataAsync(WeatherData);  
  17.                 Console.WriteLine($“Despatched information: {WeatherData}”);  
  18.             }  
  19.             catch (Exception ex)  
  20.             {  
  21.                 Console.WriteLine($“Exception: {ex.Message}”);  
  22.             }  
  23.         }  
  24.   
  25.         public async void DataSender()  
  26.         {  
  27.             await SendDataAsync(CreateWeatherData());  
  28.         }  
  29.   
  30.         public WeatherData CreateWeatherData()  
  31.         {  
  32.             return new WeatherData  
  33.             {  
  34.                 Temperature = 35,   
  35.                 WindSpeed = 300,   
  36.                 WindDirection = WeatherData.Route.North  
  37.             };  
  38.         }  
  39.     }  
  40. }  

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

  1. public class Program  
  2.     {  
  3.         public static string ConnectionString = “<your occasion hub connection string>”;  
  4.   
  5.         static void Fundamental(string[] args)  
  6.         {  
  7.             SenderHelper helper = new SenderHelper(ConnectionString);  
  8.               
  9.             for(int i = 0; i < 5; i++)  
  10.             {  
  11.                 helper.DataSender();  
  12.             }  
  13.         }  
  14.     }  

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,

 

Sending Event Data To Azure Event Hub 

 

When you ship occasions these graphs will change and they’re going to seem like this,

 

Sending Event Data To Azure Event Hub 

 

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
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button