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

拜拜、爱过 提交于 2020-01-03 05:53:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/44427622/how-to-send-message-to-azure-event-hub-with-amqp-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!