简体   繁体   中英

How to send message to Azure event hub with amqp in python

Anyone knows how to send a message to Azure event hub with amqp in python? I need to send the message with partition key(not the partition id). Thanks very much.

According to the section Use of partition keys of the offical document Partitioned queues and topics , as below, you can send a message with partition key via set the PartitionKey property of the message.

PartitionKey : If a message has the BrokeredMessage.PartitionKey property but not the BrokeredMessage.SessionId property set, then Service Bus uses the PartitionKey property as the partition key. If the message has both the SessionId and the PartitionKey properties set, both properties must be identical. If the PartitionKey property is set to a different value than the SessionId property, Service Bus returns an invalid operation exception. The PartitionKey property should be used if a sender sends non-session aware transactional messages. The partition key ensures that all messages that are sent within a transaction are handled by the same messaging broker.

For using Python, the steps as below.

  1. Install the Python Qpid Proton package for AMQP via pip install python-qpid-proton .
  2. Here is my sample code as reference.

     from proton import Messenger, Message messenger = Messenger() message = Message() message.address = "amqps://<shared_access_policy_name>:<shared_access_policy_key>@<your-servicebus-namespace>.servicebus.windows.net/<your-eventhub-name>" message.properties = { "PartitonKey" : "<a partitonKey you want>", } message.body = u"This is a text string" messenger.put(message) messenger.send() 

Please see the proton.Message class reference here .

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM