kettle ETL java 調用 kettle job 或 transfer

來源:互聯網
上載者:User

代碼如下:調用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();  
       }  
}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.