Calling the kettle transform file in Java
It can also be called through the command line, and then the command line code in Java can also be called. This does not integrate seamlessly with the Java code logic. This article explains that kettle5.1 is assumed to be seamlessly integrated through other APIs and Java code, and most of the information on the web is in the low version. cannot be executed in kettle5.x.
1. What jar files are required
It is necessary to start with kettle, and the following three are required; the two in the red box are the ones I used to test the conversion. Each is generated UUID and file.
If the jar file is missing, the execution program must have an error. Everyone based on the error to the Kettle installation folder Lib to find the corresponding Jar added to the compilation path.
2. Demo sample shows how to call the transformation through Java
The demo 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 it into the database.
The default generated UUID has a '-' spacer, so the "Replace in string" is replaced by null;
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbmv3zwfzdhn1bg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
The Excel step uses a named parameter, so you set the name parameter in the transformation configuration.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbmv3zwfzdhn1bg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
The demo 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 parameters, set parameter 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 last calling code is for example:
Publicclass etltest { publicstaticvoid main (string[] args) { KettleUtil2 ETL = new KettleUtil2 ("Testimport.ktr" ); Map<string,string> para = new hashmap<string,string> (); Assigns the value of the named parameter in the conversion para.put ("Xlsname", "data"); Etl.runtransformation (para); }}
Calling the kettle transform file in Java