Azure 函数 Python |将带有属性的 EventData 消息发送到事件中心输出

2022-01-17 00:00:00 python azure-functions azure-eventhub

问题描述

我正在编写一个 Azure python 函数,该函数被触发,然后生成多个传出消息.我需要将 EventData (body + properties) 消息发送到 Eventhub.到目前为止,我还没有找到任何方法来使用 EventHub 输出绑定向传出消息添加属性.似乎输出字符串被放入body"属性中.

I am writing an Azure python function that is triggered and then generates multiple outgoing messages. I need to send EventData (body + properties) messages to an Eventhub. Thus far I have not found any way to do add properties to an outgoing message using the EventHub output bindings. It appears that the output string is put into the "body" property.

我看到的一个可能的解决方案是将 EventHubClient 写入函数中,但这真的是让属性随消息一起发送的最有效方法吗?那为什么会有输出绑定呢?

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? Why would there be output bindings then?

我的function.json文件是:

My function.json file is:

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

这是我的代码:

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]) + "]")

我正在使用 Azure 事件中心资源管理器监视传入的消息,我收到了多条消息,但它们以以下格式到达.对于外部解析器,我需要将正文部分和属性部分分开.

I am monitoring the incoming messages with the Azure Event Hub Explorer and I receive multiple messages, but they arrive in the following format. 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
}


解决方案

目前恐怕没有办法使用 EventHub 输出绑定为传出消息添加属性.

Currently I am afraid there is no way to add properties to an outgoing message using the EventHub output bindings.

解决方法是在函数内部使用 EventHub SDK.

The workaround is using EventHub SDK inside the function.

参考:

Microsoft Azure SDK for Event Hubs(Python)

相关文章