Function list

更新时间:
复制 MD 格式

When you configure data forwarding, you can use various functions in your Resolver scripts to process data. These functions cover data type conversion, time type conversion, basic data types, and forwarding data to destinations. This topic describes the expressions and usage instructions for these functions.

The new Resolver script for data forwarding also supports functions from the previous version. For more information, see Supported functions for data forwarding.

Data type conversion functions

Expression

Description

toBoolean(Object)

Converts an Object value to a Boolean value. The parameter supports the following value types:

  • Boolean: Returns the corresponding Boolean value.

  • Number: If the parameter is 0, the function returns false. Otherwise, it returns true.

  • String: If the parameter is "true", the function returns true. Otherwise, it returns false.

  • If the parameter value is null, the function returns false.

toNumber(Object)

Converts an Object value to a Number value. The parameter supports the following value types:

  • Boolean: If the parameter is true, the function returns 1. If the parameter is false, the function returns 0.

  • Number: Returns the corresponding numeric value.

  • String: Parses the string as a numeric type.

  • If the parameter value is null, the function returns 0.

toString(Object)

Converts an Object value to a String value.

The parameter can be of any type. The function returns the corresponding String value. If the parameter value is null, the function returns an empty string.

If the parameter is a binary type, the function returns the corresponding value based on UTF-8 encoding.

toMap(Object)

Converts an Object value to a Map value. The parameter supports the following value types:

  • Map: Returns the corresponding value.

  • String: Parses the string as a Map in JSON format.

  • If the parameter value is null, the function returns an empty Map.

toArray(Object)

Converts an Object value to an Array value. The parameter supports the following value types:

  • Array: Returns the corresponding value.

  • String: Parses the string as an Array in JSON format.

  • If the parameter value is null, the function returns an empty Array.

toBinary(Object)

Converts an Object value to a binary value. The parameter supports the following value types:

  • Binary: Returns the corresponding value.

  • String: Returns the value based on UTF-8 encoding.

  • If the parameter value is null, the function returns an empty Binary value.

Time type conversion functions

Function expression

Description

format_date(timestamp, pattern, timeZone)

Converts a millisecond timestamp to a time string in a specified format. Returns a String value.

  • timestamp: The millisecond timestamp.

  • pattern: The time format to which you want to convert. For example, yyyy-MM-dd HH:mm:ss.

  • timeZone: The time zone. For example, GMT, UTC, or CST. Set the time zone. If you do not set this parameter, the default time zone UTC+8 is used.

to_timestamp(dateString, pattern, timeZone)

Converts a time string in a specified format to a millisecond timestamp. Returns a numeric timestamp.

  • dateString: The time string.

  • pattern: The specified time format. For example, yyyy-MM-dd HH:mm:ss.

  • timeZone: The time zone. For example, GMT, UTC, or CST. Set the time zone. If you do not set this parameter, the default time zone UTC+8 is used.

Functions to forward data to data destinations

In the following function expressions, the input parameter destinationId is the data destination ID, and payload is the message payload.

Function expression

Description

Example

writeAmqp(destinationId, payload, tag)

Forwards data to an AMQP consumer group.

tag: Optional. If you set a tag, all messages that are forwarded to the AMQP server-side subscription consumer group by this operation carry this tag.

The tag can be up to 128 characters in length. You can enter a constant or a variable.

  • Constants can contain Chinese characters, letters, and digits.

  • Variables represent the value of a key in the JSON data that is processed by the Resolver script. If the value cannot be retrieved, the message does not carry a tag.

Forward data to an AMQP consumer group

writeDatahub(destinationId, data)

Forwards data to DataHub.

data: The data to write to DataHub. Only Map and Binary data types are supported.

Forward data to DataHub

writeFc(destinationId, data)

Forwards data to Function Compute (FC).

data: The data to forward to FC.

Forward data to Function Compute (FC)

writeKafka(destinationId, payload, key)

Forwards data to Kafka.

key: Optional. The key that is carried in messages forwarded to a Kafka topic.

You can use tags for message routing on the Kafka consumer.

Forward data to Kafka

writeMns(destinationId, payload)

Forwards data to Simple Message Queue (formerly MNS) (SMQ).

Forward data to Simple Message Queue (MNS)

writeMq(destinationId, payload, tag)

Forwards data to RocketMQ.

tag: Optional. If you set a tag, all messages that are forwarded to the corresponding RocketMQ topic by this operation carry this tag. You can use tags for message filtering on the RocketMQ consumer.

The tag can be up to 128 characters in length. You can enter a constant or a variable.

  • Constants can contain Chinese characters, letters, and digits.

  • Variables represent the value of a key in the JSON data that is processed by the Resolver script. If the value cannot be retrieved, the message does not carry a tag.

Forward data to RocketMQ

writeTableStore(destinationId, data, flowType)

Forwards data to a Tablestore wide table model.

  • data: The data to write to Tablestore. Only the Map data type is supported. The key corresponds to the column name and the value corresponds to the column value.

    The data must contain the primary key of the Tablestore table.

  • flowType: Optional. Sets the data type for non-primary key fields.

    • If flowType is set to true, all non-primary key fields are forwarded to Tablestore as the String data type.

    • If flowType is set to false or is not specified, non-primary key fields are forwarded to Tablestore with their corresponding data types.

Forward data to a Tablestore wide table model

writeTableStoreTs(destinationId, timeInUs, metricName, tags, fields, dataSource)

Forwards data to a Tablestore time series model.

  • timeInUs: The timestamp that indicates when the data record was generated. Unit: microseconds. The data must be of the integer type.

  • metricName: The name of the physical quantity or metric that is measured by the time series data. The data must be of the string type.

  • tags: The tag information of the time series. The data must be of the Map type. The key and value of a tag must be of the string type.

  • fields: The time series data. The data must be of the Map type. You can set multiple data values at a time point. Each value corresponds to a column in the database, including the column name and column value.

    The column name must be of the string type. The column value can be of the Boolean, integer, float, string, or binary type.

  • dataSource: The identifier of the data source that generates the time series. The data must be of the string type. Only this parameter can be empty.

This function expression writes a single row of data to Tablestore. You can use the following expression to write multiple rows of data:

writeTableStoreTs(destinationId, [
    {
        "timeInUs": timestamp() * 1000,
        "metricName": "name1",
        "tags":{},
        "fields":{},
        "dataSource": ""
    },
    {
        "timeInUs": timestamp() * 1000,
        "metricName": "name2",
        "tags":{},
        "fields":{},
        "dataSource": ""
    }
]);

For more information about the parameters, see Time series model.

Forward data to a Tablestore time series model

writeRds(destinationId, data)

Forwards data to ApsaraDB RDS. Adds a new record to a table in the database.

data: The data to write to the database table. Only the Map data type is supported. The key corresponds to the column name and the value corresponds to the column value.

Forward data to ApsaraDB RDS

updateRds(destinationId, data, condition, limit)

Forwards data to ApsaraDB RDS. Updates existing data in a database table.

  • data: The data to update in the database table. Only the Map data type is supported. The key corresponds to the column name and the value corresponds to the column value.

  • condition: The condition for updating data. This parameter is required. Only strings are supported. The string is used as the WHERE clause of an SQL statement. Example: a = 3 and b = 3.

  • limit: The maximum number of rows to update. This parameter is optional. The default value is 1. The maximum value is 50.

For example:

  • If multiple rows of data meet the condition, the update operation starts from the first row of the SQL query result. For example, if limit is set to 1, the first row is updated. If limit is set to 10, the first 10 rows are updated.

  • If only one row of data meets the condition, only that row is updated.

writeTsdb(destinationId, timestamp, metricName, value, tag)

Forwards data to Time Series Database (TSDB).

  • timestamp: The timestamp.

  • metricName: The name of the metric stored in TSDB.

  • value: The value of the data point stored in TSDB. The String and Map data types are supported.

  • tag: The key-value pairs of tags that mark the data. The data must be of the Map type.

Forward data to Time Series Database (TSDB)

writeIotTopic(destinationId, topic, payload, deviceName)

Forwards data to another topic.

  • topic: The destination topic for data forwarding. The following topics are supported.

    • Custom topic: The device must have the permission to subscribe to this custom topic to receive the forwarded messages.

    • Topic for setting properties based on a TSL model: /sys/${productKey}/${deviceName}/thing/service/property/set. This topic is used in scenarios where a device needs to change its property values based on forwarded data. The device receives instructions from this topic to set its property values.

    The ${productKey} in the topic must be the same as the product specified for the data destination that corresponds to destinationId. You must specify a specific topic in the function. Wildcard characters are not supported.

    For more information, see Step 1: Create a data destination.

    Important

    If the device that owns the destination topic does not subscribe to the topic, the device cannot receive forwarded messages.

    For example, Device A connects to IoT Platform using MQTT.fx. Device A does not automatically subscribe to device topics. If Device B forwards data to the thing/service/property/set topic of Device A, but Device A has not manually subscribed to this topic, Device A cannot receive the forwarded messages.

    For more information about automatic topic subscription, see Automatic topic subscription.

  • deviceName: For products and devices that use the open source MQTT hosting mode or the MQTT cloud gateway, you must specify this parameter when you forward data to another topic. For other scenarios, you cannot specify this parameter.

    The data must be of the Map type. The key is "deviceName" and the value is deviceName(), which indicates the name of the device that sends the message to the topic.

    For more information, see Communication methods.

Important

For MQTT cloud gateway products and devices, data can be forwarded only to custom topics of the same product. Cross-product data forwarding is not supported.

For MQTT-based instances, you can create only one product. Cross-product data forwarding is not supported by default.

Forward data to other topics

writeLindorm(destinationId, timestamp, tags, fields)

Forwards data to Lindorm.

  • timestamp: The timestamp that indicates when the data record was generated.

  • tags: The tags that indicate the specific object properties for the metric. A tag consists of a tag key and a tag value. The data must be of the Map type.

  • fields: The data record. A data record can have multiple field values, which represent different objects of the metric. The Object data type is supported.

Forward data to Lindorm

writePolarDB(destinationId, data)

Forwards data to the cloud-native database PolarDB for MySQL.

data: The data to write to the database table. Only the Map data type is supported. The key corresponds to the column name and the value corresponds to the column value.

Forward data to the cloud-native database PolarDB for MySQL

Functions supported by basic data types

  • Map type

    Expression

    Description

    [Object]

    Gets the Value for a specified Key.

    size()

    Gets the number of key-value pairs in the Map data.

    containsKey(String)

    Checks whether the Map data contains a specified key.

    keySet()

    Gets the set of keys in the Map data. Returns an Array.

    remove(Object)

    Removes the key-value pair for a specified key from the Map data.

    put(Object, Object)

    Adds a key-value pair to the Map data.

    putAll(map)

    Adds a set of Map data to the Map data in a batch.

  • Array type

    Function

    Description

    [int]

    Gets the value at a specified index. The index of the first element is 0.

    contains(Object)

    Checks whether the array contains a specified element.

    remove(Object)

    Removes a specified element from the array.

    removeAt(int)

    Removes the element at a specified index from the array.

    add(Object)

    Adds an element to the end of the array.

    add(index, Object)

    Adds an element at a specified index.

    addAll(array)

    Adds another array to the array.

    size()

    Gets the number of elements in the array.

  • String type

    Function

    Description

    substring(start, end)

    Extracts a substring from the start position to the end-1 position. If end is not specified, the substring is extracted to the end of the string.

    length()

    Gets the length of the string.

    split(String)

    Splits a string using a separator character.

    startsWith(String)

    Checks whether a string starts with a specified string.

    endsWith(String)

    Checks whether a string ends with a specified substring.

    indexOf(String, index)

    Returns the position of the first occurrence of a specified string, starting from a specified index.

    If index is not specified, the default value is 0.

Other functions

Expression

Description

endWith(input, suffix)

Checks whether the string input ends with the string suffix.

Returns a Boolean value.

productKey()

Returns the ProductKey of the current product.

getDeviceTag(key)

Returns the device tag that corresponds to the specified key. If the device does not have a tag for the key, the function returns an empty value.

getOrNull(data, "items", ......)

Returns the value of a specified field from the JSON data data.

You can specify any number of fields, but you must specify them in hierarchical order. The function returns the value of the last field. If the last field does not exist or its value is empty, the function returns null.

For example, the data parameter contains the following:

"items": {
    "Humidity": {
        "value": 25,
        "time": 1626948134319
    }
}

Function examples:

  • getOrNull(data, "items") returns the value of the items field: "Humidity": {"value": 25,"time": 1626948134319}.

  • getOrNull(data, "items", "Humidity", "value") returns the value of the value field: 25.

  • getOrNull(data, "items", "Temperature"): The Temperature field does not exist. The function returns null.

payload(textEncoding)

Returns the escaped data of the payload that a device publishes. textEncoding specifies the character encoding for escaping the payload. Valid values:

  • If you do not specify this parameter, the payload is converted to a string based on UTF-8 encoding by default. payload() is equivalent to payload('utf-8').

  • 'json': Converts the payload data to a Map variable. If the payload is not in the JSON format, an error is returned.

  • 'binary': Converts the payload data to a binary variable for pass-through transmission.

parseNumber(numberString, radix)

Converts the string numberString to a number in the specified radix.

radix is an optional parameter. Valid values:

  • 10 (default): Converts the string to a decimal number.

  • 2: Converts the string to a binary number.

  • 16: Converts the string to a hexadecimal number.

For example:

// Data reported by the device.
var content = '0xFF';
// Get the string FF to be converted.
var x = content.substring(2);
// Convert FF to a hexadecimal number.
var value = parseNumber(x, 16);

References

  • You can call functions and use the supported script syntax to forward data to data destinations. For more information about how to use scripts, see Script syntax.

  • You can use these functions in the Resolver script when you configure data forwarding to cloud products to process and forward data. For more information, see Configure a Resolver.