Java调用Kettle执行任务或转换
2013-04-12 15:58
591 查看
转载java调用kettle,以供学习。
原文地址:/article/2589828.html 作者:scorpio3k
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 plaincopy
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);
// job
Job job = new Job(log, StepLoader.getInstance(), null, jobMeta);
jobMeta.setInternalKettleVariables(job);
// 执行job
job.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);
// job
Job job = new Job(log, steploader, rep, jobmeta);
// 执行job
job.execute();
// 等待job执行结束
job.waitUntilFinished();
}
}
本示例只能实现一次调用,暂时不知道如何通过API设定运行时间,但可以结合Spring和Quartz,设定定时调度,以便实现调度目标。
原文地址:/article/2589828.html 作者:scorpio3k
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 plaincopy
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);
// job
Job job = new Job(log, StepLoader.getInstance(), null, jobMeta);
jobMeta.setInternalKettleVariables(job);
// 执行job
job.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);
// job
Job job = new Job(log, steploader, rep, jobmeta);
// 执行job
job.execute();
// 等待job执行结束
job.waitUntilFinished();
}
}
本示例只能实现一次调用,暂时不知道如何通过API设定运行时间,但可以结合Spring和Quartz,设定定时调度,以便实现调度目标。
相关文章推荐
- Java调用Kettle执行任务或转换
- BI:Java调用Kettle执行任务或转换
- Java调用Kettle执行任务或转换
- java通过kettle的API调用已经创建好的转换(transformation)或任务(job)
- java调用kettle向job(任务)和transformation(转换)传递参数实例
- java调用kettle向job(任务)和transformation(转换)传递参数实例
- 用java程序调用ffmpeg执行视频文件格式转换flv
- Java调用执行kettle中的tran并且传参
- Java项目中调用Kettle转换
- java中调用kettle转换文件
- java中调用kettle转换文件
- 用java程序调用ffmpeg执行视频文件格式转换flv
- 用java程序调用ffmpeg执行视频文件格式转换flv
- kettle 程序调用执行ktr转换示例代码
- java中调用kettle转换文件
- java程序调用ffmpeg执行视频文件格式转换flv
- kettle 程序调用执行ktr转换示例代码
- [视频处理]用java程序调用ffmpeg执行视频文件格式转换flv
- linux下crontab定时执行任务的配置-crontab调用java
- 关于Java Web 使用Spring中使用Quartz(定时调用、实现固定时间执行), 触发定时器(执行某些任务)的实例