package com.kettle3.service;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.StepLoader;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class AutoKettleService {
public void executeKettle() throws KettleException {
System.out.println("执行kettle数据抓取任务开始!");
try {
StepLoader.init();
} catch (Exception ex) {
ex.printStackTrace();
}
String filePath = getKettleFilePath();
System.out.println("获取文件路径: "+filePath);
TransMeta meta = new TransMeta(filePath);
Trans trans = new Trans(meta);
trans.execute(null);
trans.waitUntilFinished();
trans.endProcessing("end");
System.out.println("执行kettle数据抓取任务结束!");
}
/**
* 获取WEB-INF目录下面kettle_demo_test.ktr文件的路径
* @return
*/
public static String getKettleFilePath()
{
String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
path=path.replace('/', '\\'); // 将/换成\
path=path.replace("file:", ""); //去掉file:
path=path.replace("classes\\", ""); //去掉class\
path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...
path+="kettle\\kettle_demo_test.ktr";
return path;
}
}
评论16
最新资源