Skip to main content

Custom Script Functions

Scripts allow you to write functions in JavaScript and execute them within queries for a stream. Functions defined with scripts can be accessed in queries similar to any other inbuilt function. You can use function definitions to define these scripts.

Function parameters are passed into the function logic as an Object[] and with the name data.

Purpose

Scripts allow you to define a function operation that is not provided in stream worker built-in functions. You do not need to write an extension to define the function logic.

Syntax

The syntax for a script definition is as follows.

define function <function name>[<javascript>] return <return type> {
<operation of the function>
};

The defined function can be used in the queries similar to inbuilt functions as follows.

<function name>( (<function parameter>(, <function parameter>)*)? )

The custom functions defined with these function definitions have higher precedence compared to other functions.

Parameters

The following parameters are configured when defining a script.

ParameterDescription
function nameThe name of the function. By convention, camelCase is used for the function name.
language nameThe name of the programming language used to define the script. Currently, Macrometa supports javascript.
return typeThe attribute type of the function’s return. This can be int, long, float, double, string, bool, or object. Here the function implementer should be responsible for returning the output attribute on the defined return type for proper functionality.
operation of the functionHere, the execution logic of the function is added. This logic should be written in the language specified under the language name, and it should return the output in the data type specified in the return type parameter.

Example 1

CREATE function concatFn[javascript] return string {
var str1 = data[0];
var str2 = data[1];
var str3 = data[2];
var response = str1 + str2 + str3;
return response;
};

CREATE STREAM TempStream(deviceID long, roomNo int, temp double);
CREATE STREAM DeviceTempStream (id string, temp double);

insert into DeviceTempStream
select concatFn(roomNo,'-',deviceID) as id, temp
from TempStream;

This function performs concatenation using JavaScript and returns the output as a string.