Java调用Kettle转换。
获取日志输出的方式:
String all_msg = KettleLogStore.getAppender().getBuffer().toString();
KettleLogStore.getAppender().clear();
String reg = "KettleException:\\s*[0-9/\\s:-]+[^\\.]+\\.\\d\\s-\\s([^\r\n]+)[\\s\\S]*?完成处理[^\\)]*?R=(\\d+)";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(all_msg);
if (m.find()) {
error_info[0] = m.group(1);
error_info[1] = m.group(2);
return null;
}
获取每行的数据输出:
final List<RowMetaAndData> rows = new ArrayList<RowMetaAndData>();
RowListener rowListener = new RowAdapter() {
@Override
public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
rows.add(new RowMetaAndData(rowMeta, row));
}
};
List<StepMetaDataCombi> steps = trans.getSteps();
String stepname = steps.get(steps.size() - 1).stepname;
StepInterface stepInterface = trans.findRunThread(stepname);
stepInterface.addRowListener(rowListener);
web项目中调用kettle的转换,KettleEnvironment.init() 调用卡住的问题解决,个人测试是因为 user.dir 的设置问题导致
if (!KettleEnvironment.isInitialized()) {
// 获得执行类的当前路径
String user_dir = System.getProperty("user.dir");
// Kettle初始化需要修改相应的配置路径
System.setProperty("KETTLE_HOME", kettleHome);
System.setProperty("user.dir", kettleHome);
// 运行环境初始化(设置主目录、注册必须的插件等)
KettleEnvironment.init();
// Kettle初始化完毕,还原执行类的当前路径
System.setProperty("user.dir", user_dir);
}