如何使用Python SDK处理来自EventHub的Json消息?

时间:2018-09-21 06:46:24

标签: python azure azure-eventhub

我正在按照以下示例处理从事件中心收到的消息:

https://github.com/Azure/azure-event-hubs-python/blob/master/examples/eph.py

但是,该代码确实显示了有关如何检索每条消息并到达Json有效负载的任何代码?

我尝试了以下代码(使用适用于Event Hub的Python SDK)

async def process_events_async(self, context, messages):
"""
Called by the processor host when a batch of events has arrived.
This is where the real work of the event processor is done.
:param context: Information about the partition
:type context: ~azure.eventprocessorhost.PartitionContext
:param messages: The events to be processed.
:type messages: list[~azure.eventhub.common.EventData]
"""
    for message in messages:
        message_content = json.loads(message.body)

    logger.info("Events processed {}".format(context.sequence_number))
    await context.checkpoint_async()

但是,我在声明json.loads的语句中收到以下错误消息:

  

“ 2018-09-20 23:13:12,484天蓝色错误事件处理器错误   TypeError(“ JSON对象必须是str,bytes或bytearray,而不是   'generator'“,)”

您能用代码帮助我到达消息的Json有效负载吗?

1 个答案:

答案 0 :(得分:0)

这是用于获取事件中心消息的JSON有效负载的工作代码:

for message in messages:
    message_body = list(message.body)
    message_content = json.loads(message_body[0])