简体   繁体   中英

How to send multiple messages to eventhub using python

I already send batch messages using C# libs. I want to do the same thing using python, how to do it? Actually I'm able to send single messages but batch send will increase my throughtput. This is the code:

from azure.servicebus import ServiceBusService

key_name = 'RootManageSharedAccessKey' # SharedAccessKeyName from Azure portal
key_value = '' # SharedAccessKey from Azure portal
sbs = ServiceBusService(service_namespace,
                        shared_access_key_name=key_name,
                        shared_access_key_value=key_value)

sbs.send_event('myhub', '{ "DeviceId":"dev-01", "Temperature":"37.0" }')

I think it's possible because on the manual it says:

"The event content is the event message or JSON-encoded string that contains multiple messages."

Link to the manual

Try to replace the event content with the format like this,

[{"Body":"Message1"},{"Body":"Message2"},{"Body":"Message3"}]

So the complete code will be like this,

from azure.servicebus import ServiceBusService

key_name = 'RootManageSharedAccessKey' # SharedAccessKeyName from Azure portal
key_value = 'hdckR8xd*********************u5a84RoZSQHE=' # SharedAccessKey from Azure portal
service_namespace = 'myservice-ns' # service bus namespace

sbs = ServiceBusService(service_namespace,
                        shared_access_key_name=key_name,
                        shared_access_key_value=key_value)

sbs.create_event_hub('myhub')

sbs.send_event('myhub', '[{"name":"derek", "gender":"male"},{ "DeviceId":"dev-01", "Temperature":"37.0" }]')

Refer to Event Hubs (classic) REST for some information.

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