Kettle ETL Java calls kettle job or transfer

Source: Internet
Author: User

CodeThe following code calls kettle 4.2.

Package test;

Import org. Apache. commons. Lang. stringutils;
Import org. Apache. commons. VFS. fileselector;
Import org. pentaho. Di. Core. kettleenvironment;
Import org. pentaho. Di. Core. database. databasemeta;
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. 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;

Import com. longtech. configuration. baseconfiguration;
Import com. Platform. Common. util. loggerutil;

/**
*
* @ Description: Java calls the conversion in kettle Database
* @ Author Chen this class is a test class that calls kettle 4.2.
* @ Versions 1.0,
* @ Date 02:40:50
*/
Public class execrepositorytrans {
/**
* This test class is used with caution !!!!!!!
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// D: \ Java \ workspace \ sics \ SRC \ config \ kettle
String classesdir = baseconfiguration. getstring ("classesdir ");
String jobname = baseconfiguration. getstring ("kettle. Path. File ");
If (stringutils. isnotempty (jobname )){
Loggerutil.info (execrepositorytrans. class, "Run kettle job start -------------------");

Runjob (classesdir + jobname );

Loggerutil.info (execrepositorytrans. class, "Run kettle job end -------------------");
} Else {

Loggerutil.info (execrepositorytrans. class, "configure the kettle. Path. File job path file ");
}

}
/**
* Java calls kettle Conversion
*/
Public static void runtransfer (){
Trans trans = NULL;
Fileselector F = NULL;
Try {
// Initialization
String fname = "d :\\ kettle \ informix_to_am_4.ktr ";
// Convert the Meta Object
Kettleenvironment. INIT (); // Initialization
Envutil. environmentinit ();
Transmeta = new transmeta (fname );
// Conversion
Trans = new Trans (transmeta );
// Execute the conversion
Trans.exe cute (null );
// Wait until the switch execution ends
Trans. waituntilfinished ();
// Throw an exception
If (trans. geterrors ()> 0 ){
Throw new exception ("there are errors during transformation exception! (Exceptions occurred during transmission )");
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Java calls kettle's job
* @ Param jobname: String fname = "D: \ kettle \ informix_to_am_4.ktr ";
*/
Public static void runjob (string jobname ){
Try {
Kettleenvironment. INIT ();
// Jobname is the path and name of the job script.
Jobmeta = new jobmeta (jobname, 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 ){
Throw new exception ("there are errors during job exception! (Job execution exception )");
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Configure the data source to call related jobs and transfer in the resource library
*/
Public static void dbresource (){
String transname = "T1 ";

Try {
Kettleenvironment. INIT ();
Databasemeta datameta = new databasemeta ("kettledbrep", "MSSQL", "native", "127.0.0.1", "ETL", "1433", "sa", "bsoft ");
Kettledatabaserepositorymeta repinfo = new kettledatabaserepositorymeta ();
Repinfo. setconnection (datameta );
Kettledatabaserepository rep = new kettledatabaserepository ();
Rep. INIT (repinfo );
Rep. Connect ("admin", "admin ");

Repositorydirectoryinterface dir = new repositorydirectory ();
Dir. setobjectid (REP. getrootdirectoryid ());

Transmeta tranmeta = rep. loadtransformation (REP. gettransformationid (transname, DIR), null );
Trans trans = new Trans (tranmeta );
Trans.exe cute (null );
Trans. waituntilfinished ();
} Catch (kettleexception e ){
E. printstacktrace ();
}
}

}

 

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.