Kettle Java call

Source: Internet
Author: User

Package kettle;

Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. util. arraylist;
Import java. util. List;

Import org. Apache. log4j. Logger;
Import org. pentaho. Di. Core. kettleenvironment;
Import org. pentaho. Di. Core. database. databasemeta;
Import org. pentaho. Di. Core. Exception. kettledatabaseexception;
Import org. pentaho. Di. Core. Exception. kettleexception;
Import org. pentaho. Di. Core. util. envutil;
Import org. pentaho. Di. job. Job;
Import org. pentaho. Di. Job. jobmeta;
Import org. pentaho. Di. repository. longobjectid;
Import org. pentaho. Di. repository. objectid;
Import org. pentaho. Di. repository. repositorydirectory;
Import org. pentaho. Di. repository. repositorydirectoryinterface;
Import org. pentaho. Di. repository. KDR. kettledatabaserepository;
Import org. pentaho. Di. repository. KDR. kettledatabaserepositorymeta;
Import org. pentaho. Di. Trans. Trans;
Import org. pentaho. Di. Trans. transmeta;

Public class kettleexecutor {

Logger log = logger. getlogger (getclass ());
Kettledatabaserepository rep;
Repositorydirectoryinterface dir;


Public String getdatabaserepositoryxml (){
String xml = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+
"<Connection>" +
"<Name> kettle </Name>" +
"<Type> MSSQL </type>" +
"<Server> 127.0.0.1 </Server>" +
"<Access> native </access>" +
"<Database> kettle </database>" +
"<Port> 1433 </port>" +
"<Username> SA </username>" +
"<Password> password </password>" +
"<Servername/>" +
"<Data_tablespace/>" +
"<Index_tablespace/>" +
"</Connection> ";
Return XML;
}

Public void connectrepository (string username, string password ){
Try {
Envutil. environmentinit ();
Kettleenvironment. INIT ();
Databasemeta datameta = new databasemeta (getdatabaserepositoryxml ());
Kettledatabaserepositorymeta repinfo = new kettledatabaserepositorymeta ();
Repinfo. setconnection (datameta );

Rep = new kettledatabaserepository ();
Rep. INIT (repinfo );
Rep. Connect (username, password );

Objectid rootid = long. parselong (REP. getrootdirectoryid (). GETID ()> 0? Rep. getrootdirectoryid (): New longobjectid (0 );
Dir = new repositorydirectory ();
Dir. setobjectid (rootid );
} Catch (kettleexception e ){
E. printstacktrace ();
}
}

Public list <objectid> getobjectids (string SQL) throws kettledatabaseexception {
List <objectid> List = new arraylist <objectid> ();
Statement stmt = NULL;
Resultset rs = NULL;
Try {
Stmt = rep. getdatabase (). getconnection (). createstatement ();
Rs = stmt.exe cutequery (SQL );
While (Rs. Next ())
List. Add (New longobjectid (Rs. getlong (1 )));
} Catch (sqlexception e ){
E. printstacktrace ();
} Finally {
Try {
If (RS! = NULL)
Rs. Close ();
If (stmt! = NULL)
Stmt. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Return list;
}

Public void runalljobs (){
Try {
List <objectid> jobids = getobjectids ("select id_job from r_job where job_status> = 0 order by id_job ");
For (final objectid OID: jobids ){
// New jobsexecutor (REP, OID). Run ();
New runnable (){
@ Override
Public void run (){
Try {
Jobmeta = rep. loadjob (OID, null );
Job job = new job (REP, jobmeta );
Log.info ("********************************** start execution job: "+ job. getjobname ());
Job. Start ();
Job. waituntilfinished ();
If (job. geterrors ()> 0 ){
Log.info ("********************************** execute a job error: "+ job. getjobname ());
}
} Catch (kettleexception e ){
E. printstacktrace ();
}
}
}. Run ();
}
} Catch (kettleexception e ){
E. printstacktrace ();
}
}

Public void runjob (objectid jobid, string versionlabel ){
Try {
Jobmeta = rep. loadjob (jobid, versionlabel );
Job job = new job (REP, jobmeta );
Job. Start ();
Job. waituntilfinished ();
If (job. geterrors ()> 0 ){
System. Out. println ("decompress fail! ");
}
} Catch (kettleexception e ){
E. printstacktrace ();
}
}

/**
* File-based job call
* @ Param filename the path and name of the job script
*/
Public void runjob (string filename ){
Try {
Kettleenvironment. INIT ();
Jobmeta = new jobmeta (filename, null );
Job job = new job (null, jobmeta );
// Pass parameters to the job script. The script obtains the parameter value: $ {parameter name}
// Job. setvariable (paraname, paravalue );
Job. Start ();
Job. waituntilfinished ();
If (job. geterrors ()> 0 ){
System. Out. println ("decompress fail! ");
}
} Catch (kettleexception e ){
System. Out. println (E );
}
}

/**
* File method call Transformation
* @ Param filename the path and name of the transformation script
*/
Public void runtran (string filename ){
Try {
Kettleenvironment. INIT ();
Transmeta = new transmeta (filename );
Trans trans = new Trans (transmeta );
Trans. prepareexecution (null );
Trans. startthreads ();
Trans. waituntilfinished ();

If (trans. geterrors ()! = 0 ){
System. Out. println ("error ");
}
} Catch (kettleexception e ){
E. printstacktrace ();
}
}

}

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.