This topic describes how to use Function Compute to perform real-time computing on the incremental data in Tablestore.

Background information

Alibaba Cloud Function Compute is an event-driven computing service that allows you to focus on writing and uploading code without the need to manage servers. Function Compute prepares computing resources and runs your code in an elastic manner. You are charged only for the resources that are consumed when the code is run. For more information, see What is Function Compute?.

A Tablestore Stream is a tunnel that is used to obtain incremental data from Tablestore tables. After you create Tablestore triggers, a Tablestore Stream and a function in Function Compute can be automatically connected. This allows the custom program logic in the function to automatically process modifications to data in Tablestore tables.

Scenarios

The following figure shows the tasks that you can perform by using Function Compute.

  • Data synchronization: You can use Function Compute to synchronize real-time data that is stored in Tablestore to data cache, search engines, or other database instances.
  • Data archiving: You can use Function Compute to archive incremental data that is stored in Tablestore to OSS for cold backup.
  • Event-driven application: You can create triggers to trigger functions to call API operations that are provided by IoT Hub and cloud applications. You can also create triggers to send notifications.
fig_fuc001

Prerequisites

Usage notes

Make sure that the Tablestore data table resides in the same region as the Function Compute service.

Step 1: Enable the Stream feature for the data table

Before you create a trigger, you must enable the Stream feature for the data table in the Tablestore console to allow the function to process incremental data that is written to the table.

  1. Log on to the Tablestore console.
  2. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance that you want to manage.
  3. In the Tables section of the Instance details tab, click the name of the required data table and click the Tunnels tab. Alternatively, you can click the fig_001 icon and then click Tunnels.
  4. On the Tunnels tab, click Enabled in the Stream Information section.
  5. In the Enable Stream dialog box, configure the Log Expiration Time parameter and click Enabled.

    The value of the Log Expiration Time parameter must be a non-zero integer and cannot be changed after it is specified. Unit: hours. Maximum value: 168.

    Note Specify a value for the Log Expiration Time parameter based on your business requirements.

Step 2: Configure a Tablestore trigger

You can create a Tablestore trigger in the Function Compute console to process the real-time data stream in a Tablestore data table.

  1. Create a Function Compute service.
    1. Log on to the Function Compute console.
    2. In the left-side navigation pane, click Services & Functions.
    3. In the top navigation bar, select a region.
    4. On the Services page, click Create Service.
    5. In the Create Service panel, configure the Name and Description parameters and configure the Logging and Tracing Analysis parameters based on your business requirements.
      For more information about the parameters, see Manage microservices.
    6. Click OK.
      After you create the service, you can view the service and service configurations on the Services page.
  2. Create a Function Compute function.
    Note You can create a function from scratch, by using a container image, or by using a template. The following procedure describes how to create a function from scratch. For information about how to create a function by using other methods, see Use a container image to create a function and Use function templates to create functions.
    1. On the Services page, click the name of the service that you want to manage.
    2. In the left-side navigation pane, click Functions and then click Create Function.
    3. On the Create Function page, select Start from Scratch.
    4. In the Basic Settings section, configure the parameters. The following table describes the parameters.
      Parameter Required Description Example
      Function Name No Enter a name for the function. The name can be up to 64 characters in length and can contain digits, letters, underscores (_), and hyphens (-). The function name is case-sensitive and must start with a letter.
      Note If you leave this parameter empty, Function Compute automatically creates a name for your function.
      Function
      Runtime Environments Yes Select a language, for example, Python, Java, PHP, or Node.js. For information about the runtime environments that are supported by Function Compute, see Manage functions. Python 3.6
      Request Type Yes If you want to use a Tablestore trigger, select Event Requests. Event Requests
      Instance Category Yes Select an instance category for the function. Valid values:
      • Elastic Instance
      • Performance Instance

      For more information, see Instance types and instance modes. For information about the billing of each instance category, see Overview.

      Elastic Instance
      Memory Capacity Yes Specify the size of the memory that is required to execute the function by using one of the following methods:
      • Select a value: Select a value from the drop-down list.
      • Input a value: Click Enter Memory Size and enter a value for the memory size. Valid values:
        • Elastic Instance: [128, 3072]. Unit: MB.
        • Performance Instance: [4, 32]. Unit: GB.
        Note The value must be a multiple of 64 MB.
      512 MB

      After you create the function, you can view the function on the Functions page.

    5. In the Configure Trigger section, configure the parameters. The following table describes the parameters.
      Parameter Description Example
      Trigger Type Select Tablestore. Tablestore
      Name Enter a name for the trigger. Tablestore-trigger
      Instance Select a Tablestore instance from the drop-down list. distribute-test
      Table Select a data table from the drop-down list. source_data
      Role Name Select AliyunTableStoreStreamNotificationRole.
      Note After you configure the preceding parameters, click OK. The first time you create a trigger of this type, click Authorize Now in the message that appears, create the role, and assign permissions to the role as prompted.
      AliyunTableStoreStreamNotificationRole
    6. Click Create.
      The trigger that you created is displayed on the Triggers tab.
      Note You can also view and create Tablestore triggers on the Trigger tab of the table in the Tablestore console.

Step 3: Test the function

Function Compute allows you to debug functions online. You can create an event to trigger a function and test whether the function logic is implemented as expected.

Tablestore events that trigger Function Compute are in the CBOR format, which is a JSON-like binary format. You can debug a function online by performing the following steps:

  1. Write code.
    1. On the Functions page, click the name of the function that you want to manage.
    2. On the function details page, click the Code tab to write code in the code editor.
      Note In this example, the function code is written in Python.

      In the code, records = json.loads(event) is used to process custom trigger events for the test.

      import logging
      import cbor
      import json
       def get_attribute_value(record, column):
           attrs = record[u'Columns']
           for x in attrs:
               if x[u'ColumnName'] == column:
                   return x['Value']
       def get_pk_value(record, column):
           attrs = record[u'PrimaryKey']
           for x in attrs:
               if x['ColumnName'] == column:
                   return x['Value']
       def handler(event, context):
           logger = logging.getLogger()
           logger.info("Begin to handle event")
           #records = cbor.loads(event)
           records = json.loads(event)
           for record in records['Records']:
               logger.info("Handle record: %s", record)
               pk_0 = get_pk_value(record, "pk_0")
               attr_0 = get_attribute_value(record, "attr_0")
           return 'OK'
  2. Test the function.
    1. On the Code tab, click the fig_20220419_downlist icon next to Test Function and select Configure Test Parameters from the drop-down list.
    2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, select Tablestore for Event Template, and then enter the event name and event content. Click OK.
      For more information about the format of event content, see Appendix: Data processing.
    3. Click Test Function to check whether the function performs as expected.
      After you test the function by using records=json.loads(event), you can change this line of code to records = cbor.loads(event) and click Deployment Code. This way, the specific function logic is triggered when data is written to the Tablestore data table.

Appendix: Data processing

When you configure test parameters, you must specify the event content based on a specific data format.

  • Data format

    A Tablestore trigger encodes incremental data in the CBOR format to create a Function Compute event. The following code provides an example of the format of incremental data:

    {
        "Version": "string",
        "Records": [
            {
                "Type": "string",
                "Info": {
                    "Timestamp": int64
                },
                "PrimaryKey": [
                    {
                        "ColumnName": "string",
                        "Value": formated_value
                    }
                ],
                "Columns": [
                    {
                        "Type": "string",
                        "ColumnName": "string",
                        "Value": formated_value,
                        "Timestamp": int64
                    }
                ]
            }
        ]
    }
  • Parameters

    The following table describes the parameters that are included in the preceding format.

    Parameter Description
    Version The version of the payload, which is Sync-v1. Data type: string.
    Records The array that stores the row of incremental data in the data table. This parameter includes the following fields:
    • Type: the type of the operation that is performed on the row. Valid values: PutRow, UpdateRow, and DeleteRow. Data type: string.
    • Info: includes the Timestamp field, which specifies the time when the row was last modified. The value of Timestamp must be in UTC. Data type: int64.
    PrimaryKey The array that stores the primary key column. This parameter includes the following fields:
    • ColumnName: the name of the primary key column. Data type: string.
    • Value: the content of the primary key column. Data type: formated_value. Valid values: integer, string, and blob.
    Columns The array that stores the attribute columns. This parameter includes the following fields:
    • Type: the type of the operation that is performed on the attribute column. Valid values: Put, DeleteOneVersion, and DeleteAllVersions. Data type: string.
    • ColumnName: the name of the attribute column. Data type: string.
    • Value: the content of the attribute column. Data type: formated_value. Valid values: integer, boolean, double, string, and blob.
    • Timestamp: the time when the attribute column was last modified. The value of Timestamp must be in UTC. Data type: int64.
  • Data example
    {
        "Version": "Sync-v1",
        "Records": [
            {
                "Type": "PutRow",
                "Info": {
                    "Timestamp": 1506416585740836
                },
                "PrimaryKey": [
                    {
                        "ColumnName": "pk_0",
                        "Value": 1506416585881590900
                    },
                    {
                        "ColumnName": "pk_1",
                        "Value": "2017-09-26 17:03:05.8815909 +0800 CST"
                    },
                    {
                        "ColumnName": "pk_2",
                        "Value": 1506416585741000
                    }
                ],
                "Columns": [
                    {
                        "Type": "Put",
                        "ColumnName": "attr_0",
                        "Value": "hello_table_store",
                        "Timestamp": 1506416585741
                    },
                    {
                        "Type": "Put",
                        "ColumnName": "attr_1",
                        "Value": 1506416585881590900,
                        "Timestamp": 1506416585741
                    }
                ]
            }
        ]
    }

Appendix: Create a trigger by using an existing Function Compute service and function

You can use an existing Function Compute function and service to create a Tablestore trigger in the Tablestore console.

  1. Log on to the Tablestore console.
  2. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance that you want to manage.
  3. In the Tables section of the Instance Details tab, click the name of the required data table.
  4. On the Trigger tab, click Use Existing Function Compute.
  5. In the Create Trigger dialog box, select a Function Compute service for FC Service and a Function Compute function for FC Function, enter a trigger name, and then click OK.