Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python处理事件中心数据_Python_Azure_Azure Eventhub_Azure Eventhub Capture - Fatal编程技术网

使用python处理事件中心数据

使用python处理事件中心数据,python,azure,azure-eventhub,azure-eventhub-capture,Python,Azure,Azure Eventhub,Azure Eventhub Capture,我正在使用azure event hub python SDK通过此链接向event hub发送消息,并从event hub接收消息。。我可以成功发送和接收消息。但是如何解析消息并从事件数据对象检索数据呢。请在下面查找代码 import os import sys #import logging from azure.eventhub import EventHubClient, Receiver, Offset ADDRESS = 'sb://####.servicebus.windows.

我正在使用azure event hub python SDK通过此链接向event hub发送消息,并从event hub接收消息。。我可以成功发送和接收消息。但是如何解析消息并从事件数据对象检索数据呢。请在下面查找代码

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
如果我试图查看收到的事件数据,我可以看到以下消息。 事件数据
事件\数据.message

<uamqp.message.Message at 0xd4f1240>

关于如何解析此消息以提取数据的上述任何帮助

截至,有新的实用程序方法可提取消息的实际数据:

那么,过去是什么

import json
event_obj = json.loads(next(event_data.body).decode('UTF-8'))
现在是:

event_obj = event_data.body_as_json()
截至,有新的实用程序方法提取消息的实际数据:

那么,过去是什么

import json
event_obj = json.loads(next(event_data.body).decode('UTF-8'))
现在是:

event_obj = event_data.body_as_json()

对于使用Event Hub版本5.2.0(最新版本为(,)的用户,它与1.1.0版本相同,即使用
body\u as\u str()
body\u as\u 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.eventhub导入EventHubConsumerClient
连接_str='>'
消费者组='>'
eventhub_name='>'
客户端=EventHubConsumerClient.from\u连接\u字符串(
连接\u str,消费者\u组,eventhub\u name=eventhub\u name
)
事件批处理上的def(分区上下文、事件):
分区\上下文。更新\检查点()
对于事件中的e:
打印(例如,body_as_str())
与客户:
client.receive\u批处理(
on_event_batch=on_event_batch,
起始_position=“-1”,#“-1”是从分区的开始。
)

对于使用Event Hub版本5.2.0(最新版本为(,)的用户,它与1.1.0版本相同,即使用
body\u as\u str()
body\u as\u 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.eventhub导入EventHubConsumerClient
连接_str='>'
消费者组='>'
eventhub_name='>'
客户端=EventHubConsumerClient.from\u连接\u字符串(
连接\u str,消费者\u组,eventhub\u name=eventhub\u name
)
事件批处理上的def(分区上下文、事件):
分区\上下文。更新\检查点()
对于事件中的e:
打印(例如,body_as_str())
与客户:
client.receive\u批处理(
on_event_batch=on_event_batch,
起始_position=“-1”,#“-1”是从分区的开始。
)

Try
event\u data.body
Reference:它给了我这个。如何从中获取str数据。有什么建议吗?它是一个生成器对象,每次调用时都会返回一个值,即
next(event\u data.body)
。如果你想一次得到所有的值,请列出(event_data.body)@Shiva如果问题解决了,你可以把它作为一个答案。谢谢@TomSun,发布了一个详细的答案。试试
event_data.body
参考:它给了我这个。如何从中获取str数据。有什么建议吗?它是一个生成器对象,每次调用时都会返回一个值,即
next(event\u data.body)
。如果您希望同时获得所有值,请执行
列表(event_data.body)
@Shiva如果问题解决,您可以将其添加为答案。谢谢@TomSun,发布了详细的答案。