java调用kettle工具类

/**
 * 
 */
package com.fms.xx.kettle;


import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.Result;
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.JobEntryListener;
import org.pentaho.di.job.JobListener;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.job.entry.JobEntryCopy;
import org.pentaho.di.job.entry.JobEntryInterface;
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;




public class KettleTest {
private static String repName = "KETTLE_4";
private static String dbType = "MS SQL Server";//"Oracle";
private static String dbAccess = "Native";
private static String dbHost = "localhost";//"localhost";
private static String dbName = "kettle_DB";//"orcl";
private static String dbPort = "1433";//"1521";
private static String dbUser = "sa";//"etl123";
private static String dbPass = "sa123";//"etl123";


public static void main(String[] args) {
KettleTest ktp = new KettleTest();
ktp.transferProject();
}
public boolean transferProject() {
boolean isTrue=true;
try {
//初始化环境
KettleEnvironment.init();
//创建DB资源库
        KettleDatabaseRepository repository = new KettleDatabaseRepository();
    DatabaseMeta databaseMeta = new DatabaseMeta(repName, dbType,dbAccess, dbHost, dbName, dbPort, dbUser, dbPass);
    // 选择资源库
        KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta("Kettle", "Kettle", "Transformation description",databaseMeta);
        repository.init(kettleDatabaseRepositoryMeta);
        //连接资源库
        repository.connect("admin", "admin");
        //根据变量查找到模型所在的目录对象
        RepositoryDirectoryInterface directory = repository.findDirectory("");///xx2/xx数据库中资源库路径,尽量不要修改
        JobMeta jobMeta =  ((Repository) repository).loadJob("Job1", directory, null, null); 
Job job = new Job(repository, jobMeta);
job.start();
 
job.addJobListener(new JobListener() {
 
@Override
public void jobStarted(Job arg0) throws KettleException {

}

@Override
public void jobFinished(Job arg0) throws KettleException {
//isTrue = true;
System.out.println("工作完成了");

}
});
 
job.addJobEntryListener(new JobEntryListener() {

@Override
public void beforeExecution(Job arg0, JobEntryCopy arg1,JobEntryInterface arg2) {

}

@Override
public void afterExecution(Job arg0, JobEntryCopy arg1,JobEntryInterface arg2, Result arg3) {

System.out.println(arg1+"--------"+arg3+"*****"+arg2);
}
});
 
 
job.waitUntilFinished();
Result result = job.getResult();
System.out.println("result"+result);
return isTrue;   


} catch(Throwable e) {
e.printStackTrace();
return  false;
}
}




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值