Home > database >  Azure Function App does not trigger by Event Hub
Azure Function App does not trigger by Event Hub

Time:10-08

Hey Guys.

I have written Python function to handle JSON events that are coming to EventHub. Those events are generated by Debezium, and this part is working fine. My python code is also working fine when executed locally from Visual Studio Code. Problem starts when I deploy (using VSC) to Azure Function App. Seems like incoming events does not trigger function app execution.

What am I doing wrong?

My function code:

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"
    }
  ]
}

enter image description here

  • Related