</pre><pre name="code" class="html">package com.test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.Repository;
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;
/**
* java调用kettle数据库型资料库中的任务
*
* @author
*
*/
public class JobTest {
private static String transName = "Main.job";
/**
* @param args
* @throws KettleException
*/
public static void main(String[] args) throws KettleException {
try {
KettleEnvironment.init();
KettleDatabaseRepository repository = new KettleDatabaseRepository();
DatabaseMeta dataMeta = new DatabaseMeta("enfo_bi", "Mysql",
"Native", "127.0.0.1", "kettle", "3306", "root", "");
KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta(
"enfo_bi", "enfo_bi", "king description", dataMeta);
repository.init(kettleDatabaseMeta);
repository.connect("admin", "admin");
RepositoryDirectoryInterface directory = repository.findDirectory("/");
String excuteType = transName.substring(transName.lastIndexOf(".")+1,transName.length());
transName = transName.substring(0,transName.lastIndexOf("."));
if (excuteType.equals("job")) {
JobMeta meta = ((Repository) repository).loadJob(transName,
directory, null, null);
Job job = new Job(repository, meta);
job.run();
job.waitUntilFinished();
if (job.getErrors() > 0) {
System.err.println(excuteType+":Transformation run Failure!");
} else {
System.out.println(excuteType+":Transformation run successfully!");
}
} else {
TransMeta transformationMeta = ((Repository) repository)
.loadTransformation(transName, directory, null, true,
null);
Trans trans = new Trans(transformationMeta);
trans.execute(null);
trans.waitUntilFinished();
if (trans.getErrors() > 0) {
System.err.println(excuteType+":Transformation run Failure!");
} else {
System.out.println(excuteType+":Transformation run successfully!");
}
}
} catch (KettleException e) {
e.printStackTrace();
}
}
}
java调用Kettle5.3的任务
最新推荐文章于 2024-05-28 15:49:51 发布