如何使用 python 库将属性附加到发送到 eventhub 的 JSON 消息?
How do you attach properties to JSON messages sent to eventhub using python library?
我正在尝试使用 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)
当 运行 时,代码不会生成任何错误,但是当从 eventhub 读取消息时,消息中包含的属性不存在。有谁知道如何正确地将属性附加到消息中?任何帮助将不胜感激!
您提到的问题是使用旧版本的事件中心客户端库。在您使用的当前版本中,应用程序属性可通过 EventData
的 properties
成员获得。
将其应用于您的代码段,重要的部分如下所示:
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 中找到示例。
我正在尝试使用 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)
当 运行 时,代码不会生成任何错误,但是当从 eventhub 读取消息时,消息中包含的属性不存在。有谁知道如何正确地将属性附加到消息中?任何帮助将不胜感激!
您提到的问题是使用旧版本的事件中心客户端库。在您使用的当前版本中,应用程序属性可通过 EventData
的 properties
成员获得。
将其应用于您的代码段,重要的部分如下所示:
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 中找到示例。