import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.logicalcobwebs.proxool.admin.servlet.AdminServlet;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.trans.StepLoader;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.performance.StepPerformanceSnapShot;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 解释
runTransformation("c:/ss.ktr");
}
public static void runTransformation(String filename) throws IOException {
try {
//初始化任务
StepLoader.init();
EnvUtil.environmentInit();
TransMeta transMeta = new TransMeta(filename);
transMeta.setCapturingStepPerformanceSnapShots(true);
Trans trans = new Trans(transMeta);
trans.setMonitored(true);
trans.setInitializing(true);
trans.setPreparing(true);
trans.setRunning(true);
trans.setSafeModeEnabled(true);
trans.execute(null); // You can pass arguments instead of null.
//放入一个MAP存储结果
HashMap map = new HashMap();
trans.setStepPerformanceSnapShots(map);
while (!trans.isFinished()) {
if (trans.getStepPerformanceSnapShots() != null&& trans.getStepPerformanceSnapShots().size() > 0) {
//得到所有步骤
Map<String, List<StepPerformanceSnapShot>> SnapShots = trans.getStepPerformanceSnapShots();
//输出动态监控情况
Iterator it = SnapShots.entrySet().iterator();
String oneTimeOneStepInfo = "";
String ontTimeAllStepInfo = "";
while(it.hasNext())
{
Entry en = (Entry)it.next();
//步骤当前情况
ArrayList SnapShotList = (ArrayList) en.getValue();
if (SnapShotList != null && SnapShotList.size() > 0) {
StepPerformanceSnapShot SnapShot = (StepPerformanceSnapShot) SnapShotList.get(SnapShotList.size() - 1);
oneTimeOneStepInfo = ( "StepName:"+SnapShot.getStepName()+";"
+"Errors: " + SnapShot.getErrors() + ";"
+ "InputBufferSize: "+ SnapShot.getInputBufferSize()+ ";"
+ "LinesInput: " + SnapShot.getLinesInput()+ ";"
+ "LinesOutput: "+ SnapShot.getLinesOutput() + ";"
+ "LinesRead: "+ SnapShot.getLinesRead()+ ";"
+ "LinesRejected: "+ SnapShot.getLinesRejected()+ ";"
+ "LinesUpdated: "+ SnapShot.getLinesUpdated()+ ";"
+ "LinesWritten: "+ SnapShot.getLinesWritten()+ ";"
+ "OutputBufferSize: "+ SnapShot.getOutputBufferSize()+ ";"
+ "StepCopy: " + SnapShot.getStepCopy()+ ";"
+ "TimeDifference: "+ SnapShot.getTimeDifference()+ ";"
+ "TotalErrors: "+ SnapShot.getTotalErrors()+ ";"
+ "TotalLinesInput: "+ SnapShot.getTotalLinesInput()+ ";"
+ "TotalLinesOutput: "+ SnapShot.getTotalLinesOutput()+ ";"
+ "TotalLinesRead: "+ SnapShot.getTotalLinesRead()+ ";"
+ "TotalLinesRejected: "+ SnapShot.getTotalLinesRejected()+ ";"
+ "TotalLinesUpdated: "+ SnapShot.getTotalLinesUpdated()+ ";"
+ "TotalLinesWritten: "+ SnapShot.getTotalLinesWritten()+ ";"
+ "Date:"+ SnapShot.getDate() + "\n");
ontTimeAllStepInfo+=oneTimeOneStepInfo;
}
System.out.println(ontTimeAllStepInfo+"\n\n\n\n");
}
}
}
System.out.println("end............");
} catch (KettleException e) {
// TODO Put your exception-handling code here.
System.out.println(e);
}
}
}