Azure Function App 不由事件中心触发
Azure Function App does not trigger by Event Hub
大家好。
我已经编写了 Python 函数来处理即将到达 EventHub 的 JSON 事件。这些事件是由 Debezium 生成的,这部分工作正常。当从 Visual Studio 代码在本地执行时,我的 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()
function.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"
}
]
}
Trigger enabled on Azure portal
当 Function App 在本地 运行 并且在部署到 Azure 时未被触发时,通常的问题是由于应用程序设置中缺少连接字符串。您本地项目中的 local.settings.json 文件中的设置应与 Azure 中的函数应用程序中的应用程序设置相同。通过 VS Code 部署时,您可以 publish them 到 Azure。您可以通过门户添加缺少的连接,也可以通过配置 -> 新应用程序设置。
大家好。
我已经编写了 Python 函数来处理即将到达 EventHub 的 JSON 事件。这些事件是由 Debezium 生成的,这部分工作正常。当从 Visual Studio 代码在本地执行时,我的 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()
function.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"
}
]
}
Trigger enabled on Azure portal
当 Function App 在本地 运行 并且在部署到 Azure 时未被触发时,通常的问题是由于应用程序设置中缺少连接字符串。您本地项目中的 local.settings.json 文件中的设置应与 Azure 中的函数应用程序中的应用程序设置相同。通过 VS Code 部署时,您可以 publish them 到 Azure。您可以通过门户添加缺少的连接,也可以通过配置 -> 新应用程序设置。