将JSON数据路由到Azure中的事件中心

时间:2018-05-15 14:05:03

标签: c# .net azure azure-eventhub azure-timeseries-insights

我有一种情况需要通过事件中心将JSON数据(一个JSON文件,而不是转换为JSON)发送到Time Series Insights。但由于我缺乏C#的经验,我无法发送数据。

我能够发送其他示例消息但不能发送JSON。我怎么能这样做?

任何帮助或见解都将不胜感激。

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);

        }
    }
}

但它在异步方法中抛出错误。

严重级代码描述项目文件行抑制状态 错误CS0120非静态字段,方法或属性需要对象引用' EventHubClient.SendAsync(EventData)' ConsoleApp5 C:\ Users \ Shyam \ source \ repos \ ConsoleApp5 \ ConsoleApp5 \ Program.cs 21 Active

更新:

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();
        }
    }
}

3 个答案:

答案 0 :(得分:3)

我确定您现在已经知道了这一点,但您遇到的问题不在于JSON,而在于您如何使用事件中心客户端。

而不是这一行:

await EventHubClient.SendAsync(eventData);

它应该是这样的:

await client.SendAsync(eventData);

答案 1 :(得分:2)

将您的事件包装到JSON数组中:

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);
}

参考:docs.microsoft.com/time-series-insights-send-events

答案 2 :(得分:1)

JSON只是Event Hubs的一个字符串,就像

一样简单
HttpException