Java calls the kettle conversion file, and java calls the kettle Conversion

Source: Internet
Author: User

Java calls the kettle conversion file, and java calls the kettle Conversion

Call kettle to convert files in java

 

You can also call the command line code through the command line, and then call the command line code in java. In this way, it is not seamlessly integrated with the java code logic. This article shows that if kettle5.1 is seamlessly integrated with java code through other APIs, most of the information on the Internet is of a lower version and cannot be run in kettle5.x.

1. Which jar files are required?


It must begin with kettle, and the three at the bottom must also be. Two in the red box are used for test conversion, which are UUID generation and file generation respectively.

If the jar file is missing, an error must be reported when running the program. You can find the corresponding jar in the kettle installation directory LIB and add it to the compilation path.

2. examples show how to call the conversion through java

In this example, the content of an excel file is imported into data, and there are only two columns in excel. Therefore, you need to generate a uuid in kettle and then import it to the database.


The uuid generated by default has a '-' interval, so it is null by replacing it with "Replace in string;


In the excel step, the naming parameters are used. Therefore, you must set the naming parameters in the conversion configuration.

The sample code is as follows:

Publicclass KettleUtil2 {public String RES_DIR = "res"; private String fullFileName; public KettleUtil2 (String fileName) {fullFileName = System. getProperty ("user. dir ") + File. separator + RES_DIR; fullFileName + = File. separator + fileName;}/*** No parameter is required. Set the parameter to null * @ param paras */publicvoid runTransformation (Map <String, String> paras) {try {KettleEnvironment. init (); TransMeta transMeta = new TransMeta (fullFileName); Trans transformation = new Trans (transMeta); for (Map. entry <String, String> entry: paras. entrySet () {transformation. setParameterValue (entry. getKey (), entry. getValue ();} transformation.exe cute (null); transformation. waitUntilFinished (); if (transformation. getErrors ()> 0) {thrownew RuntimeException ("There wereerrors during transformation execution. ") ;}} catch (KettleException e) {System. out. println (e );}}}


 

The call code is as follows:

Publicclass EtlTest {publicstaticvoid main (String [] args) {KettleUtil2 etl = new KettleUtil2 ("testimport. ktr "); Map <String, String> para = new HashMap <String, String> (); // assign para to the name parameter in the conversion. put ("XlsName", "data"); etl. runTransformation (para );}}


 

 

 


In the kettle graphic interface, data extraction and conversion can be directly implemented. Why do we need to save it as a ktr file first and then call it using a java program?

Can be run, non-graphical operation can reduce the resources consumed by the graphic interface, and there are many running methods, such as sample interface running, command line calling running, Program Calling running, remote machine running (cluster running).

How does java call a local job to generate logs by time? kettle

You can use log4j. The configuration is as follows:
<Appender name = "PROJECT" class = "org. apache. log4j. dailyRollingFileAppender "> <param name =" File "value ="/xxx. log "/> <param name =" DatePattern "value = "'. 'yyyymmdd '. log' "/> <layout class =" org. apache. log4j. patternLayout "> <param name =" ConversionPattern "value =" % d {MMdd HH: mm: ss SSS}, % m % n "/> </layout> </appender>

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.