Receiving Occasion Information From An Azure Eventhub
Introduction
Highway Map
- Construct a .NET Core Console App and Initialize an EventHubClient
- Create a PartitionReceiver to Obtain Occasions
- 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.
- public class WeatherData
- {
- public int Temperature { get; set; }
- public int WindSpeed { get; set; }
- public Course WindDirection { get; set; }
- public enum Course
- {
- North, South, East, West
- }
- public override string ToString()
- {
- return ” Windspeed: “ + WindSpeed.ToString() + ” km/h “
- + “”;
- }
- }
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.
Add the next code to your Program.cs file in WeatherData.EventHub.Receivers.Direct challenge.
- class Program
- {
- const string EventHubConnectionString = “<your occasion hub connectionstring>”;
- static void Essential(string[] args)
- {
- MainAsync().Wait();
- }
- personal static async Process MainAsync()
- {
- Console.WriteLine(“Connecting to the Occasion Hub…”);
- var EventHubClient =
- EventHubClient.CreateFromConnectionString(EventHubConnectionString);
- var runtimeInformation = await EventHubClient.GetRuntimeInformationAsync();
- var partitionReceivers = runtimeInformation.PartitionIds.Choose(partitionId =>
- EventHubClient.CreateReceiver(“$Default”,
- partitionId, DateTime.Now)).ToList();
- Console.WriteLine(“Ready for incoming occasions…”);
- foreach (var partitionReceiver in partitionReceivers)
- {
- partitionReceiver.SetReceiveHandler(
- new WeatherDataPartitionReceiveHandler(partitionReceiver.PartitionId));
- }
- Console.WriteLine(“Press any key to shutdown”);
- Console.ReadLine();
- await EventHubClient.CloseAsync();
- }
- }
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.

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.
- public class WeatherDataPartitionReceiveHandler : IPartitionReceiveHandler
- {
- public WeatherDataPartitionReceiveHandler(string partitionId)
- {
- PartitionId = partitionId;
- }
- public int MaxBatchSize => 10;
- public string PartitionId { get; }
- public Process ProcessErrorAsync(Exception error)
- {
- Console.WriteLine($“Exception: {error.Message}”);
- return Process.CompletedTask;
- }
- public async Process ProcessEventsAsync(IEnumerable<EventData> eventDatas)
- {
- if (eventDatas != null)
- {
- foreach (var eventData in eventDatas)
- {
- var dataAsJson = Encoding.UTF8.GetString(eventData.Physique.Array);
- var weatherData =
- JsonConvert.DeserializeObject<WeatherData>(dataAsJson);
- Console.WriteLine(weatherData);
- }
- }
- }
- }
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.