kettle 使用JAVA代碼進行執行

來源:互聯網
上載者:User

標籤:檔案儲存體   數值   err   代碼   tun   rac   傳輸過程   dem   ble   

kettle 設計完成之後,可以在設計工具中進行調用,也可以使用java代碼進行調用。

 

1.通過檔案方式執行轉換。

 

public static void runTransfer(String[] params, String ktrPath) { 
        Trans trans = null; 
        try { 
            // // 初始化 
            // 轉換元對象 
            KettleEnvironment.init();// 初始化 
            EnvUtil.environmentInit(); 
            TransMeta transMeta = new TransMeta(ktrPath); 
            // 轉換 
            trans = new Trans(transMeta); 
             
            // 執行轉換 
            trans.execute(params); 
            // 等待轉換執行結束 
            trans.waitUntilFinished(); 
            // 拋出異常 
            if (trans.getErrors() > 0) { 
                throw new Exception( 
                        "There are errors during transformation exception!(傳輸過程中發生異常)"); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

2.通過檔案方式執行job

public static void runJob(Map<String,String> maps, String jobPath) { 
        try { 
            KettleEnvironment.init(); 
            // jobname 是Job指令碼的路徑及名稱 
            JobMeta jobMeta = new JobMeta(jobPath, null); 
            Job job = new Job(null, jobMeta); 
            // 向Job 指令碼傳遞參數,指令碼中擷取參數值:${參數名} 
            // job.setVariable(paraname, paravalue); 
            Set<Entry<String, String>> set=maps.entrySet();
            for(Iterator<Entry<String, String>> it=set.iterator();it.hasNext();){
                Entry<String, String> ent=it.next();
                job.setVariable(ent.getKey(), ent.getValue()); 
            }
            job.start(); 
            job.waitUntilFinished(); 
            if (job.getErrors() > 0) { 
                throw new Exception( 
                        "There are errors during job exception!(執行job發生異常)"); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

 

3.執行資源庫的中的轉換。

public static void runWithDb() throws KettleException{
        KettleEnvironment.init();
        //建立DB資產庫
        KettleDatabaseRepository repository=new KettleDatabaseRepository();
        DatabaseMeta databaseMeta=new DatabaseMeta("kettle","mysql","jdbc","localhost","kettle","3306","root","root");
        //選擇資產庫
        KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta=new KettleDatabaseRepositoryMeta("kettle","kettle","Transformation description",databaseMeta);
        repository.init(kettleDatabaseRepositoryMeta);
        //串連資產庫
        repository.connect("admin","admin");
        RepositoryDirectoryInterface directoryInterface=repository.loadRepositoryDirectoryTree();
        //選擇轉換
        TransMeta transMeta=repository.loadTransformation("demo1",directoryInterface,null,true,null);
        Trans trans=new Trans(transMeta);
        trans.execute(null);
        trans.waitUntilFinished();//等待直到資料結束
        if(trans.getErrors()>0){
            System.out.println("transformation error");
        }else{
            System.out.println("transformation successfully");
        }
   }


可以將轉換檔儲存到資產庫,通過程式進行調用。

kettle 使用JAVA代碼進行執行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.