JavaScript (job entry)

Description

Use the JavaScript job entry to calculate a boolean expression. The result can be used to determine which step will be executed next. You can use functions, procedure calls, ANDs, ampersands, ORs, EQUALs, etc. The Javascript job entry evaluates and returns a true or false.

Evaluation

The result of a JavaScript job entry is either true or false.  In other words, it needs to end with a boolean expression.

Here are a few possible evaluations to end your script with :

lines_input > 100

or

true

or

parent_job.getVariable("INPUT_DIRECTORY").equals("/tmp");

The following variables are available for the expression:

Note: Make sure that you specify a step to pick the "lines_input" and other numbers from. Otherwise the values will remain 0. Open the transformation file and do that in the Transformation settings dialog, log tab.

Variable

Description

errors

Number of errors in the previous job entry (long)

lines_input

Number of rows read from database or file (long)

lines_output

Number of rows written to database or file (long)

lines_updated

Number of rows updated in a database table (long)

lines_read

number of rows read from a previous transformation step (long)

lines_written

Number of rows written to a next transformation step (long)

files_retrieved

Number of files retrieved from an FTP server (long)

exit_status

The exit status of a shell script (integer)

nr (integer)

The job entry number of the previous job entry (long); increments at every next job entry

is_windows

use if Pentaho Data Integration runs on Windows (boolean)

parent_job

The parent job of the current job entry (org.pentaho.di.job.Job class)

_entry_

The current job entry (org.pentaho.di.job.entries.JobEntryEval class)

Variables 

 
Here is how you can evaluate the content of a variable string:
parent_job.getVariable("NR_OF_ROWS") == 1000000 
 
Since we have access to the parent_job object, we can also set variables in the parent job this way:
 
parent_job.setVariable("NR_OF_ROWS", "1000000");

Note that the setVariable call can evaluates to false. Put a "true" after it to avoid having the step evaluate to false and throw an error.

For example you can do something like the following to manipulate variables within this job entry:

useDate = parent_job.getVariable("use_date").equals("1");
if (useDate == 0) { //We get to use native java classes as this is using Rhino mocks???
date = new java.util.Date();
date.setDate(date.getDate()-1); //Go back 1 full day
dateFormat = new java.text.SimpleDateFormat("yyyyMMdd");
newDateStr = dateFormat.format(date);
parent_job.setVariable("start_date", newDateStr);
}
true;

Previous result

When a job entry finishes, the result of the execution will be a Result object exposed as "previous_result" to the JavaScript engine:

Expression

Alternative

Data type

Meaning

previous_result.getResult()


boolean

true if the previous job entry was executed successfully, false if there was some error.

previous_result.getExitStatus()

exit_status

int

exit status of previous shell script job entry

previous_result.getEntryNr()

nr

int

The entry number is increased every time a job entry is executed.

previous_result.getNrErrors()

errors

long

the number of errors, also available as variable "errors"

previous_result.getNrLinesInput()

lines_input

long

The number of rows read from a file or database

previous_result.getNrLinesOutput()

lines_output

long

The number of rows written to a file or database

previous_result.getNrLinesRead()

lines_read

long

The number of rows read from previous steps

previous_result.getNrLinesUpdated()

lines_updated

long

The number of rows updated in a file or database

previous_result.getNrLinesWritten()

lines_written

long

The number of rows written to next step

previous_result.getNrLinesDeleted()

lines_deleted

long

The number of deleted rows

previous_result.getNrLinesRejected()

lines_rejected

long

The number of rows rejected and passed to another step via error handling

previous_result.getRows()


List<RowMetaAndData>

The result rows, see also below.

previous_result.isStopped()

 

boolean

Flag to signal if the previous previous job entry stopped or not.

previous_result.getResultFilesList()

 

List<ResultFile>

The list of all the files used in the previous job entry (or entries)

previous_result.getNrFilesRetrieved()

files_retrieved

int

The number of files retrieved from FTP, SFTP, etc.

previous_result.getLogText()

 

String

The log text of the execution of the previous job entry and its children.

previous_result.getLogChannelId()

 

String

The ID of the log channel of the previous job entry. You can use this to look up information on the execution lineage in the log channel log table.

 

is_windows

Boolean

True if the job runs on Windows variants, false if this is not the case.

Platform

 
We also expose a variable called "is_windows" to help you make platform specific choices

rows

 
The "rows" variable we expose to JavaScript helps you evaluate the result rows you passed to the next job entry using the "Copy rows to result" step.
Here is an example script on how to use this array:
 

var firstRow = rows[0];

firstRow.getString("name", "").equals("Foo")

 This script will follow the green job hop is the expression evaluates to true.  This happens if field "name" contains String "Foo".