水壶(Pentaho PDI):如何访问工作的所有步骤

时间:2016-06-30 12:16:08

标签: pentaho kettle pdi

我需要使用Java访问Kettle的.ktr文件中包含的所有步骤。

我正在使用

KettleEnvironment.init();
JobMeta jobMeta = new JobMeta("file.kjb", null);
Job job = new Job(null, jobMeta); 

但不是Job也不是JobMeta似乎提供了访问基本步骤的任何方法。

2 个答案:

答案 0 :(得分:0)

根据this answer,显然这是不可能的。

答案 1 :(得分:0)

因此,我设法使用DelegationListenerTransListener的组合来完成此操作,但是,我不确定此处是否需要DelegationListener。也许还有另一种方法可以将TransListeners添加到转换作业条目:

package testTransformationsInJob;

import java.util.List;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.DelegationListener;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobExecutionConfiguration;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransExecutionConfiguration;
import org.pentaho.di.trans.TransListener;
import org.pentaho.di.trans.step.StepMetaDataCombi;
import org.pentaho.di.trans.step.StepInterface;

public class MainClass {

    public MainClass() {
        // TODO Auto-generated constructor stub
    }

    private static class MyTransListener implements TransListener {

        @Override
        public void transActive(Trans arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void transFinished(Trans arg0) throws KettleException {
            // TODO Auto-generated method stub

        }

        @Override
        public void transStarted(Trans delegatedTrans) throws KettleException {
            List<StepMetaDataCombi> stepCombis = delegatedTrans.getSteps();
            if(stepCombis == null) {
                return;
            }
            for(StepMetaDataCombi stepCombi: stepCombis) {
                StepInterface step = stepCombi.step;
                //
                // Do some useful work here.
                //
                System.out.println("\t" + step.getStepname());
            }
        }

    }

    private static class MyDelegationListener implements DelegationListener {

        private TransListener transListener;

        MyDelegationListener(TransListener transListener) {
            this.transListener = transListener;
        }

        @Override
        public void jobDelegationStarted(Job delegatedJob,
                JobExecutionConfiguration jobExecutionConfiguration) {
            // TODO Auto-generated method stub

        }

        @Override
        public void transformationDelegationStarted(Trans delegatedTrans,
                TransExecutionConfiguration transExecutionConfiguratioStep) {
            System.out.println("transformationDelegationStarted");
            System.out.println(delegatedTrans.getName());
            // transformationDelegationStarted() is called after Trans object is constructed
            // but before it is executed.
            // However, we can't access steps at this point using delegatedTrans.getSteps()
            // since steps are constructed somewhere in execute method.
            // However, we can add TransListener here, which will be able to iterate steps.
            delegatedTrans.addTransListener(this.transListener);
        }

    }

    public static void main(String[] args) throws KettleException {
        KettleEnvironment.init();
        JobMeta jobMeta = new JobMeta("d:\\test_job.kjb", null);
        Job job = new Job(null, jobMeta); 
        // Here I add a DelegationListener, which will add a TransListener to every Trans in job.
        // Not sure though if using DelegationListener is a right way to access Transformation Job Entries
        // Maybe there is a more elegant way to do it.
        MyTransListener myTransListener = new MyTransListener();
        DelegationListener delegationListener = new MyDelegationListener(myTransListener);
        job.addDelegationListener(delegationListener);
        job.start();
        job.waitUntilFinished();
    }

}

这是我得到的输出:

2016/07/07 09:47:37 - test_job - Start of job execution
2016/07/07 09:47:37 - test_job - Starting entry [Transformation]
2016/07/07 09:47:37 - Transformation - Loading transformation from XML file [file:///d://test.ktr]
transformationDelegationStarted
test
2016/07/07 09:47:37 - test - Dispatching started for transformation [test]
    Detect empty stream
    User Defined Java Class
2016/07/07 09:47:37 - Detect empty stream.0 - Finished processing (I=0, O=0, R=0, W=1, U=0, E=0)
2016/07/07 09:47:38 - User Defined Java Class.0 - Finished processing (I=0, O=0, R=1, W=1, U=0, E=0)
2016/07/07 09:47:38 - test_job - Finished job entry [Transformation] (result=[true])
2016/07/07 09:47:38 - test_job - Job execution finished