Eventhub读取数据作为python中的列表

时间:2019-10-16 05:05:19

标签: python azure-eventhub

我想将Azure Eventhub用作中间件消息传递队列。我基本上是以列表格式发送模拟数据,现在以字符串格式接收它。

如您所见Here,只有几种格式可以转换数据。我希望数据格式为包含浮动数据的列表。

这是我现在正在处理的代码。我正在尝试处理以下行以累积在列表中的浮点形式的每个事件数据。

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()

您可以在Here

中找到eventData类

================================更新============= =====================

结果是,它显示“消息[a b c ....]”,我认为消息已设置为要写入,因此我想在结果格式中删除“消息”一词。

“ 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

1 个答案:

答案 0 :(得分:1)

以下固定代码有效:

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

通过这种方式,我能够收到没有“消息”的消息。