Java程式呼叫kettle中的job(.kjb)和Trans(.ktr)

zuoluo2003發表於2017-06-04
  1. package com.p.serv.util;  
  2.   
  3. import org.pentaho.di.core.KettleEnvironment;  
  4. import org.pentaho.di.core.exception.KettleException;  
  5. import org.pentaho.di.core.exception.KettleXMLException;  
  6. import org.pentaho.di.job.Job;  
  7. import org.pentaho.di.job.JobMeta;  
  8. import org.pentaho.di.trans.Trans;  
  9. import org.pentaho.di.trans.TransMeta;  
  10.   
  11. public class KettleExecu {  
  12.   
  13.     public static void runJob(String jobname) {  
  14.         try {  
  15.             KettleEnvironment.init();  
  16.             // jobname 是Job指令碼的路徑及名稱  
  17.             JobMeta jobMeta = new JobMeta(jobname, null);  
  18.             Job job = new Job(null, jobMeta);  
  19.             // 向Job 指令碼傳遞引數,指令碼中獲取引數值:${引數名}  
  20.             // job.setVariable(paraname, paravalue);  
  21.             job.start();  
  22.             job.waitUntilFinished();  
  23.             if (job.getErrors() > 0) {  
  24.                 System.out.println("decompress fail!");  
  25.             }  
  26.         } catch (KettleException e) {  
  27.             System.out.println(e);  
  28.         }  
  29.     }  
  30.   
  31.     // 呼叫Transformation示例:  
  32.     public static void runTrans(String filename) {  
  33.         try {  
  34.             KettleEnvironment.init();  
  35.             TransMeta transMeta = new TransMeta(filename);  
  36.             Trans trans = new Trans(transMeta);  
  37.             trans.prepareExecution(null);  
  38.             trans.startThreads();  
  39.             trans.waitUntilFinished();  
  40.             if (trans.getErrors() != 0) {  
  41.                 System.out.println("Error");  
  42.             }  
  43.         } catch (KettleXMLException e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         } catch (KettleException e) {  
  47.             // TODO Auto-generated catch block  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.   
  52.     public static void main(String[] args) {  
  53.         String jobname = "F:\\ETL\\kettle\\例子\\測試\\複製多表\\copymanytablejob.kjb";  
  54.         runJob(jobname);  
  55.         // String filename="D:/syslog_All/spoon/csv_fm.ktr";  
  56.         // runTrans(filename);  
  57.     }  
  58.   
  59. }  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/76103/viewspace-2140269/,如需轉載,請註明出處,否則將追究法律責任。

相關文章