groovy
Runs a user-defined Groovy script to process input records. Specify the name of a Groovy script deployed on the server, and the script's onRow() method is called for each record.
Command properties
| Property | Value |
|---|---|
| Command type | Transforming |
| Required permission | None |
| License usage | N/A |
| Parallel execution | Supported |
| Distributed execution | Runs on Data Node (mapper) |
Syntax
Target
SCRIPT_NAME- The name of the Groovy script to run. Loads the
SCRIPT_NAME.groovyfile stored in thequery_scripts/directory. The script must extend theGroovyQueryScriptabstract class and implement theonRow()method.
Error codes
Parsing errors
| Error code | Message | Description |
|---|---|---|
| groovy-script-failure | (dynamic message) | An error occurred while loading or instantiating the script. Occurs when the script file is missing or has a compilation error. |
Runtime errors
N/A
Description
The groovy command loads and runs a Groovy script deployed in the query_scripts/ directory. The script must extend the GroovyQueryScript abstract class. Override the onRow(Row row) method to process input records. Call pipe.onRow(row) to pass a processed record to the next command.
If the script implements the ThreadSafe interface, it runs in parallel without synchronization. Otherwise, execution is serialized using a synchronized block.
The script lifecycle methods are:
onStart(): Called once when the query starts.onRow(Row row): Called for each input record.onRowBatch(RowBatch rowBatch): Called for each batch of records. The default implementation callsonRow()for each record in the batch.onClose(): Called once when the query ends.
Examples
-
Run a Groovy script
json "[{'msg': 'Hello'}, {'msg': 'World'}]" | groovy my_transformLoads the
query_scripts/my_transform.groovyscript and processes each record. -
Groovy script example
The following is an example of
query_scripts/add_length.groovy. It calculates the length of themsgfield and assigns it to themsg_lenfield.import org.araqne.logdb.groovy.GroovyQueryScript import org.araqne.logdb.Row class AddLength extends GroovyQueryScript { void onRow(Row row) { def msg = row.get("msg") if (msg != null) row.put("msg_len", msg.toString().length()) pipe.onRow(row) } }After deploying this script, run it as follows.
json "[{'msg': 'Hello'}, {'msg': 'World'}]" | groovy add_lengthA
msg_lenfield is added to each record.