Kettle calls java class

Source: Internet
Author: User
Java classes called in kettle sometimes need to be called in kettle, such as verification, query, or custom encryption. Sometimes even basic data access is not that simple. For example, to obtain a storage file or use a database connection, some data sources may be encapsulated in applications, using a custom java client is the only method. This article introduces

Java classes called in kettle sometimes need to be called in kettle, such as verification, query, or custom encryption. Sometimes even basic data access is not that simple. For example, to obtain a storage file or use a database connection, some data sources may be encapsulated in applications, using a custom java client is the only method. This article introduces

Kettle calls java class

Sometimes java classes, such as verification, query, or custom encryption, need to be called in kettle. Sometimes even basic data access is not that simple. For example, to obtain a storage file or use a database connection, some data sources may be encapsulated in applications, using a custom java client is the only method. This article describes how to call java classes in kettle. Here is the sample code.Download.

Note: If you use kettle4.0 or a later version, you can also use the user defined java class step.

Modified Java ScriptValue step

The key is to use the ModifiedJava Script Value step in kettle. From the perspective of name, you can only execute javascript to implement this step. In fact, you can also execute java code. To understand this, we need to know that this step is implemented using the Rhino javaScript Engine so that it can implement javascript in jvm and make it possible to allow scripts to access java classes. To illustrate how it is implemented, let's look at a simple conversion example, using java to calculate the md5 code of a field. As shown in:

The javascript step implements md5 calculation, including the following code:

// Simulate java imports

VarMessageDigest = java. security. MessageDigest;

VarString = java. lang. String;

VarStringBuffer = java. lang. StringBuffer;

VarInteger = java. lang. Integer;

// Get the md5 digest algorithm

Varalgorithm = MessageDigest. getInstance ("MD5 ");

// Get the input as bytes

Varbytes = newString (test_value). getBytes ("UTF-8 ");

// Calculate the digest

Algorithm. reset ();

Algorithm. update (bytes );

Varmd5Digest = algorithm. digest ();

// Turn the digest into a hex-stringrepresentation

VarhexString = newStringBuffer ();

For (vari = 0; I <md5Digest. length; I ++ ){

Varhex = Integer. toHexString (0xFF & md5Digest [I]);

If (hex. length () = 1 ){

HexString. append ('0 ');

}

HexString. append (hex );

}

// Write output value

Varmd5_hash = hexString. toString (). toUpperCase ();

Let's explain the above Code in detail.

The first part is like the java import package. In fact, it achieves the same purpose as the import in java. The declared variables point to the java class to avoid inconvenience caused by reference to each java class using the complete java long class name. When using a self-defined class, be careful not to overwrite the javascript class. Javascript also has its own Date class instance, which is easy to overwrite.

Next, call a simple java API to generate the md5 value. It should be noted that the input field is used as the javascript object, and the "test_value" field is the javascript String object, there are multiple ways to convert it into a java String object, which is implemented using a simple constructor here; the getBytes () method of java String returns a byte array;

After the md5 Digest value is calculated, a short loop code is used to generate a string that represents the value. Finally, a new field is written to the output line.

Use the class in the external jar package

Too much execution may affect the performance. Therefore, we always make the compiled java code for the complex business. This is usually a neat java class library that implements our business. Sometimes we may create it ourselves. Kettle's existing functions certainly cannot meet all our needs. Calling several external class libraries can avoid writing a complete kettle plug-in. When kettle starts, it loads the libext directory and all jar files under the subdirectory. If you need to access the class of the custom jar package, you need to put it in the libext file. The example is about MD5. The corresponding jar package (apache commons codec project) is already available from kettle3.2 ). The codec project has a good way to calculate the MD5 hexadecimal string of the input string, which is exactly what we need. We only need to use the DigestUtil class method.

// Get a nice md5 hash

Varmd5_hash = org. apache. commons. codec. digest. DigestUtils. md5Hex (test_value );

In my notebook, the new switch is about 10500 rows/second, and the first version is about 2900 rows/second.

The processing speed varies greatly between the two, so encapsulated functions seem to be fast. Of course, apache projects may adopt completely different MD5 algorithms. In any case, the implementation of the latter is simple and fast.

Conclusion

The Modified Java Script Value step allows access to any java classes in the jvm. If your ETL project has specific requirements and you have already implemented them using java, you can try this method. When using step-by-step code, you always need to monitor performance issues. If you use a large number of external libraries, you must have correct version dependency management. Otherwise, problems may occur in the test environment or deployment environment.

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.