Table Storage Triggers java runtime Function Computing Processing Example Tutorial

Preface

Function Compute is an event-driven service. Through function computing, users do not need to manage the operation of servers, but only write code and upload it. Function computing prepares computing resources and runs user code in a flexible and scalable manner, while users only need to pay for the resources consumed by running the actual code. Table Store Stream is a data channel for obtaining incremental data in Table Store tables. By creating a Table Store trigger, it can realize automatic docking between Table Store Stream and function calculation, thus realizing customized automatic processing when table data in table storage changes. Specific can be seen Form Storage Trigger Function Computing Official Tutorial But the official tutorial only has code examples in the python version, which shows code examples in the java version.

Specific process

The table stores the event parameter passed to the function in cbor format, which 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
                }
            ]
        }
    ]
}

1, reference Function Computing java runtime tutorial To create a java8 runtime function, this tutorial uses jackson Library Which is related to cbor processing;

Note: The third-party packages you depend on should also be packaged together for reference. java function calculations using custom packages

The following shows the configuration and code:

<dependencies>
        <dependency>
            <groupId>com.aliyun.fc.runtime</groupId>
            <artifactId>fc-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-cbor</artifactId>
            <version>2.8.8</version>
        </dependency>
</dependencies>
package example;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.Credentials;
import com.aliyun.fc.runtime.StreamRequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.databind.JsonNode;


public class HelloFC implements StreamRequestHandler {
    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
            throws IOException {

        CBORFactory f = new CBORFactory();
        ObjectMapper mapper = new ObjectMapper(f);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        JsonNode node = mapper.readTree(inputStream);
        System.out.println("Version: " + node.get("Version"));

        JsonNode records = node.get("Records");

        if (records.isArray())
        {
            for (JsonNode record : records)
            {

                System.out.println("Type: " + record.get("Type"));

                JsonNode Info = record.get("Info");
                System.out.println("Timestamp: " + Info.get("Timestamp"));

                JsonNode PrimaryKey = record.get("PrimaryKey");
                if (PrimaryKey.isArray())
                {
                    for (JsonNode objNode : PrimaryKey)
                    {
                        System.out.println(objNode.get("ColumnName") + ": " + objNode.get("Value"));
                    }
                }

                JsonNode Columns = record.get("Columns");
                if (Columns.isArray())
                {
                    for (JsonNode objNode : Columns)
                    {
                        System.out.println(objNode.get("Type") + "  " + objNode.get("ColumnName") + ": " + objNode.get("Value") + "  >>" + objNode.get("Timestamp"));
                    }
                }
            }
        }

        outputStream.write(new String("ok").getBytes());
    }

2, reference Form Storage Trigger Function Computing Official Tutorial Create the corresponding ots instance and table, and configure the trigger of the table is a function created in 1. The final effect is as follows:

3, use Form Storage Client perhaps Table Storage Operational Table Code Data operations are performed on the corresponding tables.
I finally show the effect as follows:

The service log that needs the configuration function can be written to the logstore. Function Computing Access Log Service

Keywords: Java Python

Added by stick_figure on Sun, 12 May 2019 09:02:02 +0300