Kettle using Java code for execution

Source: Internet
Author: User
Tags throw exception

After the kettle design is complete, it can be called in the design tool, or it can be invoked using Java code.

1. Perform the conversion by file mode.

public static void Runtransfer (string[] params, String Ktrpath) {
trans trans = null;
try {
Initialize
Convert Meta Object
Kettleenvironment.init ();//initialization
Envutil.environmentinit ();
Transmeta Transmeta = new Transmeta (Ktrpath);
Transformation
trans = new trans (Transmeta);

Performing conversions
Trans.execute (params);
Wait for conversion execution to end
Trans.waituntilfinished ();
Throw exception
if (trans.geterrors () > 0) {
throw New Exception (
"There is errors during transformation exception! (Exception occurred during transmission) ");
}
} catch (Exception e) {
E.printstacktrace ();
}
}

2. Perform job by file mode

public static void Runjob (map<string,string> maps, String Jobpath) {
try {
Kettleenvironment.init ();
JobName is the path and name of the job script
Jobmeta Jobmeta = new Jobmeta (Jobpath, NULL);
Job Job = new Job (null, Jobmeta);
Pass parameters to the job script, get parameter values in script: ${parameter name}
Job.setvariable (Paraname, Paravalue);
Set<entry<string, string>> set=maps.entryset ();
For (iterator<entry<string, string>> it=set.iterator (); It.hasnext ();) {
Entry<string, string> ent=it.next ();
Job.setvariable (Ent.getkey (), Ent.getvalue ());
}
Job.start ();
Job.waituntilfinished ();
if (job.geterrors () > 0) {
throw New Exception (
"There is errors during job exception! (The execution job has an exception) ");
}
} catch (Exception e) {
E.printstacktrace ();
}
}

3. Perform the conversion in the resource pool.

public static void Runwithdb () throws kettleexception{
Kettleenvironment.init ();
To create a DB resource library
Kettledatabaserepository repository=new kettledatabaserepository ();
Databasemeta databasemeta=new Databasemeta ("Kettle", "MySQL", "jdbc", "localhost", "kettle", "3306", "root", "root");
Select a resource library
Kettledatabaserepositorymeta kettledatabaserepositorymeta=new Kettledatabaserepositorymeta ("Kettle", "Kettle", " Transformation description ", Databasemeta);
Repository.init (Kettledatabaserepositorymeta);
Connect to a resource library
Repository.connect ("admin", "admin");
Repositorydirectoryinterface Directoryinterface=repository.loadrepositorydirectorytree ();
Select conversion
Transmeta transmeta=repository.loadtransformation ("Demo1", directoryinterface,null,true,null);
Trans Trans=new trans (Transmeta);
Trans.execute (NULL);
Trans.waituntilfinished ();//wait until the data is over
if (Trans.geterrors () >0) {
SYSTEM.OUT.PRINTLN ("Transformation error");
}else{
SYSTEM.OUT.PRINTLN ("transformation successfully");
}
}


You can save the transform file to the repository and call it through the program.

Kettle using Java code for execution

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.