大家好。
我编写了 Python 函数来处理传入 EventHub 的 JSON 事件。这些事件是由 Debezium 生成的,这部分工作正常。从 Visual Studio Code 本地执行时,我的 python 代码也可以正常工作。当我将(使用 VSC)部署到 Azure Function App 时,问题就开始了。似乎传入事件不会触发函数应用程序执行。
我究竟做错了什么?
我的功能代码:
from typing import List
import logging
import json
import psycopg2
import azure.functions as func
def main(events: List[func.EventHubEvent]):
#logging.info('Starting')
conn = psycopg2.connect(database="RDSA", user='postgres', password='********', host='********.postgres.database.azure.com', port= '5432')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
for event in events:
row = json.loads(event.get_body().decode('utf-8'))
#logging.info('Python EventHub trigger processed an event: %s',
# event.get_body().decode('utf-8'))
rowDepartmentId=row["payload"]["after"]["departmentid"]
rowDepartmentName=row["payload"]["after"]["name"]
rowDepartmentGroupName=row["payload"]["after"]["groupname"]
rowModifiedDate=row["payload"]["after"]["modifieddate"]
SQL="""INSERT INTO CDCSTAGE.TF_DEPARTMENT (departmentid, \"name\", groupname, modifieddate)
VALUES (%s, '%s', '%s', to_timestamp(%s / 1000000));""" % (rowDepartmentId, rowDepartmentName, rowDepartmentGroupName, rowModifiedDate)
logging.info('=========== New record in DB: =============')
logging.info('Department name: %s', row["payload"]["after"]["name"])
logging.info('Department group: %s', row["payload"]["after"]["groupname"])
logging.info('Modified date: %s', row["payload"]["after"]["modifieddate"])
logging.info('SQL generated: %s', SQL)
logging.info('===========================================')
#Executing an MYSQL function using the execute() method
cursor.execute(SQL)
#Closing the connection
conn.commit()
cursor.close()
conn.close()
函数.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": "adventureworks.humanresources.department",
"connection": "AazureEventhubKafka_RootManageSharedAccessKey_EVENTHUB",
"cardinality": "many",
"consumerGroup": "$Default",
"dataType": "binary"
}
]
}