Eventhub在python中以列表形式读取数据
我想将Azure Eventhub用作中间件消息传递队列。我基本上是以列表格式发送模拟数据,然后以字符串格式接收 如您所见,只有少数几种格式的数据是可转换的。我希望数据的格式是一个包含浮点数据的列表 这是我现在正在编写的代码。我正试图将下面的行操作到列表中以浮点形式累积的每个事件数据Eventhub在python中以列表形式读取数据,python,azure-eventhub,Python,Azure Eventhub,我想将Azure Eventhub用作中间件消息传递队列。我基本上是以列表格式发送模拟数据,然后以字符串格式接收 如您所见,只有少数几种格式的数据是可转换的。我希望数据的格式是一个包含浮点数据的列表 这是我现在正在编写的代码。我正试图将下面的行操作到列表中以浮点形式累积的每个事件数据 LIST.append(event_data.message._body) 这是我的代码的主体 CONSUMER_GROUP = "$default" OFFSET = Offset("-1") PARTITIO
LIST.append(event_data.message._body)
这是我的代码的主体
CONSUMER_GROUP = "$default"
OFFSET = Offset("-1")
PARTITION = "0"
total = 0
last_sn = -1
last_offset = "-1"
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
i=1
LIST=[]
try:
receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=5000, offset=OFFSET)
client.run()
start_time = time.time()
batch = receiver.receive(timeout=None)
while batch:
for event_data in batch[-100:]:
last_offset = event_data.offset
last_sn = event_data.sequence_number
print("Received: {}, {}".format(i, last_sn))
LIST.append(event_data.message._body)
i += 1
total += 1
batch = receiver.receive(timeout=5000)
end_time = time.time()
client.stop()
run_time = end_time - start_time
print("Received {} messages in {} seconds".format(total, run_time))
except KeyboardInterrupt:
pass
finally:
client.stop()
您可以在中找到eventData类
=============================================更新===================================
因此,它会显示“Message[abc….]”,我认为该消息已设置为写入,因此我想删除结果格式中的“Message”一词
“sender.py”如下所示:
from azure.eventhub import EventHubClient, Sender, EventData
import time
import logging
import numpy as np
logger = logging.getLogger("azure")
ADDRESS = ""
USER = "RootManageSharedAccessKey"
KEY = ""
try:
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
sender = client.add_sender(partition="0")
client.run()
forging2 = lambda x: (np.exp(-(0.1*x-6)**2+3) + np.exp(-(0.1*x-4)**2+4))*1.4
x_value = np.arange(100)
try:
start_time = time.time()
for i in range(100):
y_value1 = forging2(x_value) + np.random.normal(0,1,len(x_value))*3
y_value1 = np.asarray(y_value1)
print("Sending message: {}, {}".format(i, y_value1))
message = y_value1
sender.send(EventData(message))
time.sleep(0.35)
except:
raise
finally:
end_time = time.time()
client.stop()
run_time = end_time - start_time
logger.info("Runtime: {} seconds".format(run_time))
except KeyboardInterrupt:
pass
固定代码如下所示:
logger = logging.getLogger("azure")
ADDRESS = ""
USER = "RootManageSharedAccessKey"
KEY = ""
try:
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
sender = client.add_sender(partition="0")
client.run()
forging2 = lambda x: (np.exp(-(0.1*x-6)**2+3) + np.exp(-(0.1*x-4)**2+4))*1.4
x_value = np.arange(100)
try:
start_time = time.time()
for i in range(100000):
y_value1 = forging1(x_value) + np.random.normal(0,1,len(x_value))*3
y_value1 = np.asarray(y_value1)
print("Sending message: {}, {}".format(i, y_value1))
message = "{}".format(y_value1)
sender.send(EventData(message))
time.sleep(0.35)
except:
raise
finally:
end_time = time.time()
client.stop()
run_time = end_time - start_time
logger.info("Runtime: {} seconds".format(run_time))
except KeyboardInterrupt:
pass
通过这种方式,我能够在没有“消息”的情况下接收消息 你能提供你的发送方法和发送的数据类型吗?@IvanYang所以,我发送了一个包含100个浮点数的数组。eventclient是对象客户机的一个类,因此,您没有在事件数据中添加“消息”,对吗?@IvanYang当然没有。它只是自动添加的,我不知道它来自哪里。我查找了似乎链接到Eventdata的message类,但仍然无法在其中找到它。我要确认的最后一件事是,如何获取“message[a b c….]”,只打印列表,还是其他什么?