User Defined Java Class

You can use the User Defined Java Class step to provide a custom Java class that drives the behavior of the step.

This step is not intended for full-scale Java plugin development. Instead, it lets you define Java methods and logic directly in the transformation.

The step uses the Janinoarrow-up-right libraries to compile Java code at runtime.

How the step compiles your code

Janino (and this step) does not require a full Java class declaration. You provide the class body (for example, imports, constructors, and methods).

Your main code lives in the Processor tab, which defines the processRow() method.

The following imports are available in the Processor code by default:

  • org.pentaho.di.trans.steps.userdefinedjavaclass.*

  • org.pentaho.di.trans.step.*

  • org.pentaho.di.core.row.*

  • org.pentaho.di.core.*

  • org.pentaho.di.core.exception.*

If you add additional class code tabs, those default Processor imports do not automatically apply.

If you need other imports, add them at the top of your Processor code, for example:

import java.util.*;

Janino supports a subset of the Java 1.8.x specification. For details, see the Janino homepagearrow-up-right.

General

User Defined Java Class step
  • Step name: Specify the unique name of the step on the canvas. You can customize the name or leave it as the default.

Use the Class code panel and option tabs to define your class. Select Test class to validate the class.

Class code panel

Class code panel

Add your Java code in the Processor tab. You can add additional tabs by right-clicking and selecting Add new.

Processor (processRow)

The Processor defines the processRow() method. The transformation calls this method repeatedly until it returns false.

circle-info

Call getRow() before the first get(Fields.In, FIELD_NAME) call. This helps avoid unexpected field-order issues (for example, with Mapping Input Specification).

Example Processor code (concatenate first name and last name):

Error handling

To route errors to an error hop:

  1. Right-click the step on the canvas.

  2. Select Error handling.

  3. Configure the target step and field names.

Example (from User Defined Java Class – Lambda Examples.ktr):

Logging

If you want PDI to log actions from your code, add logging logic.

Example:

Classes and code fragments panel

Classes and code fragments panel

Use the Classes and Code Fragments panel to navigate your class code and related snippets.

  • Classes: Shows which classes have corresponding code tabs.

  • Code snippets: Shows internal PDI reference snippets.

  • Input fields / Info fields / Output fields: Shows fields referenced or defined by your code.

Example: get input row metadata:

For more details about PDI row data, see PDI Rows Of Dataarrow-up-right.

Fields tab

Fields tab

Use the Fields table to define output fields passed to downstream steps. Fields you define appear under Output fields in the Classes and Code Fragments panel.

Parameters tab

Parameters tab

Use the Parameters table to avoid hard-coded strings (for example, field names).

Example:

Class member variables and getVariable

If you call getVariable() in the init() method, variables resolve as expected. If you call it in a class member initializer, it does not resolve by design.

Example:

Info steps tab

Info steps tab

Because getRow() returns the first row from any input stream (input or info), configure Info steps when input and info row metadata differ.

Read info rows before calling getRow().

Example:

Target steps tab

Target steps tab

Use the Target steps table to route step output to specific steps in your transformation.

Examples

The data-integration/samples/transformations directory contains these examples:

  • User Defined Java Class - Calculate the date of Easter.ktr

  • User Defined Java Class - Concatenate firstname and lastname.ktr

  • User Defined Java Class - Query the database catalog.ktr

  • User Defined Java Class - Real-time search on Twitter.ktr

  • User Defined Java Class - LambdaExamples.ktr

We recommend starting with User Defined Java Class - Calculate the date of Easter.ktr.

Metadata injection support

All fields of this step support metadata injection. You can use this step with ETL metadata injection to pass metadata to your transformation at runtime.

Last updated

Was this helpful?