User Defined Java Class

Description

The User Defined Java Class transform allows you to enter User Defined Java Class to drive the functionality of a complete transform.

In essence, this transform allows you to program your own plugin in a transform.

The goal of this transform is not to allow a user to do full-scale Java development inside a transform.

Obviously we have a whole plugin system available to help with that part.

The goal is to allow users to define methods and logic with as little as code as possible, executed as fast as possible.

For this we use the Janino project libraries that compile Java code in the form of classes at runtime.

Options

OptionDescription

Transform name

Name of the transform.

Class code

The Java code.

Fields

List of output fields.

Fieldname

Output field name.

Type

Type of field.

Length

Length of the field.

Precision

Precision of the field.

Parameters

You can use the Parameters table to avoid using hard-coded string values, such as field names (customer for example).

Tag

The parameter tag.

Value

The parameter value.

Description

Description of the parameter.

Info transforms

Tag

Transform

Which transform to read from.

Description

Target transforms

Tag

Transform

Which transform to output to.

Description

Test class

Tests the class.

Usage

Process rows

The Processor code defines the processRow() method, which is the heart of the transform. This method is called by the pipeline in a tight loop and will continue until false is returned.

  1. String firstnameField;
  2. String lastnameField;
  3. String nameField;
  4. public boolean processRow() throws HopException
  5. {
  6. // Let's look up parameters only once for performance reason.
  7. //
  8. if (first) {
  9. firstnameField = getParameter("FIRSTNAME_FIELD");
  10. lastnameField = getParameter("LASTNAME_FIELD");
  11. nameField = getParameter("NAME_FIELD");
  12. first=false;
  13. }
  14. // First, get a row from the default input hop
  15. //
  16. Object[] r = getRow();
  17. // If the row object is null, we are done processing.
  18. //
  19. if (r == null) {
  20. setOutputDone();
  21. return false;
  22. }
  23. // It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
  24. // enough to handle any new fields you are creating in this transform.
  25. //
  26. Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
  27. String firstname = get(Fields.In, firstnameField).getString(r);
  28. String lastname = get(Fields.In, lastnameField).getString(r);
  29. // Set the value in the output field
  30. //
  31. String name = firstname+" "+lastname;
  32. get(Fields.Out, nameField).setValue(outputRow, name);
  33. // putRow will send the row on to the default output hop.
  34. //
  35. putRow(data.outputRowMeta, outputRow);
  36. return true;

Error handling

If you want Hop to handle errors that may occur while running your class in a pipeline, you must implement for your own error handling code. Before adding any error handling code, right-click on the User Defined Java Class transform in the Hop client canvas and select Error Handling in the menu that appears. The resulting transform error handling settings dialog box contains options for specifying an error target transform and associated field names that you will use to implement error handling in your defined code.

  1. try {
  2. Object numList = strsList.stream()
  3. .map( new ToInteger() )
  4. .sorted( new ReverseCase() )
  5. .collect( Collectors.toList() );
  6. get( Fields.Out, "reverseOrder" ).setValue( row, numList.toString() );
  7. } catch (NumberFormatException ex) {
  8. // Number List contains a value that cannot be converteds to an Integer.
  9. rowInError = true;
  10. errMsg = ex.getMessage();
  11. errCnt = errCnt + 1;
  12. }
  13. if ( !rowInError ) {
  14. putRow( data.outputRowMeta, row );
  15. } else {
  16. // Output errors to the error hop. Right click on transform and choose "Error Handling..."
  17. putError(data.outputRowMeta, row, errCnt, errMsg, "Not allowed", "DEC_0");
  18. }

The try in the code sample above tests to see if numList contains valid numbers. If the list contains a number that is not valid, putError is used to handle the error and direct it to the wlog: ErrorPath transform in the sample pipeline. The ErrorPath transform is also specified in the Target transforms tab of the User Define Java Class transform.

Logging

You need to implement logging in your defined transform if you want Hop to log data actions from your class, such as read, write, output, or update data. The following code is an example of how to implement logging:

  1. putRow( data.outputMeta, r );
  2. if ( checkFeedback( getLinesOutput() ) ) {
  3. if ( log.isBasic() ) {
  4. logBasic( "Have I got rows for you! " + getLinesOutput() );
  5. }
  6. }

Class and code fragments

You can navigate through your defined classes along with related code snippets and fields through the Classes and Code Fragments panel. You can right-click on any item in this tree to either Delete, Rename, or Show Sample.

Classes

The Classes folder indicates what classes have corresponding code block tabs in the Class Code panel.

Code Snippits

The Code Snippits folder shows the internal Hop code related to the User Defined Java Class transform. These snippits are shown as reference for the code of your class.

Input Fields

The Input fields folder contains any input fields you define in your code. While working with your defined code, you will be handling input and output fields. Many ways exist for handling input fields. For example, to start, examine the following description of an input row.

  1. RowMetaInterface inputRowMeta = getInputRowMeta();

The inputRowMeta object contains the metadata of the input row. It includes all the fields, their data types, lengths, names, format masks, and more. You can use this object to look up input fields. For example, if you want to look for a field called customer, you would use the following code.

  1. ValueMetaInterface customer = inputRowMeta.searchValueMeta("year");

Because looking up field names can be slow if you need to do it for every row that passes through a pipeline, you could look up field names in advance in a first block of code, as shown in the following example:

  1. if (first) {
  2. yearIndex = getInputRowMeta().indexOfValue(getParameter("YEAR"));
  3. if (yearIndex<0) {
  4. throw new HopException("Year field not found in the input row, check parameter 'YEAR'\!");
  5. }
  6. }

To get the Integer value contained in the year field, you can then use the following construct.

  1. Object[] r = getRow();
  2. ...
  3. Long year = inputRowMeta().getInteger(r, yearIndex);

To make this process easier, you can use a shortcut in the following form.

  1. Long year = get(Fields.In, "year").getInteger(r);

This method also takes into account the index-based optimization mentioned above.

Metadata Injection Support

All fields of this transform support metadata injection. You can use this transform with ETL Metadata Injection to pass metadata to your pipeline at runtime.