【问题标题】:Sending header data in Azure eventhub using Python使用 Python 在 Azure eventhub 中发送标头数据
【发布时间】:2020-08-10 14:41:13
【问题描述】:

我正在使用 microsoft 提供的标准代码向 Azure 事件中心发送一条消息,它工作正常,但是,我还想为每条消息添加一个标头,以了解有关该消息的其他详细信息。我无法找到方法,以下是我的尝试,但没有成功。

client = EventHubClient(ADDRESS, username=USER, password=KEY, debug=True)
    sender = client.add_sender(partition="0", send_timeout=2000, keep_alive=500)
    client.run()
    try:
        nevent_data = EventData('Message with properties')
        nevent_data.properties = {'prop': 'prop1'}
        sender.send(nevent_data)

在此我尝试以 dict 的形式发送属性以及消息正文,以下是侦听器。

听众:

client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
try:
    receiver = client.add_receiver(
        CONSUMER_GROUP, PARTITION, prefetch=5000, offset=OFFSET)
    client.run()
    start_time = time.time()
    while True:
        for event_data in receiver.receive(timeout=1):
            print(event_data.properties)
            print("Received: {}".format(event_data.body_as_str(encoding='UTF-8')))

当我尝试获取属性时,它会出错。

【问题讨论】:

    标签: python azure azure-eventhub


    【解决方案1】:

    只需更改上面的一个参数即可。

    改变

    nevent_data.properties = {'prop': 'prop1'}
    

    nevent_data.application_properties = {'prop': 'prop1'}
    

    在接收器上做同样的事情。

    print(event_data.application_properties)
    

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多