Detailed user Defined Java class step (i)

Source: Internet
Author: User



Detailed User Defined Java Class Step (i)

The "User defined Java class" step in kettle, also known as the UDJC step, is available from the 4.0 version and is very powerful. ; You can write arbitrary code in it without compromising efficiency. This article will explain in detail the examples shown in different scenarios if you use this step, because the content is very much, easy to read, the content is divided into three parts, please complete the entire content, the sample code is downloaded here .

UDJC Step work mechanism

User-defined Java classes are from org.pentaho.di.trans.steps.userdefinedjavaclass.TransformClassBase We can download the source code and look at the methods and properties of the class, which is helpful for me to understand.

When the transform runs, the code for theUDJC step Inherits Transformclassbase and compiles, and the class is

A generic step plug-in class that has some convenient public methods. Our custom code can override or inherit the parent class's methods or properties, or declare additional methods or properties, and can also import the declaration of the class at the beginning of the code, with the following classes automatically imported by default:

Import org.pentaho.di.trans.steps.userdefinedjavaclass.*;

importorg.pentaho.di.trans.step.*;

importorg.pentaho.di.core.row.*;

Import org.pentaho.di.core.*;

importorg.pentaho.di.core.exception.*;

If you are already familiar with Kettle 's internal mechanisms and want to easily access some objects through the code, click the code snippet to the left of the UDJC Step property to help us learn faster.

The following sections show the UDJC steps in different scenarios if you use:

Simple field Conversions

The first example implements the very simple operation of turning a field of a string type into uppercase. The goal is to explain how to set up steps and work with rows, and how to access input and output fields. If you are already developing the Kettle plugin, these are very familiar to you. Assuming the row data flow contains a field "Testfield", the output field "uppercase" of a character type is defined inUDJC . The following code implements the Testfield into uppercase and writes to the output field as a result.

The code is as follows:

public boolean Processrow (Stepmetainterfacesmi, Stepdatainterface SDI) throws Kettleexception

{

object[] r = GetRow ();

if (r = = null) {

Setoutputdone ();

return false;

   }

if (first) {

first = false;

   }

r= Createoutputrow (R, Data.outputRowMeta.size ());

//Get The value from an input field

String test_value = Get (fields.in, "Testfield"). GetString (R);

//play around with it

String uppercase_value = Test_value.touppercase ();

   

//Set a value in a new output field

Get (fields.out, "uppercase"). SetValue (R, Uppercase_value);

//Send the row to the next step.

PutRow (Data.outputrowmeta, R);

return true;

}

KETTLEUDJC The step calls the Processrow() method to process an input line, and if it returns True, continues to prepare to process another input row and returns falseif no data is processed.

GetRow () is a blocking call; it waits for the previous step to provide a row of data, returning an array of objects to represent the input rows, or null to indicate that no further input lines are required to be processed.

Next is a simple seemingly useless three-line code that involves a Boolean type field first ( a field of the parent class), which makes it easy to identify whether or not you are working on a row of data, which is useful when some work needs to be performed only once. If you do not use it, you can ignore it.

calling Createoutputrow () ensures that the row array is large enough to accommodate the increased output field.

The get() method can access the input or output fields of the step based on the name, andit needsto indicate the type of the field (in, out,Info), and the name of the field, returning oan instance of the Rg.pentaho.di.trans.steps.userdefinedjavaclass.FieldHelper class that can access the data for the field. The parent class is defined as follows: Publicfieldhelper Get (fields type, String name) throwskettlestepexception;

After the output field is set in the row, call the putRow() transfer to the next possible step.

This short example converts the uppercase.ktr file in an attachment by quickly customizing the relevant contents of the input field.

The sample code is downloaded here and more on the second and third parts.

Detailed user Defined Java class step (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.