EventHub使用python触发功能

问题描述 投票:0回答:1

希望您做的很好。

我需要您在使用python来获取Azure函数的基本设置方面的帮助,以获取事件中心字符串数据并将遥测同时推送到存储和Web应用程序。谢谢

web-applications storage python-3.6 azure-eventhub azure-function-app
1个回答
0
投票

[Azure函数EventHub触发器Python示例,它从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://github.com/yokawasa/azure-functions-python-samples/tree/master/v1functions/eventhub-trigger-table-out-bindings]上找到完整的样本>

© www.soinside.com 2019 - 2024. All rights reserved.