简体   繁体   English

Azure 功能 Python | 将带有属性的 EventData 消息发送到事件中心 output

[英]Azure Functions Python | Send EventData messages with properties to Event Hub output

I am writing an Azure python function that is triggered and then generates multiple outgoing messages.我正在写一个 Azure python function 被触发然后生成多个传出消息。 I need to send EventData (body + properties) messages to an Eventhub.我需要将 EventData (body + properties) 消息发送到 Eventhub。 Thus far I have not found any way to do add properties to an outgoing message using the EventHub output bindings.到目前为止,我还没有找到使用 EventHub output 绑定向传出消息添加属性的任何方法。 It appears that the output string is put into the "body" property.似乎 output 字符串已放入“body”属性中。

One possible solution that I see is to write an EventHubClient into the function, but is that really the most effective way to get properties send with the message?我看到的一种可能的解决方案是将 EventHubClient 写入 function,但这真的是让属性随消息一起发送的最有效方法吗? Why would there be output bindings then?那么为什么会有 output 绑定呢?

My function.json file is:我的 function.json 文件是:

    {
      "type": "eventHub",
      "name": "outputHub",
      "eventHubName": "test",
      "connection": "TestSendConnection",
      "direction": "out"
}

Here is my code:这是我的代码:

def main(events: func.EventHubEvent,
         referenceInput: func.InputStream,
         outputHub: func.Out[str]):
    logging.info('Send an output event to eventhub')

    evt_data_list = []
    for k in range(0,10):
        evt_data = EventData("Sample Body")
        evt_data.properties['EventType'] = "log"
        evt_data_list.append(evt_data)

    logging.info('Send an output event to eventhub')
    import random
    outputHub.set("[" + ",".join([str(evt) for evt in evt_data_list]) + "]")

I am monitoring the incoming messages with the Azure Event Hub Explorer and I receive multiple messages, but they arrive in the following format.我正在使用 Azure 事件中心资源管理器监视传入的消息,我收到多条消息,但它们以以下格式到达。 I need the body and the properties sections to be separate for the external parser.对于外部解析器,我需要将正文和属性部分分开。

{
  "body": {
    "body": "Sample Body",
    "properties": {
      "EventType": "log"
    }
  },
  "enqueuedTimeUtc": "2020-06-09T17:59:04.803Z",
  "offset": "1335734859528",
  "sequenceNumber": 4995022
}

Currently I am afraid there is no way to add properties to an outgoing message using the EventHub output bindings.目前恐怕无法使用 EventHub output 绑定向传出消息添加属性。

The workaround is using EventHub SDK inside the function.解决方法是在 function 中使用 EventHub SDK。

Reference:参考:

Microsoft Azure SDK for Event Hubs(Python) Microsoft Azure SDK 用于事件中心 (Python)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM