如何从eventhub发送消息到另一个eventhub?

时间:2016-10-17 18:01:24

标签: c# azure azure-eventhub

我想发送eventhub客户,然后将其下载到样本数据(如天气)并发送另一个eventhub。我的代码无法正常工作。没有错误,但数据不会发送到数据库。

public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
        {
                try
            {
                foreach (EventData message in messages)
                {
                        string data = Encoding.UTF8.GetString(message.GetBytes());
                        NewClient Client = JsonConvert.DeserializeObject<NewClient>(data);
                    if (Client.City != null && Client.Street != null )
                    {

                        GoogleGeoApi GeoClient = new GoogleGeoApi();
                        GeoClient.SetCoordinates(Client.City, Client.Street);
                        WeatherApi WeatherApiobject = new WeatherApi();
                        WeatherApiobject.GetJson(GeoClient.convertlat, GeoClient.convertlng);
                        string weatherdata = WeatherApiobject.sendEvent;
                        SenderEvent NewSenderEvent = new SenderEvent();
                        NewSenderEvent.DataSender(weatherdata, ConstFile.WeatherEventHubName);
                        //StartH(ConstFile.WeatherEventHubName).Wait();
                    }

                    Interlocked.Increment(ref this.totalMessages);
                    this.LastMessageOffset = message.Offset;
                }

                if (this.IsClosed)
                {
                    this.IsReceivedMessageAfterClose = true;
                }

                if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(1))
                {
                    lock (this)
                    {
                        this.checkpointStopWatch.Reset();
                        return context.CheckpointAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} > Event Hub Exception: {1}", DateTime.Now, ex.Message);
            }

            return Task.FromResult<object>(null);
        }

我将添加我的接收器eventhub看起来像这样: https://code.msdn.microsoft.com/Service-Bus-Event-Hub-45f43fc3/view/SourceCode#content

4 个答案:

答案 0 :(得分:0)

   foreach($users as $user) {
      $user->user_id;
      //use the alias as used in the select chained function 
    } 

答案 1 :(得分:0)

您可以使用Stream Analytics轻松完成此操作!您的第一个事件中心是Stream Analytics中的input。然后你可以对流写一个query(从[Input1]中选择* ...这将为你提供一切)。您可以output将流回送到另一个事件中心。

答案 2 :(得分:0)

private static async Task StartHost(string eventHubName)
        {
            string eventProcessorHostName = "1";
            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", ConstFile.storageAccountName, ConstFile.storageAccountKey);
            host = new EventProcessorHost(
                eventProcessorHostName,
                eventHubName,
                ConstFile.ConsumerGroup,
                ConstFile.eventHubConnectionString,
                storageConnectionString, eventHubName.ToLowerInvariant());

            factory = new DemoEventProcessorFactory(eventProcessorHostName);

            try
            {
                var options = new EventProcessorOptions();
                options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
                await host.RegisterEventProcessorFactoryAsync(factory);
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0} > Exception: {1}", DateTime.Now.ToString(), exception.Message);
                Console.ResetColor();
            }
        }
    }
}

答案 3 :(得分:0)

取决于:

  1. 如果您的事件中心是低延迟 - 您可以使用Event Hub触发器(azure 每当事件中心收到新内容时将运行的函数 事件)。在您的天蓝色功能中,您可以使用the example
  2. 中的代码
  3. 如果你有很多活动 - 你可以试试azure stream     分析过滤掉您的天气数据并将其发送给其他人     活动中心。你建立一个像这样的管道:

    EventHub1 - &gt; AzureStreamAnalytics - &gt; EventHub2