All Products
Search
Document Center

Function Compute (2.0):Tablestore triggers

Last Updated:Feb 01, 2024

Tablestore is a distributed NoSQL data storage service that is built on top of the Apsara system. You can create a Tablestore trigger to connect Tablestore as an event source of Function Compute. A function in Function Compute is automatically triggered to process Tablestore data when specified events occur..

Scenarios

The following figure shows a typical scenario of Tablestore triggers.

image

A data source stores data to Table A. The data update triggers a function to cleanse the data and store the cleansed data to Table B for direct reads. The entire process is an elastic and scalable serverless web application.

Before you start

Limits

  • Tablestore triggers are supported in the following regions: China (Beijing), China (Hangzhou), China (Shanghai), China (Shenzhen), Japan (Tokyo), Singapore, Australia (Sydney), Germany (Frankfurt), and China (Hong Kong).

  • The Tablestore table must reside in the same region as the associated Function Compute service.

  • If you want to access the function that is associated with a Tablestore trigger over an internal network, you can use a VPC endpoint of Tablestore, which is in the following format: {instance}.{region}.vpc.tablestore.aliyuncs.com. Do not use a Tablestore internal endpoint in this case.

  • The execution duration of the triggered function cannot exceed one minute.

Usage notes

  • Avoid invocation loops when you write function code. In an invocation loop, Table A triggers Function B to update data in Table A, which in turn triggers Function B again.

  • If an error occurs during the execution of a function, the function keeps retrying until the log data in Tablestore expires.

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. In the top navigation bar, select a region.

  3. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column.

  4. In the Tables section of the Instance Details tab, click the name of the required data table and then click the Tunnels tab. Alternatively, you can click the fig_001 icon and then click Tunnels.

  5. On the Tunnels tab, click Enable in the Stream Information section.

  6. In the Enable Stream dialog box, configure the Log Expiration Time parameter and click Enable.

    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.

    Important

    The Log Expiration Time parameter cannot be modified after it is set. Proceed with caution.

Step 2: Create a Tablestore trigger

  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, find the desired service and click Functions in the Actions column.
  5. On the Functions page, click the function that you want to manage.
  6. On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  7. In the Create Trigger panel, configure parameters and click OK.

    Parameter

    Description

    Example

    Trigger Type

    Select Tablestore.

    Tablestore

    Parameter

    Enter a custom trigger name.

    Tablestore-trigger

    Version or Alias

    The default value is LATEST. If you want to create a trigger for another version or alias, select a version or alias from the Version or Alias drop-down list on the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    Instance

    Select a Tablestore instance from the drop-down list.

    d00dd8xm****

    Table

    Select a table from the drop-down list.

    mytable

    Role Name

    Select AliyunTableStoreStreamNotificationRole.

    Note

    If this is the first time that you create a trigger of this type, click Authorize Now in the message that appears.

    AliyunTableStoreStreamNotificationRole

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Manage triggers.

Step 3: Configure the input parameter of the function

  1. On the function details page, click the Code tab, click the xialatubiao icon next to Test Function, and select Configure Test Parameters from the drop-down list.

  2. In the Configure Test Parameters panel, select the Create New Test Event or Modify Existing Test Event tab, configure Event Name and the event content in the code editor, and then click OK.

    The Tablestore trigger encodes incremental data in Concise Binary Object Representation (CBOR) format to construct the event parameter of Function Compute. The following sample code provides an 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
                    }
                ]
            }
        ]
    }

    The following table describes the fields in event.

    Field

    Description

    Version

    The version of the payload. Example: Sync-v1. The value is a string.

    Records

    The array that stores the rows of incremental data in the 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. The value is a string.

    • Info: the information about the row, including the Timestamp parameter, which specifies the time when the row was last modified. The time must be in UTC. The value is of type "Int64."

    PrimaryKey

    The array that stores primary key columns. This parameter includes the following fields:

    • ColumnName: the name of the primary key column. The value is a string.

    • Value: the content of the primary key column. The value is of type formated_value, which can be Integer, String, or 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. The value is a string.

    • ColumnName: the name of the attribute column. The value is a string.

    • Value: the content of the attribute column. The value is of type formatted_value, which can be Integer, Boolean, Double, String, or Blob.

    • Timestamp: the time when the attribute column was last modified. The time must be in UTC. The value is of type "Int64."

Step 4: Write and test the function

After you create the Tablestore trigger, you can write function code and test the function to verify whether the code is correct. Functions are automatically triggered when data in Tablestore is updated.

  1. On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.

    In this example, the function code is written in Python. For more information about how to write a function in other runtime environments, see Examples of calling Function Compute by using a Tablestore trigger.

    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. On the Code tab, click Test Function.

    After the function is executed, you can view the result on the Code tab.

References

  • If you fail to create a Tablestore trigger in a specific region, check whether the region supports Tablestore triggers. For more information, see Limits.

  • If you cannot find a created Tablestore table when you create a Tablestore trigger, check whether the table resides in the same region as the associated Function Compute service.

  • In most cases, if an error that indicates a client cancels invocation is repeatedly reported when you use a Tablestore trigger, the timeout period configured for function execution on the client is shorter than the function execution duration. In this case, we recommend that you increase the client timeout period. For more information, see What do I do if the client is disconnected and the message "Invocation canceled by client" is reported?

  • If data in added to a Tablestore table but the associated Tablestore trigger is not triggered, you can troubleshoot the issue by performing in the following steps. For more information about how to troubleshoot trigger failures, see What do I do if a trigger cannot trigger function execution?

    • Check whether the Stream feature is enabled for the table. For more information, see Step 1: Enable the Stream feature for the data table.

    • Check whether the correct role was configured when you created the trigger. You can use the default trigger role AliyunTableStoreStreamNotificationRole. For more information, see Step 2: Create a Tablestore trigger.

    • Check the function run logs to see whether the function failed to be executed. If a function fails to be executed, the function is retried until the log data in Tablestore expires.