如何使用python在EventHub消息中发送属性详细信息? [英] How to send Properties details in EventHub message using python?

查看:123
本文介绍了如何使用python在EventHub消息中发送属性详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是本文"

消息具有以下组件:偏移量,正文,systemProperties,属性. 偏移量是自动生成的,但我们可以提供另一个.

对于我的项目-除了消息正文外,我还需要发送不属于正文一部分的"Properties",该怎么做?我检查了类EventData(object):看起来application_properties可以用于执行此操作,但是我不确定如何实现.

application_properties是否类似于消息中的属性"?

您能否通过示例显示如何使用python EventData类发送除消息正文(如属性和SystemProperties)之外的详细信息的示例,向文章中添加更多详细信息. 截至目前,属性已作为空白列表发送.

我需要使用Python发送到EventHub的示例消息格式:

Sender.send(EventData('{"**offset**":"2415248","**body**":"TESTone:100, 
Temperature:553.0","**systemProperties**":[{"key":{"string":"x-opt- 
sequence-number"},"value":{"string":"23512"}},{"key":{"string":"x-opt- 
offset"},"value":{"string":"2415248"}},{"key":{"string":"x-opt- 
enqueued-time"},"value":{"string":"Fri Feb 22 02:14:23 UTC 
2019"}}],"**properties**":[]}'))

我想在属性中发送此dict值:{"key":{"string":"Type"},"value":{"string":"iPhone"}}

解决方案

找到了解决方案,是的,我们可以使用"application_properties"发送消息的"properties". 这是我有效的示例代码:

from azure.eventhub import EventHubClient, Receiver, Offset, Sender, EventData
from uamqp import Message

ADDRESS = "amqps://<>.windows.net/<>"
USER = "RootManageSharedAccessKey"
KEY = "<>"

client = EventHubClient(ADDRESS, debug=True, username=USER, password=KEY)
Sender = client.add_sender(partition="0")
client.run()

event = EventData(body="TESTTWO:100, Temperature:-127.0")
event.application_properties = {"Type": "iPhone"}
Sender.send(event)

I am referring to this article "https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send" related to sending messages to EventHub using Python.

A Message has the following components: offset, body, systemProperties, properties. offset is auto-generated but we can provide the other.

For my project - apart from message body I also need to send "Properties" which is not part of Body, How to do that? I checked the class EventData(object): and looks like application_properties can be used to do this but i'm not sure how to implement this.

Is application_properties right analogy to obtain "Properties" in message?

Could you please add more details to the article with example displaying how to use the python EventData class for sending detailed information apart from message body like Properties and SystemProperties. As of now Properties is being sent as empty list.

Sample message format that i need to send to EventHub using Python:

Sender.send(EventData('{"**offset**":"2415248","**body**":"TESTone:100, 
Temperature:553.0","**systemProperties**":[{"key":{"string":"x-opt- 
sequence-number"},"value":{"string":"23512"}},{"key":{"string":"x-opt- 
offset"},"value":{"string":"2415248"}},{"key":{"string":"x-opt- 
enqueued-time"},"value":{"string":"Fri Feb 22 02:14:23 UTC 
2019"}}],"**properties**":[]}'))

And I want to send this dict values in properties: {"key":{"string":"Type"},"value":{"string":"iPhone"}}

解决方案

Found solution to this, yes we can use "application_properties" to send "properties" of message. This is my sample code that worked:

from azure.eventhub import EventHubClient, Receiver, Offset, Sender, EventData
from uamqp import Message

ADDRESS = "amqps://<>.windows.net/<>"
USER = "RootManageSharedAccessKey"
KEY = "<>"

client = EventHubClient(ADDRESS, debug=True, username=USER, password=KEY)
Sender = client.add_sender(partition="0")
client.run()

event = EventData(body="TESTTWO:100, Temperature:-127.0")
event.application_properties = {"Type": "iPhone"}
Sender.send(event)

这篇关于如何使用python在EventHub消息中发送属性详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆