Using a DDS Trigger

This section describes how to create a DDS trigger for a function, and invoke the function when a database table changes.

A function using a DDS trigger will be triggered every time a database table is updated. For details about the DDS event source, see Supported Event Sources.

Prerequisites

Before creating a DDS trigger, make sure you have prepared the following:

Creating a DDS Trigger

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the HelloWorld function.
  3. On the displayed function details page, click the Triggers tab.
  4. Click Create Trigger.
  5. Set the following parameters:

    • Trigger Type: Select Document Database Service (DDS).
    • DB Instance: Select a DDS DB instance.
    • Password: Enter the password of DDS DB instance administrator rwuser.
    • Database: Enter the name of a database. Note that admin, local, and config are reserved database names and cannot be used here.
    • Collection: Enter the name of a database collection.
    • Batch Size: Set the number of records to be read from the database at a time.

  6. Click OK.

Configuring a DDS Event to Trigger the Function

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the HelloWorld function.
  3. On the displayed function details page, select a version and choose Select test event > Configure test event.
  4. Set the parameters described in Table 1 and click Save.

    Table 1 Test event information

    Parameter

    Description

    Configure Test Event

    You can choose to create a test event or edit an existing one.

    Use the default option Create new test event.

    Event Template

    Select dds-event-template.

    Event Name

    The event name can contain 1 to 25 characters and must start with a letter and end with a letter or digit. Only letters, digits, underscores (_), and hyphens (-) are allowed. For example, dds-123test.

    Event data

    The system automatically loads the built-in DDS event template, which is used in this example without modifications.

    The event template is as follows:

    {
    	"records": [
    		{
    			"event_source": "dds",
    			"event_name": "insert",
    			"region": "xx-xxxxx-1",
    			"event_version": "1.0",
    			"dds": {
    				"size_bytes": "100",
    				"token": {
                        "_data": "825D8C2F4D0000001529295A100474039A3412A64BA89041DC952357FB4446645F696400645D8C2F8E5BECCB6CF5370D6A0004"
                    },
    				"full_document": {
    					"_id": {
    						"$oid": "5d8c2f8e5beccb6cf5370d6a"
    					},
    					"name": "dds",
    					"age": {
    						"$numberDouble": "52.0"
    					}
    				},
    				"ns": {
    				   "db": "functiongraph",
    				   "coll": "person"
    				}
    			},
    			"event_source_id": "e6065860-f7b8-4cca-80bd-24ef2a3bb748"
    		}
    	]
    }

  5. Click Test. The function test result is displayed, as shown in Figure 1.

    Figure 1 Test result