如何在 python 中使用 amqp 将消息发送到 Azure 事件中心
How to send message to Azure event hub with amqp in python
有人知道如何在 python 中使用 amqp 向 Azure 事件中心发送消息吗?我需要使用分区键(不是分区 ID)发送消息。非常感谢。
根据官方文档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. 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.
使用Python,步骤如下。
- 通过
pip install python-qpid-proton
. 安装 AMQP 的 Python Qpid Proton 包
这是我的示例代码作为参考。
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()
请参阅 proton.Message
class 参考 here。
希望对您有所帮助。
有人知道如何在 python 中使用 amqp 向 Azure 事件中心发送消息吗?我需要使用分区键(不是分区 ID)发送消息。非常感谢。
根据官方文档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. 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.
使用Python,步骤如下。
- 通过
pip install python-qpid-proton
. 安装 AMQP 的 Python Qpid Proton 包
这是我的示例代码作为参考。
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()
请参阅 proton.Message
class 参考 here。
希望对您有所帮助。