Calling Java classes in kettle

Source: Internet
Author: User

Calling Java classes in kettle

Sometimes you need to call Java classes in kettle, such as: validation, querying, or custom encryption. Sometimes even basic data access is not as simple as getting a storage file or using a database connection, some data sources may be encapsulated in the application, and manually using custom Java Client Access is the only way. This article describes how to call the Java class in kettle. The sample code is downloaded here.

Note: If you use kettle4.0 and above, you can also use the User defined Java class step.

Modified Java Scriptvalue Steps

The key is to use the Modifiedjava Script Value step in kettle. From the name it is just the implementation of JavaScript to implement this step, in fact, you can also execute Java code. To understand that, we need to know that this step is implemented using the Rhino JavaScript engine, which makes it possible to implement JavaScript in the JVM, and make the script accessible to Java classes. To illustrate how it is implemented, let's look at a simple conversion example that uses Java to calculate the MD5 code for a field. As shown in the following:

The JavaScript step implements the MD5 calculation, which contains the following code:

Simulate Java Imports

var messagedigest =java.security.messagedigest;

var String =java.lang.string;

var StringBuffer =java.lang.stringbuffer;

var Integer =java.lang.integer;

  

Get the MD5 digest algorithm

var algorithm =messagedigest.getinstance ("MD5");

  

Get the input as bytes

var bytes = new String (Test_value). GetBytes ("UTF-8");

  

Calculate the Digest

Algorithm.reset ();

Algorithm.update (bytes);

var md5digest =algorithm.digest ();

  

Turn the digest into a hex-stringrepresentation

var hexstring = new StringBuffer ();

  

for (vari =0; i < md5digest.length; i++) {

var hex= integer.tohexstring (0xFF & Md5digest[i]);

if (hex.length () = = 1) {

Hexstring.append (' 0 ');

}

Hexstring.append (hex);

}

  

Write output value

var Md5_hash =hexstring.tostring (). toUpperCase ();

Let's explain the above code in more detail.

The first part is just like the Java Import Package link, in fact, the same purpose as import in Java. The declaration variable points to the Java class to avoid the inconvenience of using the full Java long class name for each Java class that we refer to later. When using a class of your own definition, be careful to overwrite the JavaScript class accidentally. JavaScript also has its own instance of the date class, slightly unconcerned, and is easily overwritten with this approach.

Next is to invoke the simple Java API to generate the MD5 value, it should be noted that the input field as a JavaScript object, the "Test_value" field is a JavaScript string object, there are several ways to convert it to a Java string object, This is accomplished using a simple constructor, and the GetBytes () method of Java string returns a byte array;

After the MD5 digest value is calculated, a string that shows the value is generated by a small loop of code, which is then written to the output line as a new field.

Using classes in an external jar package

Performing too many steps can affect performance, so always complicate the business with compiled Java code, usually some neat Java class Library to implement our business, and sometimes we may create it ourselves. Kettle existing features of course do not meet all of our needs, calling several external class libraries can avoid writing a full kettle plugin. Kettle loads all the jar files in the Libext directory and subdirectories at startup, and if you need to access the class of the custom jar package, you need to put it in the Libext file. Our example is about MD5, which has a corresponding jar package (Apache Commons codec project) from kettle3.2. The codec project has a good way of calculating the MD5 hexadecimal string of the input string, which is exactly what we need; we just use the Digestutil class method.

Get a nice MD5 hash

Varmd5_hash = Org.apache.commons.codec.digest.DigestUtils.md5Hex (Test_value);

Run the new conversion in my notebook about 10500 lines/second, the first version of the only about 2900 lines/second.

The processing speed of the two is very different, so it seems really fast to use the encapsulated function, of course, it is possible that the Apache project will be implemented using a completely separate MD5 algorithm. In any case, the latter's implementation is both simple and fast.

Conclusion

The Modified Java Script Value Step allows access to Java classes in any JVM. If you have specific requirements for your ETL project, and you have already implemented the requirements in Java, you can consider using this method to try. When using footstep code you always need to monitor performance issues. If you use a large number of external libraries, you need to have the correct version dependency management, or you will have problems in your test environment or deployment environment.

Calling Java classes in kettle

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.