【问题标题】:How do you attach properties to JSON messages sent to eventhub using python library?如何使用 python 库将属性附加到发送到 eventhub 的 JSON 消息?
【发布时间】:2020-10-01 09:56:25
【问题描述】:

我正在尝试使用 pythonEventHubProducerClient 库将带有正文和属性部分的 JSON 消息发送到 Azure EventHub,但目前无法包含属性部分。这是与此处提出的问题类似的问题,但是该修复对我不起作用:How to send Properties details in EventHub message using python?

我使用的代码如下:

eventhubConnectionString = "example connection string"
eventhubName = "example eventhub name"
body = '{"ExampleBody":"ExampleValue"}'
properties = {"ExampleProperty":"ExampleValue"}
async def send_message(eventhubConnectionString, eventhubName, body, properties):
  producer = EventHubProducerClient.from_connection_string(conn_str=eventhubConnectionString, eventhub_name=eventhubName)
  async with producer:
      event_data_batch = await producer.create_batch()
      event = EventData(body)
      event.application_properties = properties
      event_data_batch.add(event)
      await producer.send_batch(event_data_batch)

运行时,代码不会产生任何错误,但是从 eventthub 读取消息时,消息中包含的属性不存在。有谁知道如何正确地将属性附加到消息中?任何帮助将不胜感激!

【问题讨论】:

    标签: python json azure azure-eventhub


    【解决方案1】:

    您引用的问题是使用事件中心客户端库的旧版本。在您使用的当前版本中,应用程序属性可通过EventData 的properties 成员获得。

    将其应用于您的 sn-p,重要的部分如下所示:

    async with producer:
        event_data_batch = await producer.create_batch()
        event = EventData(body)
    
        # This is the important change
        event.properties = {"ExampleProperty":"ExampleValue"}
    
        event_data_batch.add(event)
        await producer.send_batch(event_data_batch)
    

    可以在 SDK 存储库的 this sample 中找到一个示例。

    【讨论】:

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