简体   繁体   English

如何使用Python SDK将变量发送到Azure事件中心

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

如果我有一个包含温度值的变量,如何使用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 . 如果使用的是Azure流分析 ,则必须确保发送到事件中心的格式为json,csv或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. 如果您有一个变量,那么创建字典,使用Python的json库对其进行序列化并发送结果字符串就足够简单了。 Example: 例:

from azure.servicebus.control_client import ServiceBusService
import json;

sbs = ServiceBusService("your_namespace",
                        shared_access_key_name="your_policy_name",
                        shared_access_key_value="your_policy_secret")

# 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. 请确保您已为EventHub创建ServiceBus命名空间。 If not, please refer to "Create an Event Hub" section of the article https://azure.microsoft.com/en-us/documentation/articles/event-hubs-csharp-ephcs-getstarted/ . 如果不是,请参阅文章https://azure.microsoft.com/zh-cn/documentation/articles/event-hubs-csharp-ephcs-getstarted/的 “创建事件中心”部分。

You will get ServiceBus SAS name & key in the Service Bus Configure page as below. 您将在“服务总线配置”页面中获得ServiceBus SAS名称和密钥,如下所示。

在此处输入图片说明

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. 如果服务总线不包含事件中心,则可以通过单击“事件中心”选项卡手动创建它,也可以使用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_name=key_name,
                        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
print(flag)
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. 根据您的场景,您可以将事件数据序列化为JSON字符串,如JSturtevant所说的带有标头的CSV字符串。

For temperature example: 对于温度示例:

  1. JSON JSON

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

  1. CSV CSV

event_data = 'deviceId,time,temperature\\ndev01,2015-08-24 12:34:45,30.0' 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" https://github.com/Azure/azure-sdk-for-python/tree/master/azure-servicebus . 有关ServiceBus Python SDK的更多信息,您可以参考链接“ README.rst”的“用法”部分https://github.com/Azure/azure-sdk-for-python/tree/master/azure-服务总线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM