希望你做得很好。
我需要您的帮助,在基本设置天青功能,使用python获得事件集线器字符串数据,并推动遥测同时到存储和网络应用程序。谢谢
发布于 2020-03-24 05:17:30
Azure函数触发EventHub示例,从发送方发送的EventHub读取消息,并使用Azure表绑定将输出记录写入Azure表存储。
function.json
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "myEventHubMessage",
"path": "<eventhub-entitiy-name>",
"consumerGroup": "$Default",
"connection": "<eventhub-namespace>_<SAS-policy-name>_EVENTHUB",
"cardinality": "one",
"direction": "in"
},
{
"type": "table",
"name": "outputTable",
"tableName": "<table-name>",
"connection": "<storage-account-name>_STORAGE",
"direction": "out"
}
],
"disabled": false
}
run.py
# -*- coding: utf-8 -*-
import os
import sys
import json
import uuid
"""
Expected Receiving Body Message:
{
"deviceId": "myDevice0001",
"temperature": "10.1"
}
[note]
Use "deviceId" as PartitionKey for Azure table to write
Expected Function's Trigger Configuration:
- 'trigger': AzureEventHub
- 'Event hub cardinality': 'One'
Expected Function's Output Configuration:
- 'output': Azure Table Storage
- 'Table parameter name: 'outputTable
"""
# Read the EventHub Message
receivedBody = json.loads(open(os.environ['myEventHubMessage']).read())
print('Received body:', receivedBody)
# -> ('received object:', {u'deviceId': u'myDevice0001', u'temperature': u'10.1'})
if not 'deviceId' in receivedBody or not 'temperature' in receivedBody:
print("Skip: invalid eventHub body!")
sys.exit(0)
## Device ID
recordId = str(uuid.uuid4())
outdoc= {
"PartitionKey": receivedBody['deviceId'],
"RowKey": recordId,
"temperature": receivedBody['temperature']
}
# Writing to Azure Table Storage (Table parameter name: outputTable)
print('Writing data to Azure Table:', outdoc)
with open(os.environ['outputTable'], 'w') as f:
json.dump(outdoc,f)
完整的样本可以在上找到
https://stackoverflow.com/questions/60648818
复制相似问题