Calling the kettle transform file in Java

Source: Internet
Author: User

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

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.