版权声明:本文为博主原创文章,未经博主允许。
Java调用Kettle执行任务或转换,需要使用Kettle中的jar,可以先导入lib目录中的几个基本的jar,如:kettle-core.jar、kettle-db.jar、kettle-engine.jar ,其它jar根据情况进行添加,所需的jar在<kettle-home>\lib、<kettle-home>\libext下面都可以找到,本示例引用的jar如下图:
之后编写代码测试Java调用,调用前先使用Kettle的设计器设计了一个转换,取名为voucher.ktr。另外,本示例使用的是Kettle3.2的版本,据说4.1版本调用方法会有不同。
[java]view plain copy importorg.pentaho.di.core.database.DatabaseMeta;importorg.pentaho.di.core.exception.KettleException;importorg.pentaho.di.core.logging.LogWriter;importorg.pentaho.di.core.util.EnvUtil;importorg.pentaho.di.job.Job;importorg.pentaho.di.job.JobEntryLoader;importorg.pentaho.di.job.JobMeta;importorg.pentaho.di.repository.Repository;importorg.pentaho.di.repository.RepositoryDirectory;importorg.pentaho.di.repository.RepositoryMeta;importorg.pentaho.di.repository.UserInfo;importorg.pentaho.di.trans.StepLoader;importorg.pentaho.di.trans.Trans;importorg.pentaho.di.trans.TransMeta;/***Java调用Kettle示例代码**@author李文锴*@since-8-14下午03:50:00**/publicclassKettleTest{publicstaticvoidmain(String[]args)throwsKettleException{Stringfilename="voucher.ktr";//callNativeTrans(filename);//executeTrans();//executeJobs();callNativeTransWithParam(filename);System.out.println("ok");}/***调用本地的转换文件**@Description:*@paramtransFileName*@throwsKettleException*@author李文锴*@since:-8-15下午02:58:41*/publicstaticvoidcallNativeTrans(StringtransFileName)throwsKettleException{//初始化EnvUtil.environmentInit();StepLoader.init();//转换元对象TransMetatransMeta=newTransMeta(transFileName);//转换Transtrans=newTrans(transMeta);//执行转换trans.execute(null);//等待转换执行结束trans.waitUntilFinished();}/***调用本地的转换文件(带参数)**@Description:*@paramtransFileName*@throwsKettleException*@author李文锴*@since:-8-15下午02:58:54*/publicstaticvoidcallNativeTransWithParam(StringtransFileName)throwsKettleException{//初始化EnvUtil.environmentInit();StepLoader.init();//转换元对象TransMetatransMeta=newTransMeta(transFileName);//转换Transtrans=newTrans(transMeta);String[]params={};//执行转换trans.execute(params);//等待转换执行结束trans.waitUntilFinished();}/***执行存储在数据库资源库中的转换**@Description:*@throwsKettleException*@author李文锴*@since:-8-15下午02:59:14*/publicstaticvoidexecuteTrans()throwsKettleException{//初始化EnvUtil.environmentInit();StepLoader.init();//日志LogWriterlog=LogWriter.getInstance("TransTest.log",true,LogWriter.LOG_LEVEL_DEBUG);//用户UserInfouserInfo=newUserInfo();userInfo.setLogin("admin");userInfo.setPassword("admin");//数据库连接元对象(连接名称,不必与kettle中配置的保持一致:数据库类型:连接方式(kettle支持的连接方式):资源库IP:资源库实例名:资源库端口:资源库用户名:资源库用户密码)DatabaseMetaconnection=newDatabaseMeta("","Oracle","Native","192.168.3.232","NSDEV","1521","nstcsa3441","671468");//资源库元对象RepositoryMetarepinfo=newRepositoryMeta();repinfo.setConnection(connection);//资源库Repositoryrep=newRepository(log,repinfo,userInfo);//连接资源库rep.connect("");//资源库目录对象RepositoryDirectorydir=newRepositoryDirectory(rep);//转换元对象TransMetatransMeta=newTransMeta(rep,"凭证(N9->EVC2)",dir);//转换Transtrans=newTrans(transMeta);//执行转换trans.execute(null);//等待转换执行结束trans.waitUntilFinished();}/***执行本地的任务文件**@Description:*@paramjobFileName*@throwsKettleException*@author李文锴*@since:-8-15下午02:59:34*/publicstaticvoidcallNativeJob(StringjobFileName)throwsKettleException{//初始化EnvUtil.environmentInit();JobEntryLoader.init();StepLoader.init();//日志LogWriterlog=LogWriter.getInstance("TransTest.log",true,LogWriter.LOG_LEVEL_DETAILED);//job元对象JobMetajobMeta=newJobMeta(log,jobFileName,null);//jobJobjob=newJob(log,StepLoader.getInstance(),null,jobMeta);jobMeta.setInternalKettleVariables(job);//执行jobjob.execute();//等待job执行结束job.waitUntilFinished();}/***执行数据库资源库中的任务**@Description:*@throwsKettleException*@author李文锴*@since:-8-15下午02:59:45*/publicstaticvoidexecuteJobs()throwsKettleException{//初始化EnvUtil.environmentInit();JobEntryLoader.init();StepLoader.init();//日志LogWriterlog=LogWriter.getInstance("TransTest.log",true,LogWriter.LOG_LEVEL_DETAILED);//用户UserInfouserInfo=newUserInfo();userInfo.setLogin("admin");userInfo.setPassword("admin");//数据库连接元对象DatabaseMetaconnection=newDatabaseMeta("","Oracle","Native","192.168.3.232","NSDEV","1521","nstcsa3441","671468");//资源库元对象RepositoryMetarepinfo=newRepositoryMeta();repinfo.setConnection(connection);//资源库Repositoryrep=newRepository(log,repinfo,userInfo);//连接资源库rep.connect("");//资源库目录对象RepositoryDirectorydir=newRepositoryDirectory(rep);//步骤加载对象StepLoadersteploader=StepLoader.getInstance();//job元对象JobMetajobmeta=newJobMeta(log,rep,"4.账户每日余额",dir);//jobJobjob=newJob(log,steploader,rep,jobmeta);//执行jobjob.execute();//等待job执行结束job.waitUntilFinished();}}
本示例只能实现一次调用,暂时不知道如何通过API设定运行时间,但可以结合spring和Quartz,设定定时调度,以便实现调度目标。
来源:/scorpio3k/article/details/7869196