步骤很简单,标红的地方就是要注意的地方.代码例子是调用job的,自测正常,转换也是一个道理,但是没有试验.
代码如下:
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogWriter;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobEntryLoader;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.StepLoader;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class EtlParams {
public static void main(String[] args) {
// TODO Auto-generated method stub
runJob("D:\\etl\\params1.kjb");
}
public static void runJob(String filename) {
try {
StepLoader.init();
EnvUtil.environmentInit();
JobEntryLoader.init();
JobMeta jobMeta = new JobMeta(LogWriter.getInstance(),filename,null);
Job job = new Job(LogWriter.getInstance(),StepLoader.getInstance() ,null,jobMeta);
job.getJobMeta().setInternalKettleVariables(job);
job.setVariable("XMLPATH", "D:\\etl\\gz_zb_zb");
job.setVariable("ZB_FJD", "23d267b7aad54f5b961f66fac791b356");
job.execute(); // You can pass arguments instead of null.
job.waitUntilFinished();
if ( job.getErrors() > 0 )
{
throw new RuntimeException( "There were errors during transformation execution." );
}
}
catch ( KettleException e ) {
System.out.println(e);
}
}
}
job里,在需要调用的地方直接${变量名},调用就可以了