Calling Java classes in kettle
Sometimes it is necessary to call Java classes in kettle, such as: validation, querying, or defining your own encryption. Sometimes even the main data access is not so simple, such as getting a storage file or using a database connection, some data sources may be encapsulated in the application, manual use of their own defined javaclient access is the only way.
This article describes how to call Java classes in kettle. The demo sample code is downloaded here.
Note: Suppose you use kettle4.0 and above version number. You can also use the User defined Java class step to implement it.
Modified Java Scriptvalue Steps
The key is to use the Modifiedjava Script Value step in kettle. From the name, it is only running JavaScript to implement this step, and in fact it can run Java code. In order to understand. We need to know that this step is implemented using the Rhino JavaScript engine. Makes it possible for JavaScript to be implemented in the JVM, allowing the script to access Java classes. To illustrate how it is implemented. Let's look at a simple conversion demo sample that uses Java to calculate the MD5 code for a field. For example, as seen in:
The JavaScript steps implement MD5 calculations, including for example 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 take a detailed explanation of the above code.
The first part is like the Java Import Package link, in fact, in Java and import to achieve the same purpose. Declares a variable to point to a Java class. is to avoid the inconvenience of using a full Java long class name for each Java class that we refer to later. When using custom classes, be careful to accidentally overwrite the JavaScript class. JavaScript also has its own instance of the date class, slightly unconcerned, and is very easy to cover in such a way.
Next is to invoke the simple Java API to generate the MD5 value, and it should be noted that the input field is used 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, which is implemented using a simple constructor function. The GetBytes () method of Java string returns a byte array.
After the MD5 digest value is calculated. It then generates a string that shows the value through a small loop of code, and finally writes to the output line as a new field.
Using classes in an external jar package
Running too many steps can affect performance, so it's always good to have a complex business with compiled Java code. It is generally a neat Java class library that implements our business, and sometimes we may create it ourselves. Kettle existing features of course do not meet all our needs, calling several external class libraries to avoid writing a full kettle plugin. Kettle load all jar files under Libext folder and subfolders at startup. Suppose you need to access the class that defines the jar package, which needs to be placed in the Libext file. Our demo sample is about MD5, from kettle3.2 already has a corresponding jar package (Apache Commons codec project).
The codec project has a very good way to calculate the MD5 hexadecimal string of the input string, which is exactly what we need; we only use the Digestutil class method.
Get a nice MD5 hash
Varmd5_hash = Org.apache.commons.codec.digest.DigestUtils.md5Hex (Test_value);
In my notebook to perform a new conversion of about 10500 lines/second, the first version number of only about 2900 lines/second.
The processing speed difference between the two is very large, so the packaged function seems to be really fast. Of course, it is possible that the Apache project is implemented using a completely different MD5 algorithm.
In any case, the latter's implementation is both simple and fast.
Conclusion
Modified Java Script Value Step agrees to access the Java class in whatever JVM. Assuming you have specific requirements for your ETL project, you have already implemented the requirements in Java. At this point you can consider using this method to try.
When using footstep code you always need to monitor performance issues. Suppose you use a lot of external libraries. It is necessary to have the correct version number dependent on the management, otherwise there will be problems in the test environment or the deployment environment.
Calling Java classes in kettle