关闭

Java程序调用kettle中的job及trans

1588人阅读 评论(0) 收藏 举报
分类:
package org.gdh.kettle.transform;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;

public class ReadJob {
public static void runJob(String jobname){ 
try { 
KettleEnvironment.init(); 
//jobname 是Job脚本的路径及名称 
JobMeta jobMeta = new JobMeta(jobname, null); 
Job job = new Job(null, jobMeta); 
//向Job 脚本传递参数,脚本中获取参数值:${参数名} 
//  job.setVariable(paraname, paravalue); 
job.start(); 
job.waitUntilFinished(); 
if (job.getErrors() > 0) { 
  System.out.println("decompress fail!"); 
} catch (KettleException e) { 
System.out.println(e); 
}
// 调用Transformation示例: 
public static void runTrans(String filename) { 
  try {
KettleEnvironment.init(); 
TransMeta transMeta = new TransMeta(filename); 
Trans trans = new Trans(transMeta); 
trans.prepareExecution(null); 
trans.startThreads(); 
trans.waitUntilFinished(); 
if (trans.getErrors()!=0) { 
System.out.println("Error"); 
}
} catch (KettleXMLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KettleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
     
public static void main(String[] args) {
String jobname="D:/syslog_All/spoon/job_csv_bsf.kjb";
runJob(jobname);
// String filename="D:/syslog_All/spoon/csv_fm.ktr";
// runTrans(filename);
}
}
 
-=================================
 
public class KettleTest {
public static void main(String[] args) {
  String filename = "/wang/work/study/Kettle/sample/excel_trans.ktr";
  try {
    KettleEnvironment.init();
    EnvUtil.environmentInit();  
    TransMeta transMeta = new TransMeta(filename);
    Trans trans = new Trans(transMeta);
    trans.execute(null); // You can pass arguments instead of null.
    trans.waitUntilFinished();
    if ( trans.getErrors() > 0 )
{
      throw new RuntimeException( "There were errors during transformation execution." );
}
  }
  catch (KettleException e ) {
    // TODO Put your exception-handling code here.
    System.out.println(filename);
    System.out.println(e);
  }
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:219843次
    • 积分:3248
    • 等级:
    • 排名:第11180名
    • 原创:8篇
    • 转载:322篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论