首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python的EventHub触发器功能

使用python的EventHub触发器功能
EN

Stack Overflow用户
提问于 2020-03-12 06:41:44
回答 1查看 737关注 0票数 0

希望你做得很好。

我需要您的帮助,在基本设置天青功能,使用python获得事件集线器字符串数据,并推动遥测同时到存储和网络应用程序。谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-03-24 05:17:30

Azure函数触发EventHub示例,从发送方发送的EventHub读取消息,并使用Azure表绑定将输出记录写入Azure表存储。

function.json

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
# -*- 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)

完整的样本可以在上找到

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60648818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档