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

PropertyValue
Command typeTransforming
Required permissionNone
License usageN/A
Parallel executionSupported
Distributed executionRuns on Data Node (mapper)

Syntax

groovy SCRIPT_NAME

Target

SCRIPT_NAME
The name of the Groovy script to run. Loads the SCRIPT_NAME.groovy file stored in the query_scripts/ directory. The script must extend the GroovyQueryScript abstract class and implement the onRow() method.

Error codes

Parsing errors
Error codeMessageDescription
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 calls onRow() for each record in the batch.
  • onClose(): Called once when the query ends.

Examples

  1. Run a Groovy script

    json "[{'msg': 'Hello'}, {'msg': 'World'}]"
    | groovy my_transform
    

    Loads the query_scripts/my_transform.groovy script and processes each record.

  2. Groovy script example

    The following is an example of query_scripts/add_length.groovy. It calculates the length of the msg field and assigns it to the msg_len field.

    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_length
    

    A msg_len field is added to each record.