簡體   English   中英

如何在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 :如果郵件具有BrokeredMessage.PartitionKey屬性,但沒有設置BrokeredMessage.SessionId屬性,則服務總線將PartitionKey屬性用作分區鍵。 如果郵件同時設置了SessionId和PartitionKey屬性,則兩個屬性必須相同。 如果PartitionKey屬性設置為與SessionId屬性不同的值,則Service Bus返回無效的操作異常。 如果發件人發送非會話感知的事務性消息,則應使用PartitionKey屬性。 分區鍵可確保同一事務中發送的所有消息均由同一個消息傳遞代理處理。

對於使用Python,請執行以下步驟。

  1. 通過pip install python-qpid-proton用於AMQP的Python Qpid Proton軟件包。
  2. 這是我的示例代碼作為參考。

     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類參考。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM