Azure

Receiving Occasion Information From An Azure Eventhub

Introduction

 

This text is a continuation of my Sending Occasion Information To Azure Occasion Hub article. Right here we’re going to be constructing an utility that receives knowledge from an Azure EventHub in actual time. The situation we beforehand constructed was that we’re sending knowledge from a climate station collected through sensors to an occasion hub. Now we’re on the finish the place knowledge will likely be despatched to from the EventHub. Right here after we obtain the info we are able to do something with it to easily simply storing it in an excel file or constructing complicated graphs and experiences utilizing that knowledge. Let’s get began.

 

Highway Map

  1. Construct a .NET Core Console App and Initialize an EventHubClient
  2. Create a PartitionReceiver to Obtain Occasions
  3. Transformation of information.

Construct a .Internet Core Console App and initialize an EventHubClient

 

Begin off by making a .internet core console utility. After that embody the next nuget bundle to your challenge: Microsoft.Azure.EventHubs. This would be the library we’ll use to work together with the azure EventHub.

 

Begin of by including two class libraries to your challenge. One library will likely be storing our fashions. Within the different, we will likely be writing all our logic which handles the receiving of the info from the EventHub. We’ll additionally embody the recordsdata as named within the following picture.

 

 

Add your mannequin class to the WeatherData.cs file.

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

Create a PartitionReceiver to Obtain Occasions

 

So as a way to obtain occasions we now have to write down a ParitionReceiver. An occasion hub can include greater than 1 partition that acts as a queue the place occasion knowledge is saved. The EventHub manages by itself to which partition it desires to ship the newly acquired knowledge. It makes use of a spherical robin technique of distributing the messages.

 

Thus when receiving knowledge from an occasion hub we now have to subscribe to every partition in order that we are able to obtain all the info that’s being despatched to the EventHub and never simply elements of it that’s being despatched solely to the partition that we’d have subscribed to.

 

Moreover, an EventHub consists of client teams. A Shopper Group is form of like a gateway into the EventHub’s partitions. The app into which it’s a must to obtain the info from the EventHub firstly must have a client group related to it via which the app will likely be contacting the EventHub. For extra particulars on client teams attempt this.

 

Add the next code to your Program.cs file in WeatherData.EventHub.Receivers.Direct challenge.

  1. class Program  
  2.   {  
  3.     const string EventHubConnectionString = “<your occasion hub connectionstring>”;     
  4.     static void Essential(string[] args)  
  5.     {  
  6.       MainAsync().Wait();  
  7.     }  
  8.     personal static async Process MainAsync()  
  9.     {  
  10.       Console.WriteLine(“Connecting to the Occasion Hub…”);  
  11.       var EventHubClient =  
  12.         EventHubClient.CreateFromConnectionString(EventHubConnectionString);  
  13.       var runtimeInformation = await EventHubClient.GetRuntimeInformationAsync();  
  14.       var partitionReceivers = runtimeInformation.PartitionIds.Choose(partitionId =>  
  15.           EventHubClient.CreateReceiver(“$Default”,  
  16.           partitionId, DateTime.Now)).ToList();  
  17.       Console.WriteLine(“Ready for incoming occasions…”);  
  18.       foreach (var partitionReceiver in partitionReceivers)  
  19.       {  
  20.         partitionReceiver.SetReceiveHandler(  
  21.           new WeatherDataPartitionReceiveHandler(partitionReceiver.PartitionId));  
  22.       }  
  23.       Console.WriteLine(“Press any key to shutdown”);  
  24.       Console.ReadLine();  
  25.       await EventHubClient.CloseAsync();  
  26.     }  
  27.   }  

Now on this code, the road that comprises “$Default” is the road the place we’re utilizing the default client group that our EventHub comprises and thru that client group we’re getting all of the partitions of our occasion hub and storing their ids in a listing.

 

You will discover your present client teams and create new ones within the client teams choice when you go into the choices for you occasion hub on the Azure portal.

 

Receiving Event Data From An Azure Eventhub

 

Merely add a brand new client group and put the identify of that client group instead of $Default in your code. This additionally supplies some perception on the parallelism of EventHubs. You may have a number of purposes listening in on the identical EventHub via totally different client teams and do totally different duties with that knowledge all on the identical time.

 

Transformation of Information

 

Add the next code into your WeatherDataPartitionReceiveHandler.cs file.

  1. public class WeatherDataPartitionReceiveHandler : IPartitionReceiveHandler  
  2.   {  
  3.     public WeatherDataPartitionReceiveHandler(string partitionId)  
  4.     {  
  5.       PartitionId = partitionId;  
  6.     }  
  7.     public int MaxBatchSize => 10;  
  8.     public string PartitionId { get; }  
  9.     public Process ProcessErrorAsync(Exception error)  
  10.     {  
  11.       Console.WriteLine($“Exception: {error.Message}”);  
  12.       return Process.CompletedTask;  
  13.     }  
  14.     public async Process ProcessEventsAsync(IEnumerable<EventData> eventDatas)  
  15.     {  
  16.       if (eventDatas != null)  
  17.       {  
  18.         foreach (var eventData in eventDatas)  
  19.         {  
  20.           var dataAsJson = Encoding.UTF8.GetString(eventData.Physique.Array);  
  21.           var weatherData =  
  22.             JsonConvert.DeserializeObject<WeatherData>(dataAsJson);  
  23.           Console.WriteLine(weatherData);                
  24.         }  
  25.       }  
  26.     }  
  27.   }  

This logic is liable for really processing the acquired knowledge, reworking it after which presenting it on the console. Thus, via this course of we now have completed studying knowledge from an EventHub.

 

Now since this app will likely be asynchronously listening on the EventHub for any new messages within the partitions you possibly can depart it working and ship messages to your occasion hub.

 

This utility will obtain messages from the EventHub as quickly because the EventHub receives the messages.

 

Abstract

 

On this article we checked out what are partitions, client teams and the way an app we created can use these two issues to obtain knowledge from an Azure occasion hub.

Tags
Show More

Related Articles

Leave a Reply

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

Close