简体   繁体   中英

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. 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.

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?

My function.json file is:

    {
      "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. 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.

The workaround is using EventHub SDK inside the function.

Reference:

Microsoft Azure SDK for Event Hubs(Python)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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