Controlling the flow with the putRow() function

With the Java Class step, you control which rows go to the next step using the putRow() method. With this method selectively, you decide which rows to send and which rows to discard.

As an example, if we want to apply a filter and keep only the words with length greater than three, we could move the putRow() function inside an if clause, as shown in the following sample:

if (len_word > 3) { 
putRow(data.outputRowMeta, outputRow);
}

Your final code should look as follows:

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { 
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
if (first) { first = false;}
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());

String word = get(Fields.In, "word").getString(r);
get(Fields.Out, "word").setValue(outputRow, word.toUpperCase());
long len_word = word.length();
get(Fields.Out, "len_word").setValue(outputRow, len_word);
if (len_word > 3) {
putRow(data.outputRowMeta, outputRow);
}

return true;
}

If you run a preview, you will see something like this:

Previewing the output of a Java Class step

In summary, the output dataset differs from the original in that it has a new field named len_word, but only contains the rows where the length of the word field is greater than 3. Also, the word field has been converted to uppercase.