How do you send a variable to Azure Event Hubs using Python SDK

If I have a variable containing the temperature value, how can I send it using ServiceBusService.send_event?


It depends on how you are consuming it on the other side of event hubs. If you are using Azure Stream Analytics you must make sure that the format you are sending to event hubs is either json, csv, or arvo.

If you have a variable, it would be simple enough to create a dictionary, serialize it using Python's json library and send the resulting string. Example:

from azure.servicebus import ServiceBusService
import json;

sbs = ServiceBusService("your_namespace",

# build dictionary and send value
temp = {'DeviceId': 'dev-01', 'Temperature': str(i)}
sbs.send_event('woodstove2', json.dumps(temp))

Please make sure you have created a ServiceBus namespace for EventHub. If not, please refer to "Create an Event Hub" section of the article

You will get ServiceBus SAS name & key in the Service Bus Configure page as below.

If the service bus does not contain an event hubs, you can create it manually via clicking the tab "event hubs", or create it by using Python SDK.

from azure.servicebus import ServiceBusService

servns = '<service_bus _namespace>'
key_name = '<service_bus_sas_keyname>'  # SharedAccessKeyName from Azure portal
key_value = '<service_bus_sas_key>'  # SharedAccessKey from Azure portal
sbs = ServiceBusService(service_namespace=servns,
                        shared_access_key_value=key_value) # Create a ServiceBus Service Object
flag = sbs.create_event_hub('<event_hub_name>') # Create a Event Hub for the ServiceBus. If it exists then return true, else return false
event_data = '<event_data>'
sbs.send_event('<event_hub_name>', event_data) # Send event data to your Event Hub, like real-time temperature data

According to your scene, you can serialize event data to JSON string, CSV String with header as jsturtevant said.

For temperature example:

  1. JSON

event_data = '{"deviceId": "dev01", "time": "2015-08-24 12:34:45", "temperature": 30.0 }'

  1. CSV

event_data = 'deviceId,time,temperature\ndev01,2015-08-24 12:34:45,30.0'

For more information about ServiceBus Python SDK, you can refer to the part - "Usage" of the link "README.rst"

Need Your Help

Python regex to go match any string that starts with a string

python regex

I don't know much about regexes and am trying to learn them. I am using Python and need to use re.compile to make a regex that matches any string that starts with a variable string. The string is

Remove \xe2\x80\xa6 from string python

python string replace ascii

I have a lot of txt files, and I need to replace some text on them. Almost all of them has this non-ascii character (I thought it was "...", but … is not the same)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.