BI:Java调用Kettle执行任务或转换

来源:互联网 发布:网络金庸群侠传 编辑:程序博客网 时间:2024/06/04 18:40

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版本调用方法会有不同。

import org.pentaho.di.core.database.DatabaseMeta;import org.pentaho.di.core.exception.KettleException;import org.pentaho.di.core.logging.LogWriter;import org.pentaho.di.core.util.EnvUtil;import org.pentaho.di.job.Job;import org.pentaho.di.job.JobEntryLoader;import org.pentaho.di.job.JobMeta;import org.pentaho.di.repository.Repository;import org.pentaho.di.repository.RepositoryDirectory;import org.pentaho.di.repository.RepositoryMeta;import org.pentaho.di.repository.UserInfo;import org.pentaho.di.trans.StepLoader;import org.pentaho.di.trans.Trans;import org.pentaho.di.trans.TransMeta;/** * Java调用Kettle示例代码 *  * @author 李文锴 * @since 2012-8-14 下午03:50:00 *  */public class KettleTest {public static void main(String[] args) throws KettleException {String filename = "voucher.ktr";// callNativeTrans(filename);// executeTrans();// executeJobs();callNativeTransWithParam(filename);System.out.println("ok");}/** * 调用本地的转换文件 *  * @Description: * @param transFileName * @throws KettleException * @author 李文锴 * @since:2012-8-15 下午02:58:41 */public static void callNativeTrans(String transFileName) throws KettleException {// 初始化EnvUtil.environmentInit();StepLoader.init();// 转换元对象TransMeta transMeta = new TransMeta(transFileName);// 转换Trans trans = new Trans(transMeta);// 执行转换trans.execute(null);// 等待转换执行结束trans.waitUntilFinished();}/** * 调用本地的转换文件(带参数) *  * @Description: * @param transFileName * @throws KettleException * @author 李文锴 * @since:2012-8-15 下午02:58:54 */public static void callNativeTransWithParam(String transFileName) throws KettleException {// 初始化EnvUtil.environmentInit();StepLoader.init();// 转换元对象TransMeta transMeta = new TransMeta(transFileName);// 转换Trans trans = new Trans(transMeta);String[] params = {};// 执行转换trans.execute(params);// 等待转换执行结束trans.waitUntilFinished();}/** * 执行存储在数据库资源库中的转换 *  * @Description: * @throws KettleException * @author 李文锴 * @since:2012-8-15 下午02:59:14 */public static void executeTrans() throws KettleException {// 初始化EnvUtil.environmentInit();StepLoader.init();// 日志LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DEBUG);// 用户UserInfo userInfo = new UserInfo();userInfo.setLogin("admin");userInfo.setPassword("admin");// 数据库连接元对象(连接名称,不必与kettle中配置的保持一致:数据库类型:连接方式(kettle支持的连接方式):资源库IP:资源库实例名:资源库端口:资源库用户名:资源库用户密码)DatabaseMeta connection = new DatabaseMeta("", "Oracle", "Native", "192.168.3.232", "NSDEV", "1521", "nstcsa3441", "671468");// 资源库元对象RepositoryMeta repinfo = new RepositoryMeta();repinfo.setConnection(connection);// 资源库Repository rep = new Repository(log, repinfo, userInfo);// 连接资源库rep.connect("");// 资源库目录对象RepositoryDirectory dir = new RepositoryDirectory(rep);// 转换元对象TransMeta transMeta = new TransMeta(rep, "凭证(N9->EVC2)", dir);// 转换Trans trans = new Trans(transMeta);// 执行转换trans.execute(null);// 等待转换执行结束trans.waitUntilFinished();}/** * 执行本地的任务文件 *  * @Description: * @param jobFileName * @throws KettleException * @author 李文锴 * @since:2012-8-15 下午02:59:34 */public static void callNativeJob(String jobFileName) throws KettleException {// 初始化EnvUtil.environmentInit();JobEntryLoader.init();StepLoader.init();// 日志LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DETAILED);// job元对象JobMeta jobMeta = new JobMeta(log, jobFileName, null);// jobJob job = new Job(log, StepLoader.getInstance(), null, jobMeta);jobMeta.setInternalKettleVariables(job);// 执行jobjob.execute();// 等待job执行结束job.waitUntilFinished();}/** * 执行数据库资源库中的任务 *  * @Description: * @throws KettleException * @author 李文锴 * @since:2012-8-15 下午02:59:45 */public static void executeJobs() throws KettleException {// 初始化EnvUtil.environmentInit();JobEntryLoader.init();StepLoader.init();// 日志LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DETAILED);// 用户UserInfo userInfo = new UserInfo();userInfo.setLogin("admin");userInfo.setPassword("admin");// 数据库连接元对象DatabaseMeta connection = new DatabaseMeta("", "Oracle", "Native", "192.168.3.232", "NSDEV", "1521", "nstcsa3441", "671468");// 资源库元对象RepositoryMeta repinfo = new RepositoryMeta();repinfo.setConnection(connection);// 资源库Repository rep = new Repository(log, repinfo, userInfo);// 连接资源库rep.connect("");// 资源库目录对象RepositoryDirectory dir = new RepositoryDirectory(rep);// 步骤加载对象StepLoader steploader = StepLoader.getInstance();// job元对象JobMeta jobmeta = new JobMeta(log, rep, "4.账户每日余额", dir);// jobJob job = new Job(log, steploader, rep, jobmeta);// 执行jobjob.execute();// 等待job执行结束job.waitUntilFinished();}}


本示例只能实现一次调用,暂时不知道如何通过API设定运行时间,但可以结合Spring和Quartz,设定定时调度,以便实现调度目标。

 

 

 

 

原创粉丝点击