问题
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"}}
回答1:
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)
来源:https://stackoverflow.com/questions/54819998/how-to-send-properties-details-in-eventhub-message-using-python