Enhanced JS Built-in Function
Enhanced JS nodes allow you to utilize all built-in functions for external calls, such as networking and database operations. If your requirement is solely to process and operate on data records, it is recommended to use standard JS nodes.
For detailed instructions on how to use enhanced JS nodes and explore various scenarios, please refer to the documentation and resources available for JS processing node.
This feature is only supported for use in data transformation tasks.
DateUtilβ
parseβ
Description: Converts date strings in various formats to Date.
Example:
-
General Usage
var dte = DateUtil.parse('2010-01-01 00:00:00'); -
Advanced usage:
parse(dateString, timeoffset), that is, specify the time zone offset while converting.// UTC+08:00
var dte = DateUtil.parse('2010-01-01 00:00:00', 8);
// UTC+0
var dte = DateUtil.parse('2010-01-01 00:00:00', 0);
determineDateFormatβ
Description: Get the date format.
Example:
var format = DateUtil.determineDateFormat('2010-01-01 00:00:00');
timeStamp2Dateβ
Description: Converts the timestamp to a date string in the specified format.
Example:
var dteStr = DateUtil.timeStamp2Date(1592233019140, 'yyyy-MM-dd HH:mm:ss');
addYears/addMonths/addDays/addHours/addMinutes/addSecondsβ
Description: Adds or subtracts the year/month/day/hour/minute/second of the date.
Example:
var dte = DateUtil.addYears(new Date(), 1);
dte = DateUtil.addYears(dte, -1);
sameYear/sameMonth/sameDay/sameHour/sameMinute/sameSecondβ
Description: Compares the year/month/day/hour/minute/second of the date.
Example:
if ( DataUtil.sameYear(new Date(), new Date()) ) {
...
}
idGen/UUIDGeneratorβ
uuidβ
Description: Generate uuid, if you use var str = uuid();, you can get a random string.
Example:
// Both methods below are available
var uuid = idGen.uuid();
var uuid = UUIDGenerator.uuid();
objectIdβ
Description: Generate MongoDB ObjectId.
Example:
// Both methods below are available
var oid = idGen.objectId();
var oid = UUIDGenerator.objectId();
objectIdStrβ
Description: Generate MongoDB ObjectId String section.
Example:
// Both methods below are available
var oidStr = idGen.objectIdStr();
var oidStr = UUIDGenerator.objectIdStr();
networkUtilβ
GetAddressβ
Description: Network tool to get the IP address or MAC address.
Example:
// Get the MAC address of the first network interface
var mac = networkUtil.GetAddress("mac");
// Get IP address
var ip = networkUtil.GetAddress("ip");
HashMapβ
put/removeβ
Description: Hash dictionary.
Example:
var map = new HashMap();
map.put(βnameβ, βtestβ);
map.remove(βnameβ);
ArrayListβ
add/removeβ
Note: Array type.
Example:
var list = new ArrayList();
list.add(βtest1β);
list.remove(0);
Dateβ
add/removeβ
Description: Date type.
Example:
var dte = new Date();
var year = dte.getYear()+1900;
ScriptExecutorsManagerβ
getScriptExecutorβ
Description: Get the data source executor.
Example:
var source = ScriptExecutorsManager.getScriptExecutor('mysql-connection-name');
ScriptExecutorβ
Preparation (Optional)
For demonstration purposes, we executed the following SQL statements in the database to create an Orders table and procedures to simulate real business scenarios.
-- 1. Create Orders table
CREATE TABLE Orders (
order_id VARCHAR(20) PRIMARY KEY,
order_date DATETIME,
total_amount DECIMAL(10, 2),
status INT DEFAULT 0, -- 0: Pending, 1: Paid, 2: Shipped, 9: Closed
points INT DEFAULT 0 -- Order points
);
-- Initialize test data
INSERT INTO Orders (order_id, order_date, total_amount, status, points) VALUES
('ORD_001', NOW(), 100.00, 0, 0),
('ORD_002', NOW(), 5000.00, 1, 0),
('ORD_003', '2023-01-01 10:00:00', 50.00, 0, 0); -- An old record to demonstrate auto-closing
-- 2. Procedure 1 (No parameters): Batch close expired orders
-- Function: Simulate a scheduled task to mark all unpaid orders older than 30 days as '9' (Closed)
DELIMITER $$
CREATE PROCEDURE sp_close_expired_orders()
BEGIN
UPDATE Orders SET status = 9 WHERE status = 0 AND order_date < DATE_SUB(NOW(), INTERVAL 30 DAY);
END$$
DELIMITER ;
-- 3. Procedure 2 (With input parameters): Ship order
-- Function: Update the status of a specific order to '2' (Shipped)
DELIMITER $$
CREATE PROCEDURE sp_ship_order(IN p_order_id VARCHAR(20))
BEGIN
UPDATE Orders SET status = 2 WHERE order_id = p_order_id;
END$$
DELIMITER ;
-- 4. Procedure 3 (Complex scenario): Calculate order points (With IN/OUT parameters)
-- Function: Input order amount, return points earned
-- Rule: Amount < 1000, 1 point per 10 currency units; Amount >= 1000, 2 points per 10 currency units
DELIMITER $$
CREATE PROCEDURE sp_calculate_points(
IN p_amount DECIMAL(10,2),
OUT p_points INT
)
BEGIN
IF p_amount < 1000 THEN
SET p_points = FLOOR(p_amount / 10);
ELSE
SET p_points = FLOOR(p_amount / 10) * 2;
END IF;
END$$
DELIMITER ;
execute / executeQueryβ
Description: Obtain a script executor for a specific data source via ScriptExecutorsManager, then call this method to execute SQL statements or NoSQL operations. This method is recommended for simple scenarios.
- executeQuery: Primarily used for queries (SELECT). Returns an array (result set) and supports trial runs (data preview).
- execute: Used to execute SQL statements. It can return multiple result sets, equivalent to the behavior of standard database tools. While suitable for retrieving result sets in simple query scenarios, note that this method does not support data preview during trial runs.
The object before execute or executeQuery determines the target database: source for the source database and target for the target database.
Structured Databases (e.g., MySQL)β
Examples:
-
Using execute for DML
// Get source connection (replace with your actual source connection name, e.g., 'Source_MySQL')
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Simple SQL execution: update orders with status 0 (Pending) to 9 (Closed)
var result = source.execute({
"sql": "UPDATE Orders SET status = 9 WHERE order_id = 'ORD_001'"
});
log.info("Update result: " + result); -
Using executeQuery for queries
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Query all high-value orders (amount > 1000)
var result = source.executeQuery({
"sql": "SELECT * FROM Orders WHERE total_amount > 1000"
});
log.info("High value orders: " + result); -
Using execute to call procedures (no parameters)
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Call the procedure to batch close expired orders
// Equivalent to source.call("sp_close_expired_orders", [])
var result = source.execute({
"sql": "CALL sp_close_expired_orders()"
}); -
Using execute to call procedures (with parameters)
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Call the shipping procedure (manually construct SQL parameters)
// Equivalent to source.call("sp_ship_order", [...])
var result = source.execute({
"sql": "CALL sp_ship_order('ORD_002')"
});
execute and executeQuery do not support retrieving OUT (output) or INOUT (input/output) parameters from procedures, nor do they support non-query return values. For such complex scenarios, please use the call method described below.
NoSQL Databases (e.g., MongoDB)β
Parameter Description:
- database: The name of the database to operate on.
- collection: The name of the collection to operate on.
- op: The operation to perform (INSERT/UPDATE/DELETE,
executeonly). - filter: Conditions for query, update, or delete.
- opObject: The specific data to insert or update.
- upsert: Whether to use MongoDB's UPSERT mode (insert if not exists). Default is false.
- multi: Whether to update multiple records. Default is false.
- sort: Sorting conditions (
executeQueryonly). - limit: Limit the number of output records (
executeQueryonly).
Examples:
-
Using execute for updates
var result = target.execute({
database: "test",
collection: "user",
op: "update",
filter: {id: 1},
opObject: {name: "user001", age: 20},
upsert: true
}); -
Using executeQuery for queries
var users = target.executeQuery({
database: "test",
collection: "user",
filter: {age: {$gt: 10}},
sort: {age: -1},
limit: 10
});
callβ
Description: Call custom procedures in the database, supporting complex input/output parameters and return values. This method is recommended for procedures involving multiple result sets or complex parameters (IN/OUT/RETURN).
This method is based on the JDBC generic interface. It is recommended to use basic parameter types (such as int, double, varchar) and avoid database-specific complex types to maximize compatibility across different database systems.
Parameter Description:
call(procedureName, parameters)
- procedureName: The name of the procedure.
- parameters: An array of parameters, strictly ordered, containing the following properties:
- mode: Parameter mode. Options:
in|out|in/out|return. - type: Data type. Supports common types like
int|double|varchar. - value: The specific value of the input parameter.
- name: Parameter name (optional; if specified, this name will be used as the key in the returned result; otherwise, it is auto-generated).
- mode: Parameter mode. Options:
Examples:
-
Simple call (no parameters)
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Call parameterless procedure: batch close expired orders
source.call("sp_close_expired_orders", []); -
Call with input parameters
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Call procedure with parameters: Ship order (update status of order ORD_002 to 2)
source.call("sp_ship_order", [
{"mode": "in", "type": "varchar", "value": 'ORD_002'}
]); -
Complex call (with return values/output parameters)
var source = ScriptExecutorsManager.getScriptExecutor('Source_MySQL');
// Scenario: Calculate order points
// Input: Order amount 5000 (in)
// Output: Points earned (out)
var result = source.call("sp_calculate_points", [
{"mode": "in", "type": "decimal", "value": 5000.00},
{"name": "points", "mode": "out", "type": "int"}
]);
// Result returned as a Map
// Example: {points=1000}
log.info("Points earned: " + result.points);
JSONUtilβ
json2List/obj2Json/obj2JsonPretty/json2Mapβ
Description: JSON format conversion.
Example:
var d = new Date();
var json = JSONUtil.obj2Json(d)
HanLPUtilβ
hanLPParticipleβ
Description: Chinese word segmentation tool, two parameters need to be set in parentheses, the format is (String inputString, String language).
Example:
var d = HanLPUtil.hanLPParticiple('δ½ ε₯½', 'HK_T')
Parameter Description
-
inputString: A string that requires word segmentation.
-
language: the type of the language with the word segmentation, support:
-
CH_S: Simplified Chinese.
-
CH_T: Traditional Chinese.
-
HK_T: Traditional Chinese (Hong Kong).
-
TW_T: Traditional Chinese (Taiwan).
-
Returns: Array type, that is, the result set after word segmentation.
split_chineseβ
Description: Chinese word segmentation tool, two parameters need to be set in parentheses, the format is (String inputString, String language).
Example:
var strs = split_chinese("θΏζ―δΈδΈͺδΈζε₯ε", "CH_S");
Parameter Description
-
inputString: A string that requires word segmentation.
-
language: the type of the language with the word segmentation, support:
-
CH_S: Simplified Chinese.
-
CH_T: Traditional Chinese.
-
HK_T: Traditional Chinese (Hong Kong).
-
TW_T: Traditional Chinese (Taiwan).
-
Returns: Array type, that is, the result set after word segmentation.
utilβ
strToBase64/base64ToStr/unwindβ
Description: String format conversion.
Example:
// Convert the string to Base64 format
var b = util.strToBase64('aa');
// Split JSON arrays into hierarchy levels
var list = util.unwind(map, 'a.b.c');
MD5Util/MD5β
Description: MD5 encryption tool.
Example:
// Get the MD5 signature of a string, the second parameter indicates whether to convert it to uppercase
var b = MD5Util.crypt('aa', true);
// Or
var b = MD5('aa', true);
Collectionsβ
sort/get/emptySet/emptyListβ
Description: Collection tool classes, such as sorting, getting collections, etc.
Example:
// Sort the List
Collections.sort(list);
// Get an empty collection
var set = Collections.emptySet();
MapUtilβ
getValueByKey/needSplit/removeValueByKey/containsKey/getValuePositionInMap/deepCloneMap/copyToNewMap/putValueInMap/recursiveFlatMap/obj2Mapβ
Description: Dictionary tool class.
Example:
// Get the value of a specified level from a given map
var a = MapUtil.getValueByKey(map, 'a.b.c');
sleepβ
Description: The program sleeps for a specified duration, measured in milliseconds.
Example:
// Sleep for 10 milliseconds in the program
sleep(10);
restβ
get/post/patch/deleteβ
Description: Call HTTP methods (such as Get), format reference:
rest.get(url, header)
rest.get(url, header, returnType)
rest.get(url, header, connectTimeOut, readTimeOut)
rest.get(url, header, returnType, connectTimeOut, readTimeOut)
- returnType: The default return result type is an array.
- connectTimeOut: The connection timeout duration in milliseconds. The default value is 10,000 milliseconds (10 seconds).
- readTimeOut: The read timeout duration in milliseconds. The default value is 30,000 milliseconds (30 seconds).
Example:
-
Get
var result = rest.get('http://127.0.0.1:1234/users?where[user_id]=1', {status: 0}, {}, 30, 300);rest.get(url)
rest.get(url, headers)
rest.get(url, connectTimeOut, readTimeOut)
rest.get(url, headers, connectTimeOut, readTimeOut) -
Post
var result = rest.post('http://127.0.0.1:1234/users?id=1', {}, '[array/object/string]', 30, 300);rest.post(url, parameters)
rest.post(url, parameters, headers, returnType)
rest.post(url, parameters, connectTimeOut, readTimeOut)
rest.post(url, parameters, headers, returnType, connectTimeOut, readTimeOut) -
Patch
var result = rest.patch('http://127.0.0.1:1234/users/find', {}, {}, '[array/object/string]', 30, 300);rest.patch(url, parameters)
rest.patch(url, parameters, headers)
rest.patch(url, parameters, connectTimeOut, readTimeOut)
rest.patch(url, parameters, headers, connectTimeOut, readTimeOut) -
Delete
var result = rest.delete('http://127.0.0.1:1234/users?where[user_id]=1', {}, 30, 300);
mongoβ
getData/insert/update/deleteβ
Description: Add, delete and check data in MongoDB, format reference:
mongo.getData(uri, collection)
mongo.getData(uri, collection, filter)
mongo.getData(uri, collection, filter, limit, sort)
Example:
-
Query Data
var result = mongo.getData('mongodb://127.0.0.1:27017/test', 'users', {id: 1}, 10, {add_time: -1});mongo.insert(url, collection, inserts) -
Insert data, supporting input of arrays or objects.
mongo.insert('mongodb://127.0.0.1:27017/test', 'users', [{id: 1, name: 'test1'}, {id: 2, name: 'test2'}]);mongo.update(url, collection, filter, update) -
Update data
var modifyCount = mongo.update('mongodb://127.0.0.1:27017/test', 'users', {id: 1}, {name: 'test3'});mongo.delete(url, collection, filter) -
Delete Data
var deleteCount = mongo.delete('mongodb://127.0.0.1:27017/test', 'users', {id: 1});