【发布时间】: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.KEY、event.KEY、json.loads(event)['body']['key']、json.loads(event)['key']。
无论我尝试使用 events 做什么,都会引发 OWNERSHIP_LOST。
您是否知道解决方法或碰巧知道我在这里做错了什么?
【问题讨论】:
-
检查事件不应引发该异常。您是否从 update_checkpoint(event) 调用中得到异常?
标签: python azure-eventhub