Search code examples
c#.netazureazure-eventhubazure-timeseries-insights

Routing JSON data to Event Hubs in Azure


I have a situation where I need to send JSON data (a JSON file, not convert to JSON) to Time Series Insights via Event Hubs. But I am not able to send the data due to my lack of experience in C#.

I am able to send other sample messages but not JSON. How can I do that?

Any help or insight would be appreciated.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using Microsoft.ServiceBus.Messaging;

namespace ConsoleApp5
{
    class Program
    {
        static string _connectionString = "Endpoint..;

        static async Task MainAsync(string[] args)
        {
            var client = EventHubClient.CreateFromConnectionString(_connectionString, "eventhub");
            var json = File.ReadAllText(@"C:\Users\Shyam\Downloads\personal.json");
            var eventData = new EventData(Encoding.UTF8.GetBytes(json));
            await EventHubClient.SendAsync(eventData);

        }
    }
}

It throws an error in the async method though.

Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'EventHubClient.SendAsync(EventData)' ConsoleApp5 C:\Users\Shyam\source\repos\ConsoleApp5\ConsoleApp5\Program.cs 21 Active

UPDATE:

namespace jsonData
{
    using System;
    using System.Text;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.Azure.EventHubs;

    public class Program
    {
        private static EventHubClient eventHubClient;
        private const string EhConnectionString = "Endpoint=sb://";
        private const string EhEntityPath = "hub";

        public static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
        }

        private static async Task MainAsync(string[] args)
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but this simple scenario
            // uses the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
            {
                EntityPath = EhEntityPath
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            var json = File.ReadAllText(@"D:\Sample.json");
            var eventData = new EventData(Encoding.UTF8.GetBytes(json));
            await eventHubClient.SendAsync(eventData);

            await eventHubClient.CloseAsync();


            Console.WriteLine("Press ENTER to exit.");
            Console.ReadLine();
        }
    }
}

Solution

  • Wrap your events into a JSON array:

    using (var ms = new MemoryStream())
    using (var sw = new StreamWriter(ms))
    {
        // Wrap events into JSON array:
        sw.Write("[");
        for (int i = 0; i < events.Count; ++i)
        {
            if (i > 0)
            {
                sw.Write(',');
            }
            sw.Write(events[i]);
        }
        sw.Write("]");
    
        sw.Flush();
        ms.Position = 0;
    
        // Send JSON to event hub.
        EventData eventData = new EventData(ms);
        eventHubClient.Send(eventData);
    }
    

    Reference: learn.microsoft.com/time-series-insights-send-events