使用 python 处理事件中心数据
Processing event hub data using python
我正在使用 azure 事件中心 python SDK 在此 link.https://github.com/Azure/azure-event-hubs-python/tree/develop 之后向事件中心发送消息和从事件中心接收消息。我可以成功发送和接收消息。但是我如何解析消息并从事件数据对象中检索数据。请在下面找到代码。
import os
import sys
#import logging
from azure.eventhub import EventHubClient, Receiver, Offset
ADDRESS = 'sb://####.servicebus.windows.net/#####'
USER = '##########'
KEY = '##################################'
CONSUMER_GROUP = "$default"
OFFSET = Offset("-1")
PARTITION = "1"
total = 0
last_sn = -1
last_offset = "-1"
try:
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=5000,
offset=OFFSET)
client.run()
try:
batched_events = receiver.receive(timeout=20)
except:
raise
finally:
client.stop()
for event_data in batched_events:
last_offset = event_data.offset.value
last_sn = event_data.sequence_number
total += 1
print("Partition {}, Received {}, sn={} offset={}".format(
PARTITION,
total,
last_sn,
last_offset))
except KeyboardInterrupt:
pass
如果我尝试查看收到的 event_data,我会看到以下消息。
event_data
<azure.eventhub.common.EventData at 0xd4f1358>
event_data.message
<uamqp.message.Message at 0xd4f1240>
以上关于如何解析此消息以提取数据的任何帮助
从 1.1.0
开始,有新的实用方法可以提取消息的实际数据:
那么,过去是什么
import json
event_obj = json.loads(next(event_data.body).decode('UTF-8'))
现在是:
event_obj = event_data.body_as_json()
对于使用事件中心版本 5.2.0 的用户——截至今天 (GitHub, Reference Docs) 的最新版本,它与 1.1.0 版本相同,即使用 body_as_str()
或 body_as_json()
.但客户端已更改——新版本中有一个 EventHubProducerClient
和一个 EventHubConsumerClient
。要打印收到的事件正文:
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
)
def on_event_batch(partition_context, events):
partition_context.update_checkpoint()
for e in events:
print(e.body_as_str())
with client:
client.receive_batch(
on_event_batch=on_event_batch,
starting_position="-1", # "-1" is from the beginning of the partition.
)
我正在使用 azure 事件中心 python SDK 在此 link.https://github.com/Azure/azure-event-hubs-python/tree/develop 之后向事件中心发送消息和从事件中心接收消息。我可以成功发送和接收消息。但是我如何解析消息并从事件数据对象中检索数据。请在下面找到代码。
import os
import sys
#import logging
from azure.eventhub import EventHubClient, Receiver, Offset
ADDRESS = 'sb://####.servicebus.windows.net/#####'
USER = '##########'
KEY = '##################################'
CONSUMER_GROUP = "$default"
OFFSET = Offset("-1")
PARTITION = "1"
total = 0
last_sn = -1
last_offset = "-1"
try:
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=5000,
offset=OFFSET)
client.run()
try:
batched_events = receiver.receive(timeout=20)
except:
raise
finally:
client.stop()
for event_data in batched_events:
last_offset = event_data.offset.value
last_sn = event_data.sequence_number
total += 1
print("Partition {}, Received {}, sn={} offset={}".format(
PARTITION,
total,
last_sn,
last_offset))
except KeyboardInterrupt:
pass
如果我尝试查看收到的 event_data,我会看到以下消息。
event_data
<azure.eventhub.common.EventData at 0xd4f1358>
event_data.message
<uamqp.message.Message at 0xd4f1240>
以上关于如何解析此消息以提取数据的任何帮助
从 1.1.0
开始,有新的实用方法可以提取消息的实际数据:
那么,过去是什么
import json
event_obj = json.loads(next(event_data.body).decode('UTF-8'))
现在是:
event_obj = event_data.body_as_json()
对于使用事件中心版本 5.2.0 的用户——截至今天 (GitHub, Reference Docs) 的最新版本,它与 1.1.0 版本相同,即使用 body_as_str()
或 body_as_json()
.但客户端已更改——新版本中有一个 EventHubProducerClient
和一个 EventHubConsumerClient
。要打印收到的事件正文:
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
)
def on_event_batch(partition_context, events):
partition_context.update_checkpoint()
for e in events:
print(e.body_as_str())
with client:
client.receive_batch(
on_event_batch=on_event_batch,
starting_position="-1", # "-1" is from the beginning of the partition.
)