java程序调用kettle

(1).将相应的kettle的jar包导入的java项目,主要的jar包有一下几个。

(2).java程序。

package cn.com.taiji.oosweb.test.web;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class test {
//public static void main(String[] args) throws IOException {
//
// String basename = "cn.com.taiji.oosweb.test.web.common";
// Locale currentLocale = Locale.getDefault();
//
// Locale cn = Locale.CHINA;//中文
// Locale us = Locale.US;//英文
// ResourceBundle myResourcesCN = ResourceBundle.getBundle(basename,cn);
//
// String usernameCN = myResourcesCN.getString("close");
// System.out.println(usernameCN);
//}

//public void readResources() throws IOException{
// InputStream is = this.getClass().getResourceAsStream("/conf/common.properties");
// System.out.println((char)is.read());
// is.close();
//}





/**
* 本测试类慎用!!!!!!!
*
* @param args
*/
public static void main(String[] args) {
String datetime = "2014-12-19 23:20:45";
String[] params = {"707", datetime}; // 传递参数
String path = "C:\\Users\\administor\\Desktop\\kettle\\kettleTest.kjb";
for (int i = 0; i < 3; i++) {
params[0] += i;
runJob(params, path);
}
}


/**
* java 调用 kettle 的job
*
* @param jobname
* 如: String fName= "D:\\kettle\\informix_to_am_4.ktr";
*/
public static void runJob(String[] params, String jobPath) {
try {
KettleEnvironment.init();
// jobname 是Job脚本的路径及名称
JobMeta jobMeta = new JobMeta(jobPath, null);
Job job = new Job(null, jobMeta);
// 向Job 脚本传递参数,脚本中获取参数值:${参数名}
// job.setVariable(paraname, paravalue);
job.setVariable("id", params[0]);
System.err.println(params[0]+"=========="+params[1]);
job.setVariable("dt", params[1]);
job.start();
job.waitUntilFinished();
if (job.getErrors() > 0) {
throw new Exception(
"There are errors during job exception!(执行job发生异常)");
}
} catch (Exception e) {
e.printStackTrace();
}
}




/**
* 运行转换文件方法
* @param params 多个参数变量值
* @param ktrPath 转换文件的路径,后缀ktr
*/
public static void runTransfer(String[] params, String ktrPath) {
Trans trans = null;
try {
// // 初始化
// 转换元对象
//KettleEnvironment.init();// 初始化
EnvUtil.environmentInit();
TransMeta transMeta = new TransMeta(ktrPath);
// 转换
trans = new Trans(transMeta);

// 执行转换
trans.execute(params);
// 等待转换执行结束
trans.waitUntilFinished();
// 抛出异常
if (trans.getErrors() > 0) {
throw new Exception(
"There are errors during transformation exception!(传输过程中发生异常)");
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

 

(3).kettle的画法。

1.

2.

3.

 

posted @ 2017-07-25 09:27  baby孔祥超  阅读(4739)  评论(0编辑  收藏  举报