隨筆-200  評論-148  文章-15  trackbacks-0
          轉自

          http://blog.csdn.net/chen978616649/article/details/9063695


          代碼如下:調用kettle 4.2版本


          package test;


          import org.apache.commons.lang.StringUtils;
          import org.apache.commons.vfs.FileSelector;
          import org.pentaho.di.core.KettleEnvironment;
          import org.pentaho.di.core.database.DatabaseMeta;
          import org.pentaho.di.core.exception.KettleException;
          import org.pentaho.di.core.util.EnvUtil;
          import org.pentaho.di.job.Job;
          import org.pentaho.di.job.JobMeta;
          import org.pentaho.di.repository.RepositoryDirectory;
          import org.pentaho.di.repository.RepositoryDirectoryInterface;
          import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
          import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
          import org.pentaho.di.trans.Trans;
          import org.pentaho.di.trans.TransMeta;


          import com.longtech.configuration.BaseConfiguration;
          import com.platform.common.util.LoggerUtil;


          /**
           * 
           * @Description:  java調用kettle 數據庫型資料庫中的轉換
           * @author chen 此類是調用kettle 4.2版本的測試類
           * @version 1.0, 
           * @date 2013-5-23 下午02:40:50
           */
          public class ExecRepositoryTrans {
          /**
          * 本測試類慎用!!!!!!!
          * @param args
          */
          public static void main(String[] args) {
          //D:\java\workspace\sics\src\config\kettle
          String classesdir = BaseConfiguration.getString("classesdir");
          String jobName = BaseConfiguration.getString("kettle.path.file");
          if(StringUtils.isNotEmpty(jobName)){
          LoggerUtil.info(ExecRepositoryTrans.class, "執行kettle job start-------------------");

          runJob(classesdir + jobName);

          LoggerUtil.info(ExecRepositoryTrans.class, "執行kettle job end -------------------");
          }else{

          LoggerUtil.info(ExecRepositoryTrans.class, "請配置kettle.path.file job 的路徑文件");
          }

          }
          /**
          *java 調用kettle 轉換 
          */
          public static void runTransfer(){
          Trans trans=null;  
          FileSelector f = null;
          try {
          // 初始化  
                      String fName= "D:\\kettle\\informix_to_am_4.ktr";
                      // 轉換元對象  
                  KettleEnvironment.init();//初始化
                  EnvUtil.environmentInit();
                      TransMeta transMeta = new TransMeta(fName);
                      // 轉換  
                      trans = new Trans(transMeta);  
                      // 執行轉換  
                      trans.execute(null);   
                      // 等待轉換執行結束  
                      trans.waitUntilFinished();  
                      //拋出異常  
                      if(trans.getErrors()>0){  
                          throw new Exception("There are errors during transformation exception!(傳輸過程中發生異常)");  
                      }  
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          /**
          * java 調用 kettle 的job
          * @param jobname 如: String fName= "D:\\kettle\\informix_to_am_4.ktr";
          */
          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) {   
           throw new Exception("There are errors during job exception!(執行job發生異常)");  
           }   
          } catch (Exception e) {   
          e.printStackTrace();
          }   
          }
          /**
          * 配置數據源 調用資源庫中的相關job 、transfer
          */
          public static void dbResource(){
          String transName="t1";  
                   
                 try {  
                     KettleEnvironment.init();  
                     DatabaseMeta dataMeta = new DatabaseMeta("KettleDBRep","MSSQL","Native","127.0.0.1","etl","1433","sa","bsoft");  
                     KettleDatabaseRepositoryMeta repInfo = new KettleDatabaseRepositoryMeta();  
                     repInfo.setConnection(dataMeta);  
                     KettleDatabaseRepository rep = new KettleDatabaseRepository();  
                     rep.init(repInfo);  
                     rep.connect("admin", "admin");  
                       
                     RepositoryDirectoryInterface dir = new RepositoryDirectory();  
                     dir.setObjectId(rep.getRootDirectoryID());  
                       
                     TransMeta tranMeta = rep.loadTransformation(rep.getTransformationID(transName, dir), null);  
                     Trans trans = new Trans(tranMeta);  
                     trans.execute(null);  
                     trans.waitUntilFinished();  
                 } catch (KettleException e) {  
                     e.printStackTrace();  
                 }  
          }


          }

          posted on 2014-03-02 08:20 無聲 閱讀(1642) 評論(0)  編輯  收藏 所屬分類: 職場生活
          主站蜘蛛池模板: 溧阳市| 鄂州市| 富源县| 鄂温| 辽中县| 宣化县| 阳谷县| 翁牛特旗| 嘉黎县| 万年县| 观塘区| 浦城县| 自治县| 高雄市| 青浦区| 古田县| 许昌县| 平塘县| 壶关县| 泰兴市| 石阡县| 祁阳县| 江门市| 郸城县| 东乡县| 辛集市| 陇南市| 那坡县| 木兰县| 常州市| 巫山县| 分宜县| 黄平县| 兴和县| 永州市| 遵义市| 东丽区| 汉沽区| 尚志市| 平遥县| 霍林郭勒市|