After a Tablestore trigger is created, you can write the code of a function. This topic describes how to write a function in the Function Compute console.

Prerequisites

Create a trigger

Write function code in Python

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Service/Function.
  4. Locate the target function in the target service and click the function name.
  5. Click Code tag, and write code in the code editor.
    In this example, the function code is written in Python. For more information about how to write function code in other runtime environments, see Use Tablestore to trigger Function Compute in Node.js, PHP, Java, and C# runtimes.
    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'

Event format

A Tablestore trigger encodes incremental data in CBOR format to form an event in Function Compute. The data format of incremental data is as follows:

{
    "Version": "string",
    "Records": [
        {
            "Type": "string",
            "Info": {
                "Timestamp": int64
            },
            "PrimaryKey": [
                {
                    "ColumnName": "string",
                    "Value": formated_value
                }
            ],
            "Columns": [
                {
                    "Type": "string",
                    "ColumnName": "string",
                    "Value": formated_value,
                    "Timestamp": int64
                }
            ]
        }
    ]
}
  • Version: the version of the payload of the data. The current version is Sync-v1. The value is of type "String."
  • Records: an array of incremental data rows in the data table.
    • Type: the type of the data row. Valid values: PutRow, UpdateRow, and DeleteRow. The value is of type "String."
    • Info: the basic information about the data row.
      • Timestamp: the last time when the row was modified. The time must be in UTC. The value is of type "Int64."
    • PrimaryKey: an array of primary key columns.
      • ColumnName: the name of the primary key column. The value is of type "String."
      • Value: the content of the primary key column. The value is of type "formatted_value," which can be Integer, String, or Blob.
    • Columns: an array of attribute columns.
      • Type: the type of the attribute column. Valid values: Put, DeleteOneVersion, and DeleteAllVersions. The value is of type "String."
      • ColumnName: the name of the attribute column. The value is of type "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 last time when the attribute column was modified. The time must be in UTC. The value is of type "Int64."

The sample code of an event is as follows.

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

What to do next

Debug a function