【问题标题】:Azure Eventhub Python - OWNERSHIP_LOSTAzure Eventhub Python - OWNERSHIP_LOST
【发布时间】:2020-04-23 10:44:45
【问题描述】:

我已经复制粘贴了这个脚本,它运行良好。我正在从 eventthub 获取所有事件:

import logging
from azure.eventhub import EventHubConsumerClient

connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
consumer_group = '<< CONSUMER GROUP >>'
eventhub_name = '<< NAME OF THE EVENT HUB >>'
client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)

logger = logging.getLogger("azure.eventhub")
logging.basicConfig(level=logging.INFO)

def on_event(partition_context, event):
    logger.info("Received event from partition {}".format(partition_context.partition_id))
    partition_context.update_checkpoint(event)

with client:
    client.receive(
        on_event=on_event, 
        starting_position="-1",  # "-1" is from the beginning of the partition.
    )
    # receive events from specified partition:
    # client.receive(on_event=on_event, partition_id='0')

但是,一旦我尝试从事件中获取任何值,我就会收到 OWNERSHIP_LOST 异常,并且脚本不会检索任何内容。

我试过event.body.KEYevent.KEYjson.loads(event)['body']['key']json.loads(event)['key']。 无论我尝试使用 events 做什么,都会引发 OWNERSHIP_LOST。

您是否知道解决方法或碰巧知道我在这里做错了什么?

【问题讨论】:

  • 检查事件不应引发该异常。您是否从 update_checkpoint(event) 调用中得到异常?

标签: python azure-eventhub


【解决方案1】:

OWNERSHIP_LOST 可能是由接收事件时的错误或 on_event 中的错误引起的。你可以试试 event.body_as_json()['key'] 吗?

【讨论】:

    【解决方案2】:

    EventData.body 返回bytes or Generator[bytes]

    在以您想要的方式提取内容之前,您需要以适当的方式将这些原始字节反序列化为字符串/对象。

    EventData 对象有两个帮助方法 body_as_str 尝试解码返回字符串的原始字节,body_as_json 尝试将原始字节解码为字符串,然后尝试加载返回 json 对象的字符串。

    解析原始接收字节取决于您发送的数据,如果您可以提供示例数据,这将有助于调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 2020-04-26
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多