After you create a Tablestore trigger, the function is automatically triggered to
process data based on your configurations when the data is updated in Tablestore.
This topic describes how to configure a Tablestore trigger for a function in the Function Compute console, including how to create a trigger, configure input parameters, write function
code, and test the function. In this example, a Tablestore trigger is used to trigger
the function to obtain data.
Prerequisites
- Function Compute
- Tablestore
Step 1: Create a Tablestore trigger
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services and Functions.
- In the top navigation bar, select the region where the service resides.
- On the Services page, find the desired service and click Functions in the Actions column.
- On the Functions page, click the function that you want to manage.
- 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.
- In the Create Trigger panel, enter the relevant information. Then, click OK.
Field |
Description |
Example |
Trigger Type |
Select Tablestore.
|
Tablestore |
Name |
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 of a service, 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 After you configure the preceding parameters, click OK. 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.
Step 2: Configure the input parameter of the function
- On the function details page, click the Code tab and click the
icon. From the drop-down list that appears, select Configure Test Parameters.
- 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 Tablestore trigger encodes incremental data in Concise Binary Object Representation (CBOR) format to construct the event parameter of Function Compute. The following 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 the event parameter.
Field |
Description |
Version |
The version of the payload. Example: Sync-v1. The value is a 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. The value is a string.
- Info: includes the Timestamp field, which specifies the time when the row was last
modified. The value of Timestamp must be in UTC. The value is of 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. 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 value of Timestamp
must be in UTC. The value is of type "Int64."
|
Step 3: 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 the data in Tablestore is updated.
- On the function details page, click the Code tab, enter function code in the code editor, and then click Save.
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'
- On the function details page, click the Code tab and click Test Function.
After the function is executed, you can view the result on the Code tab.
References
In addition to the Function Compute console, you can configure triggers by using the
following methods:
- Use Serverless Devs to configure triggers. For more information, see Serverless Devs.
- Use SDKs to configure triggers. For more information, see Supported SDKs.
To modify or delete an existing trigger, see Manage triggers.