Calling the kettle transform file in Java
It can also be called through the command line, and then the command line code can be invoked in Java, which is not seamlessly integrated with the Java code logic. This article explains that if kettle5.1 is seamlessly integrated through other APIs and Java code, most of the information on the web is low and cannot be run in kettle5.x.
1. What jar files are required
The beginning of the kettle is necessary, the bottom three is also to be; the two in the red box are the ones I used to test the conversion, which are the generated UUID and the file.
If the jar file is missing, run the program must be error, we are based on errors to the Kettle installation directory Lib to find the appropriate Jar added to the compilation path.
2. Example shows how to call a transformation via Java
The example is to import the contents of an Excel into the data, Excel has only two columns, so you need to generate a list of UUID in kettle and then import into the database.
The default generated UUID has a '-' spacer, so the "Replace in string" is replaced by null;
The Excel step uses named parameters, so you set the naming parameters in the transformation 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 Yes, set 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.execute (NULL); Transformation.waituntilfinished (); if (transformation.geterrors () > 0) {thrownew runtimeexception ("there wereerrors during TRA Nsformation execution. ");}} catch (Kettleexception e) {SYSTEM.OUt.println (e); } }}
The final calling 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 a value to the named parameter in the transformation para.put ("Xlsname", "data"); Etl.runtransformation (para); }}
Calling the kettle transform file in Java