Andrea Cattaneo
Andrea Cattaneo

Reputation: 588

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

Upvotes: 2

Views: 1224

Answers (1)

Derek
Derek

Reputation: 837

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.

Upvotes: 3

Related Questions