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