Function Compute supports DataHub as a trigger source. DataHub is a data streaming service of Alibaba Cloud. When a request arrives at DataHub for which a Function Compute connector is created, DataHub triggers the execution of the function. After the function execution, Function Compute returns the execution result to DataHub. This topic describes how to configure DataHub to trigger function execution, including how to configure the input parameters, write function code, and test the function.

Prerequisites

Limits

DataHub is a one-way trigger. You can configure a trigger only in the event source. Perform the following operations:

Log on to the DataHub console. On the Projects page, click the name of the desired project to go to the project details page. In the Create Topic panel that appears, configure the parameters to create a topic. After the topic is created, go to the Topic List tab and click the name of the created topic. sc_create_topic_for_datahub_trigger

On the topic details page that appears, click + Connector in the upper-right corner. Create a Function Compute connector to synchronize data to Function Compute. After that, DataHub data can be used in Function Compute.

sc_datahub_trigger

Step 1: Configure the input parameters of the function

When DataHub triggers the execution of a function, the synchronized information is sent to the function by using the event parameter. You can use the event parameter passed in by DataHub to test whether the function code is correct.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click the name of the desired function.
  4. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  5. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.
    The following sample code provides an example of event: For more information, see the "Data structure of events" section of Synchronize data to Function Compute.
    {
      "eventSource": "acs:datahub",
      "eventName": "acs:datahub:putRecord",
      "eventSourceARN": "/projects/test_project_name/topics/test_topic_name",
      "region": "cn-hangzhou",
      "records": [
        {
          "eventId": "0:12345",
          "systemTime": 1463000123000,
          "data": "[\"col1's value\",\"col2's value\"]"
        },
        {
          "eventId": "0:12346",
          "systemTime": 1463000156000,
          "data": "[\"col1's value\",\"col2's value\"]"
        }
      ]
    }

Step 2: Write the function code and test the function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click the name of the desired function.
  4. On the function details page, click the Code tab, edit the function code in the code editor, and then click Deploy.
    In this example, the function code is written in Python 3.6.
    # -*- coding: utf-8 -*-
    import logging
    import json
    
    # To enable the initializer feature
    # please implement the initializer function as below:
    # def initializer(context):
    #   logger = logging.getLogger()
    #   logger.info('initializing')
    
    def handler(event, context):
      logger = logging.getLogger()
      logger.info('hello world')
      # parse and handle event here
      # evt = json.loads(event) 
      logger.info(event)
      return 'hello world'
  5. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.