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:
|
toNumber(Object) | Converts an Object value to a Number value. The parameter supports the following value types:
|
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:
|
toArray(Object) | Converts an Object value to an Array value. The parameter supports the following value types:
|
toBinary(Object) | Converts an Object value to a binary value. The parameter supports the following value types:
|
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.
|
to_timestamp(dateString, pattern, timeZone) | Converts a time string in a specified format to a millisecond timestamp. Returns a numeric timestamp.
|
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 The tag can be up to 128 characters in length. You can enter a constant or a variable.
| |
writeDatahub(destinationId, data) | Forwards data to DataHub. data: The data to write to DataHub. Only Map and Binary data types are supported. | |
writeFc(destinationId, data) | Forwards data to Function Compute (FC). data: The data to forward to 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. | |
writeMns(destinationId, payload) | Forwards data to Simple Message Queue (formerly MNS) (SMQ). | |
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.
| |
writeTableStore(destinationId, data, flowType) | Forwards data to a Tablestore wide table model.
| |
writeTableStoreTs(destinationId, timeInUs, metricName, tags, fields, dataSource) | Forwards data to a Tablestore time series model.
This function expression writes a single row of data to Tablestore. You can use the following expression to write multiple rows of data: For more information about the parameters, see 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. | |
updateRds(destinationId, data, condition, limit) | Forwards data to ApsaraDB RDS. Updates existing data in a database table.
For example:
| |
writeTsdb(destinationId, timestamp, metricName, value, tag) | Forwards data to Time Series Database (TSDB).
| |
writeIotTopic(destinationId, topic, payload, deviceName) | Forwards data to another topic.
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. | |
writeLindorm(destinationId, timestamp, tags, fields) | Forwards 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. |
Functions supported by basic data types
Map type
Expression
Description
[Object]
Gets the
Valuefor a specifiedKey.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
startposition to theend-1position. Ifendis 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 |
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: Function examples:
|
payload(textEncoding) | Returns the escaped data of the payload that a device publishes. textEncoding specifies the character encoding for escaping the payload. Valid values:
|
parseNumber(numberString, radix) | Converts the string numberString to a number in the specified radix. radix is an optional parameter. Valid values:
For example: |
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.