简体   繁体   English

如何在Python中使用amqp将消息发送到Azure事件中心

[英]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? 任何人都知道如何使用python中的amqp向Azure事件中心发送消息吗? I need to send the message with partition key(not the partition id). 我需要发送带有分区键(而不是分区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. 根据以下官方文档“ Partitioned queues and topics Use of partition keys ”部分,可以通过设置消息的PartitionKey属性来发送带有分区键的消息。

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. PartitionKey :如果邮件具有BrokeredMessage.PartitionKey属性,但没有设置BrokeredMessage.SessionId属性,则服务总线将PartitionKey属性用作分区键。 If the message has both the SessionId and the PartitionKey properties set, both properties must be identical. 如果邮件同时设置了SessionId和PartitionKey属性,则两个属性必须相同。 If the PartitionKey property is set to a different value than the SessionId property, Service Bus returns an invalid operation exception. 如果PartitionKey属性设置为与SessionId属性不同的值,则Service Bus返回无效的操作异常。 The PartitionKey property should be used if a sender sends non-session aware transactional messages. 如果发件人发送非会话感知的事务性消息,则应使用PartitionKey属性。 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. 对于使用Python,请执行以下步骤。

  1. Install the Python Qpid Proton package for AMQP via pip install python-qpid-proton . 通过pip install python-qpid-proton用于AMQP的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 . 请在此处查看proton.Message类参考。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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