Kettle java調用

來源:互聯網
上載者:User

package kettle;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
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.LongObjectId;
import org.pentaho.di.repository.ObjectId;
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;

public class KettleExecutor {

    Logger log = Logger.getLogger(getClass());
    KettleDatabaseRepository rep;
    RepositoryDirectoryInterface dir;
    
    
    public String getDatabaseRepositoryXMl(){
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<connection>" +
                      "<name>Kettle</name>" +
                      "<type>MSSQL</type>" +
                      "<server>127.0.0.1</server>" +
                    "<access>Native</access>" +
                    "<database>Kettle</database>" +
                    "<port>1433</port>" +
                    "<username>SA</username>" +
                    "<password>password</password>" +
                    "<servername/>" +
                    "<data_tablespace/>" +
                    "<index_tablespace/>" +
                "</connection>" ;
        return xml;
    }
    
    public void connectRepository(String username,String password){
        try {
            EnvUtil.environmentInit();
            KettleEnvironment.init();
            DatabaseMeta dataMeta = new DatabaseMeta(getDatabaseRepositoryXMl());
            KettleDatabaseRepositoryMeta repInfo = new KettleDatabaseRepositoryMeta();
            repInfo.setConnection(dataMeta);
            
            rep = new KettleDatabaseRepository();
            rep.init(repInfo);
            rep.connect(username, password);
            
            ObjectId rootId = Long.parseLong(rep.getRootDirectoryID().getId()) > 0 ? rep.getRootDirectoryID() : new LongObjectId(0);
            dir = new RepositoryDirectory();
            dir.setObjectId(rootId);
        } catch (KettleException e) {
            e.printStackTrace();
        }
    }
    
    public List<ObjectId> getObjectIds(String sql) throws KettleDatabaseException{
        List<ObjectId> list = new ArrayList<ObjectId>();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = rep.getDatabase().getConnection().createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next())
                list.add(new LongObjectId(rs.getLong(1)));        
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{            
            try {
                if(rs != null)
                    rs.close();
                if(stmt != null)
                    stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }            
        }
        return list;
    }
    
    public void runAllJobs(){
        try {
            List<ObjectId> jobIds = getObjectIds("select id_job from r_job where job_status >= 0 order by id_job");
            for (final ObjectId oid : jobIds) {
//                new JobsExecutor(rep, oid).run();
                new Runnable() {
                    @Override
                    public void run(){
                        try {
                            JobMeta jobMeta = rep.loadJob(oid, null);
                            Job job = new Job(rep,jobMeta);
                            log.info("***********************************開始執行job:"+job.getJobname());
                            job.start();
                            job.waitUntilFinished();
                            if (job.getErrors() > 0) {
                                log.info("***********************************執行job錯誤:"+job.getJobname());
                            }
                        } catch (KettleException e) {
                            e.printStackTrace();
                        }
                    }
                }.run();                
            }
        } catch (KettleException e) {
            e.printStackTrace();
        }
    }
    
    public void runJob(ObjectId jobId,String versionLabel){
        try {
            JobMeta jobMeta = rep.loadJob(jobId, versionLabel);
            Job job = new Job(rep,jobMeta);
            job.start();
            job.waitUntilFinished();
            if (job.getErrors() > 0) {
                System.out.println("decompress fail!");
            }
        } catch (KettleException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 檔案方式調用Job
     * @param fileName Job指令碼的路徑及名稱
     */
    public void runJob(String fileName) {
        try {
            KettleEnvironment.init();
            JobMeta jobMeta = new JobMeta(fileName, null);
            Job job = new Job(null, jobMeta);
            // 向Job 指令碼傳遞參數,指令碼中擷取參數值:${參數名}
            // job.setVariable(paraname, paravalue);
            job.start();
            job.waitUntilFinished();
            if (job.getErrors() > 0) {
                System.out.println("decompress fail!");
            }
        } catch (KettleException e) {
            System.out.println(e);
        }
    }
    
    /**
     * 檔案方式調用Transformation
     * @param filename  Transformation指令碼的路徑及名稱
     */
    public void runTran(String filename) {
        try {
            KettleEnvironment.init();
            TransMeta transMeta = new TransMeta(filename);
            Trans trans = new Trans(transMeta);
            trans.prepareExecution(null);
            trans.startThreads();
            trans.waitUntilFinished();

            if (trans.getErrors() != 0) {
                System.out.println("Error");
            }
        } 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.