In java, you can call the kettle conversion file 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?
In java, you can call the kettle conversion file 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?
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
Paras) {try {KettleEnvironment. init (); TransMeta transMeta = new TransMeta (fullFileName); Trans transformation = new Trans (transMeta); for (Map. Entry
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
Para = new HashMap
(); // Assign para. put ("XlsName", "data") to the name parameter in the conversion; etl. runTransformation (para );}}