- 手动加依赖
由于maven官网没有kettle相关的依赖,需要自己打包
mvn install:install-file -Dfile=kettle-core-8.2.0.0-342.jar -Dpackaging=jar -DgroupId=pentaho-kettle -DartifactId=kettle-core -Dversion=8.2.0.0-342
mvn install:install-file -Dfile=kettle-engine-8.2.0.0-342.jar -Dpackaging=jar -DgroupId=pentaho-kettle -DartifactId=kettle-engine -Dversion=8.2.0.0-342
mvn install:install-file -Dfile=metastore-8.2.0.0-342.jar -Dpackaging=jar -DgroupId=pentaho-kettle -DartifactId=metastore -Dversion=8.2.0.0-342
- 加入依赖
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
<version>8.2.0.0-342</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
<version>8.2.0.0-342</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>metastore</artifactId>
<version>8.2.0.0-342</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
3.ktr方法
/**
* Execute the Kettle ktr file
*
* @param filePath ktr 文件路径
* @param params 参数
* @throws KettleException kettle异常
*/
public void executeKettleKtr(String filePath, Map<String, Object> params) throws KettleException {
File file = FileUtil.file(filePath);
String path = file.getPath();
System.out.println(path);
// 初始化
KettleEnvironment.init();
// 加载文件
TransMeta transMeta = new TransMeta(path);
Trans trans = new Trans(transMeta);
// 区分参数列表有还是没有,如果有,还要考虑参数不全的情况
String[] jobMetaParams = transMeta.listParameters();
List<String> keys = new ArrayList<>();
Collections.addAll(keys, jobMetaParams);
for (String key : params.keySet()) {
// 先判断是否有此参数,如果没有要先加参数,否则设置参数无效果
if (!keys.contains(key)) {
transMeta.addParameterDefinition(key, null, "");
trans.addParameterDefinition(key, null, "");
}
// 赋值, 通过上面的addParams...将没有的key添加进去后,就可以用下面通用的赋值
transMeta.setParameterValue(key, params.get(key).toString());
trans.setParameterValue(key, params.get(key).toString());
}
trans.prepareExecution(null);
trans.startThreads();
// 等待执行完毕
trans.waitUntilFinished();
}
- kjb方法
/**
* Execute the Kettle kjb file
*
* @param filePath kjb 文件路径
* @param params 参数
* @throws KettleException kettle异常
*/
public void executeKettleJob(String filePath, Map<String, Object> params) throws KettleException {
File file = FileUtil.file(filePath);
String path = file.getPath();
// 初始化
KettleEnvironment.init();
// 加载文件
// Job脚本的路径及名称
JobMeta jobMeta = new JobMeta(path, null);
Job job = new Job(null, jobMeta);
// 区分参数列表有还是没有,如果有,还要考虑参数不全的情况
String[] jobMetaParams = jobMeta.listParameters();
List<String> keys = new ArrayList<>();
Collections.addAll(keys, jobMetaParams);
for (String key : params.keySet()) {
// 先判断是否有此参数,如果没有要先加参数,否则设置参数无效果
if (!keys.contains(key)) {
jobMeta.addParameterDefinition(key, null, "");
job.addParameterDefinition(key, null, "");
}
// 赋值, 通过上面的addParams...将没有的key添加进去后,就可以用下面通用的赋值
jobMeta.setParameterValue(key, params.get(key).toString());
job.setParameterValue(key, params.get(key).toString());
}
job.start();
job.waitUntilFinished();
}
- 测试
@Test
public void testKettleKtr() throws KettleException {
Map<String, Object> params = new HashMap<>();
// 注意日期要加个单引号
params.put("BATCH_DATE", "'2023-03-26'");
params.put("WEEK_NUM", "7");
// 文件名称
String filePath = "C:\\Users\\abc.ktr";
executeKettleKtr(filePath, params);
}
- 注意事项
- 传参为日期时,要加单引号,否者会当做减法运算。 params.put(“BATCH_DATE”, “‘2023-03-26’”);而不是 params.put(“BATCH_DATE”, “2023-03-26”);