kettle 응용 프로그램 통합 코드

6303 단어 자바kettle
더 읽 기

package com.zjhcsoft.kettle.test;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.CentralLogStore;
import org.pentaho.di.core.logging.Log4jBufferAppender;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.RepositoryPluginType;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.RepositoriesMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryMeta;
import org.pentaho.di.repository.StringObjectId;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;

public class TestKettle {
	
	 /**
     *       --        
     * @author twr_wjz
     * @date 2011-11-17
     * @param filename       
     * @throws Exception
     */
    public void runFileTransformation(String filename) {
            try {
                    //        (     、        )
                    KettleEnvironment.init();
                    EnvUtil.environmentInit();
                    //    
                    TransMeta transMeta = new TransMeta(filename);
                    Trans trans = new Trans(transMeta);
                    //       You can pass arguments instead of null.
                    trans.execute(null); 
                    //         
                    trans.waitUntilFinished();
                    /*
                     *     
                     */
                    if ( trans.getErrors() > 0 )
                    {
                            throw new RuntimeException( "         。" );
                    }
            }
            catch ( KettleException e ) {
                // TODO Put your exception-handling code here.
                    System.out.println(e);
            }
    }
    /**
     *       --       
     * @author twr_wjz
     * @date 2011-11-17
     * @param repID    ID, :1 ----      ,    
     * @param transformationID   ID--       r_transformation ID_TRANSFORMATION
     * @param transformationVersion     --       r_transformation TRANS_VERSION
     * @throws Exception
     */
    /**
     * @param repID
     * @param transformationID
     * @param transformationVersion
     */
    public void runRepTransformation(String repID,String transformationID,String transformationVersion) {
            try {
                    //        (     、        )   
            KettleEnvironment.init();   
            RepositoriesMeta repositoriesMeta = new RepositoriesMeta();   
            //                  
            repositoriesMeta.readData();   
            //             
            RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repID);   
            //           
            Repository repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta.getId(), Repository.class);  
            repository.init(repositoryMeta);   
            //         
            repository.connect("admin", "admin");  
            //    ID
            ObjectId id = new StringObjectId(transformationID); 
            //       
            TransMeta transMeta = repository.loadTransformation(id,transformationVersion);
            Trans trans = new Trans(transMeta);
            //       You can pass arguments instead of null.
                    trans.execute(null); 
//                           
                    Log4jBufferAppender appender = CentralLogStore.getAppender();
                    //          
                    trans.waitUntilFinished();
                    //      IU  
                    //InsertUpdate insertUpdate = (InsertUpdate)trans.findBaseSteps("IU").get(0);
                    //String logText = appender.getBuffer(insertUpdate.getLogChannelId(), false).toString();
                    //System.out.println("      
"+logText+"

"); // // CentralLogStore.discardLines(trans.getLogChannelId(),false); /* * */ if ( trans.getErrors() > 0 ) { throw new RuntimeException( " 。" ); } repository.disconnect(); } catch ( KettleException e ) { // TODO Put your exception-handling code here. System.out.println(e); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestKettle testKettle = new TestKettle(); // testKettle.runFileTransformation("C:/111.ktr"); System.out.println(System.getProperty("user.dir")); System.out.println(System.getProperty("KETTLE_HOME")); System.setProperty("user.dir", "E:/myeclipseworkspace/testkettle/kettleconfig"); System.setProperty("KETTLE_HOME", "E:/myeclipseworkspace/testkettle/kettleconfig"); System.out.println(System.getProperty("user.dir")); System.out.println(System.getProperty("KETTLE_HOME")); testKettle.runRepTransformation("testkettle","1","1.0"); testKettle.runRepTransformation("testkettle","2","1.0"); } }

kettle 설정 디 렉 터 리 변경 후 코드 설정 은 다음 과 같 습 니 다.
  • TestKettle.rar (1.6 KB)
  • 다운로드 횟수: 12
  • 좋은 웹페이지 즐겨찾기