Search in sources :

Example 21 with RowProducer

use of org.pentaho.di.trans.RowProducer in project pentaho-kettle by pentaho.

the class JaninoStepIT method testJaninoStep.

/**
 * Test case for janino step.
 */
@Test
public void testJaninoStep() throws Exception {
    KettleEnvironment.init();
    // 
    // Create a new transformation...
    // 
    TransMeta transMeta = new TransMeta();
    transMeta.setName("janino test");
    PluginRegistry registry = PluginRegistry.getInstance();
    // create an injector step...
    String injectorStepName = "injector step";
    InjectorMeta im = new InjectorMeta();
    // Set the information of the injector.
    String injectorPid = registry.getPluginId(StepPluginType.class, im);
    StepMeta injectorStep = new StepMeta(injectorPid, injectorStepName, im);
    transMeta.addStep(injectorStep);
    // 
    // create a janino step...
    // 
    String stepname = "janino";
    JaninoMeta jm = new JaninoMeta();
    // Set the information of the step
    String janinoPid = registry.getPluginId(StepPluginType.class, jm);
    StepMeta janinoStep = new StepMeta(janinoPid, stepname, jm);
    transMeta.addStep(janinoStep);
    jm.setDefault();
    JaninoMetaFunction[] formulas = { new JaninoMetaFunction("string", "(string==null)?null:\"string-value\"", ValueMetaInterface.TYPE_STRING, -1, -1, "string"), new JaninoMetaFunction("integer", "(integer==null)?null:new Long(42L)", ValueMetaInterface.TYPE_INTEGER, -1, -1, "integer"), new JaninoMetaFunction("number", "(number==null)?null:new Double(23.0)", ValueMetaInterface.TYPE_NUMBER, -1, -1, "number"), new JaninoMetaFunction("bigdecimal", "(bigdecimal==null)?null:new java.math.BigDecimal(11.0)", ValueMetaInterface.TYPE_BIGNUMBER, -1, -1, "bigdecimal"), new JaninoMetaFunction("date", "(date==null)?null:new java.util.Date(10000000)", ValueMetaInterface.TYPE_DATE, -1, -1, "date"), new JaninoMetaFunction("binary", "(binary==null)?null:new byte[]{1,2,3,4,5}", ValueMetaInterface.TYPE_BINARY, -1, -1, "binary"), new JaninoMetaFunction("bool", "(bool==null)?null:Boolean.TRUE", ValueMetaInterface.TYPE_BOOLEAN, -1, -1, "bool"), new JaninoMetaFunction("timestamp", "(timestamp==null)?null:new java.sql.Timestamp(0L)", ValueMetaInterface.TYPE_TIMESTAMP, -1, -1, "timestamp"), new JaninoMetaFunction("inetaddress", "(inetaddress==null)?null:java.net.InetAddress.getByAddress( new byte[]{ 127, 0, 0, 1} )", ValueMetaInterface.TYPE_INET, -1, -1, "inetaddress") };
    jm.setFormula(formulas);
    transMeta.addTransHop(new TransHopMeta(injectorStep, janinoStep));
    // 
    // Create a dummy step
    // 
    String dummyStepname = "dummy step";
    DummyTransMeta dm = new DummyTransMeta();
    String dummyPid = registry.getPluginId(StepPluginType.class, dm);
    StepMeta dummyStep = new StepMeta(dummyPid, dummyStepname, dm);
    transMeta.addStep(dummyStep);
    TransHopMeta hi = new TransHopMeta(janinoStep, dummyStep);
    transMeta.addTransHop(hi);
    // Now execute the transformation...
    Trans trans = new Trans(transMeta);
    trans.prepareExecution(null);
    StepInterface si = trans.getStepInterface(dummyStepname, 0);
    RowStepCollector rc = new RowStepCollector();
    si.addRowListener(rc);
    RowProducer rp = trans.addRowProducer(injectorStepName, 0);
    trans.startThreads();
    for (RowMetaAndData rm : createInputList()) {
        rp.putRow(rm.getRowMeta(), rm.getData());
    }
    rp.finished();
    trans.waitUntilFinished();
    List<RowMetaAndData> checkList = createExpectedList();
    List<RowMetaAndData> resultRows = rc.getRowsWritten();
    checkRows(resultRows, checkList);
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) RowStepCollector(org.pentaho.di.trans.RowStepCollector) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) StepInterface(org.pentaho.di.trans.step.StepInterface) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans) Test(org.junit.Test)

Example 22 with RowProducer

use of org.pentaho.di.trans.RowProducer in project pentaho-kettle by pentaho.

the class RestInputIT method createAndTestTrans.

protected Trans createAndTestTrans(PluginRegistry registry, TransMeta transMeta, StepMeta inputStep, RowStepCollector rowStepCollector, String name, int limit) throws KettleException {
    // 
    // Create a dummy step
    // 
    String dummyStepname = "dummy step";
    DummyTransMeta dm1 = new DummyTransMeta();
    String dummyPid1 = registry.getPluginId(StepPluginType.class, dm1);
    StepMeta dummyStep1 = new StepMeta(dummyPid1, dummyStepname, dm1);
    transMeta.addStep(dummyStep1);
    TransHopMeta hi3 = new TransHopMeta(inputStep, dummyStep1);
    transMeta.addTransHop(hi3);
    // Now execute the transformation...
    Trans trans = new Trans(transMeta);
    trans.prepareExecution(null);
    StepInterface si = trans.getStepInterface(dummyStepname, 0);
    si.addRowListener(rowStepCollector);
    RowProducer rp = trans.addRowProducer(inputStep.getName(), 0);
    RowMeta rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaString("pageSize"));
    rowMeta.addValueMeta(new ValueMetaString("name"));
    rp.putRow(rowMeta, new Object[] { Integer.valueOf(limit), name });
    rp.finished();
    return trans;
}
Also used : StepInterface(org.pentaho.di.trans.step.StepInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowProducer(org.pentaho.di.trans.RowProducer) RowMeta(org.pentaho.di.core.row.RowMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) TransHopMeta(org.pentaho.di.trans.TransHopMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) Trans(org.pentaho.di.trans.Trans) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta)

Example 23 with RowProducer

use of org.pentaho.di.trans.RowProducer in project pentaho-kettle by pentaho.

the class JavaScriptSpecialIT method testLuhnCheck.

/**
 * Test case for javascript functionality: ltrim(), rtrim(), trim().
 */
public void testLuhnCheck() throws Exception {
    KettleEnvironment.init();
    // 
    // Create a new transformation...
    // 
    TransMeta transMeta = new TransMeta();
    transMeta.setName("test javascript LuhnCheck");
    PluginRegistry registry = PluginRegistry.getInstance();
    // 
    // create an injector step...
    // 
    String injectorStepname = "injector step";
    InjectorMeta im = new InjectorMeta();
    // Set the information of the injector.
    String injectorPid = registry.getPluginId(StepPluginType.class, im);
    StepMeta injectorStep = new StepMeta(injectorPid, injectorStepname, im);
    transMeta.addStep(injectorStep);
    // 
    // Create a javascript step
    // 
    String javaScriptStepname = "javascript step";
    ScriptValuesMetaMod svm = new ScriptValuesMetaMod();
    ScriptValuesScript[] js = new ScriptValuesScript[] { new ScriptValuesScript(ScriptValuesScript.TRANSFORM_SCRIPT, "script", "var str = string;\n" + "var bool = LuhnCheck(str);") };
    svm.setJSScripts(js);
    svm.setFieldname(new String[] { "bool" });
    svm.setRename(new String[] { "" });
    svm.setType(new int[] { ValueMetaInterface.TYPE_BOOLEAN });
    svm.setLength(new int[] { -1 });
    svm.setPrecision(new int[] { -1 });
    svm.setReplace(new boolean[] { false });
    svm.setCompatible(false);
    String javaScriptStepPid = registry.getPluginId(StepPluginType.class, svm);
    StepMeta javaScriptStep = new StepMeta(javaScriptStepPid, javaScriptStepname, svm);
    transMeta.addStep(javaScriptStep);
    TransHopMeta hi1 = new TransHopMeta(injectorStep, javaScriptStep);
    transMeta.addTransHop(hi1);
    // 
    // Create a dummy step
    // 
    String dummyStepname = "dummy step";
    DummyTransMeta dm = new DummyTransMeta();
    String dummyPid = registry.getPluginId(StepPluginType.class, dm);
    StepMeta dummyStep = new StepMeta(dummyPid, dummyStepname, dm);
    transMeta.addStep(dummyStep);
    TransHopMeta hi2 = new TransHopMeta(javaScriptStep, dummyStep);
    transMeta.addTransHop(hi2);
    // Now execute the transformation...
    Trans trans = new Trans(transMeta);
    trans.prepareExecution(null);
    StepInterface si;
    si = trans.getStepInterface(javaScriptStepname, 0);
    RowStepCollector javaScriptRc = new RowStepCollector();
    si.addRowListener(javaScriptRc);
    si = trans.getStepInterface(dummyStepname, 0);
    RowStepCollector dummyRc = new RowStepCollector();
    si.addRowListener(dummyRc);
    RowProducer rp = trans.addRowProducer(injectorStepname, 0);
    trans.startThreads();
    // add rows
    List<RowMetaAndData> inputList = createData1();
    Iterator<RowMetaAndData> it = inputList.iterator();
    while (it.hasNext()) {
        RowMetaAndData rm = it.next();
        rp.putRow(rm.getRowMeta(), rm.getData());
    }
    rp.finished();
    trans.waitUntilFinished();
    List<RowMetaAndData> goldenImageRows = createResultData1();
    List<RowMetaAndData> resultRows1 = javaScriptRc.getRowsWritten();
    checkRows(resultRows1, goldenImageRows);
    List<RowMetaAndData> resultRows2 = dummyRc.getRowsRead();
    checkRows(resultRows2, goldenImageRows);
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) RowStepCollector(org.pentaho.di.trans.RowStepCollector) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) StepInterface(org.pentaho.di.trans.step.StepInterface) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans)

Example 24 with RowProducer

use of org.pentaho.di.trans.RowProducer in project pentaho-kettle by pentaho.

the class JavaScriptStringIT method testStringsPadCase.

/**
 * Test case for javascript functionality: lpad(), rpad(), upper(), lower().
 */
public void testStringsPadCase() throws Exception {
    KettleEnvironment.init();
    // 
    // Create a new transformation...
    // 
    TransMeta transMeta = new TransMeta();
    transMeta.setName("test javascript pad casing");
    PluginRegistry registry = PluginRegistry.getInstance();
    // 
    // create an injector step...
    // 
    String injectorStepname = "injector step";
    InjectorMeta im = new InjectorMeta();
    // Set the information of the injector.
    String injectorPid = registry.getPluginId(StepPluginType.class, im);
    StepMeta injectorStep = new StepMeta(injectorPid, injectorStepname, im);
    transMeta.addStep(injectorStep);
    // 
    // Create a javascript step
    // 
    String javaScriptStepname = "javascript step";
    ScriptValuesMetaMod svm = new ScriptValuesMetaMod();
    ScriptValuesScript[] js = new ScriptValuesScript[] { new ScriptValuesScript(ScriptValuesScript.TRANSFORM_SCRIPT, "script", "var lpadded1 = lpad(string, \"x\", 10);\n" + "var lpadded2 = lpad(string, \" \", 9);\n" + "var rpadded1 = rpad(string, \"x\", 10);\n" + "var rpadded2 = rpad(string, \" \", 9);\n" + "var upperStr = upper(string);\n" + "var lowerStr = lower(string);\n") };
    svm.setJSScripts(js);
    svm.setFieldname(new String[] { "lpadded1", "lpadded2", "rpadded1", "rpadded2", "upperStr", "lowerStr" });
    svm.setRename(new String[] { "", "", "", "", "", "", "" });
    svm.setType(new int[] { ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING, ValueMetaInterface.TYPE_STRING });
    svm.setLength(new int[] { -1, -1, -1, -1, -1, -1, -1 });
    svm.setPrecision(new int[] { -1, -1, -1, -1, -1, -1, -1 });
    svm.setReplace(new boolean[] { false, false, false, false, false, false, false });
    svm.setCompatible(true);
    String javaScriptStepPid = registry.getPluginId(StepPluginType.class, svm);
    StepMeta javaScriptStep = new StepMeta(javaScriptStepPid, javaScriptStepname, svm);
    transMeta.addStep(javaScriptStep);
    TransHopMeta hi1 = new TransHopMeta(injectorStep, javaScriptStep);
    transMeta.addTransHop(hi1);
    // 
    // Create a dummy step
    // 
    String dummyStepname = "dummy step";
    DummyTransMeta dm = new DummyTransMeta();
    String dummyPid = registry.getPluginId(StepPluginType.class, dm);
    StepMeta dummyStep = new StepMeta(dummyPid, dummyStepname, dm);
    transMeta.addStep(dummyStep);
    TransHopMeta hi2 = new TransHopMeta(javaScriptStep, dummyStep);
    transMeta.addTransHop(hi2);
    // Now execute the transformation...
    Trans trans = new Trans(transMeta);
    trans.prepareExecution(null);
    StepInterface si;
    si = trans.getStepInterface(javaScriptStepname, 0);
    RowStepCollector javaScriptRc = new RowStepCollector();
    si.addRowListener(javaScriptRc);
    si = trans.getStepInterface(dummyStepname, 0);
    RowStepCollector dummyRc = new RowStepCollector();
    si.addRowListener(dummyRc);
    RowProducer rp = trans.addRowProducer(injectorStepname, 0);
    trans.startThreads();
    // add rows
    List<RowMetaAndData> inputList = createData2();
    Iterator<RowMetaAndData> it = inputList.iterator();
    while (it.hasNext()) {
        RowMetaAndData rm = it.next();
        rp.putRow(rm.getRowMeta(), rm.getData());
    }
    rp.finished();
    trans.waitUntilFinished();
    List<RowMetaAndData> goldenImageRows = createResultData2();
    List<RowMetaAndData> resultRows1 = javaScriptRc.getRowsWritten();
    checkRows(resultRows1, goldenImageRows);
    List<RowMetaAndData> resultRows2 = dummyRc.getRowsRead();
    checkRows(resultRows2, goldenImageRows);
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) RowStepCollector(org.pentaho.di.trans.RowStepCollector) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) StepInterface(org.pentaho.di.trans.step.StepInterface) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans)

Example 25 with RowProducer

use of org.pentaho.di.trans.RowProducer in project pentaho-kettle by pentaho.

the class SplitFieldToRowsIT method splitFieldToRows.

/**
 * Splits the "stringToSplit" with the passed "delimiter". The "delimiter" is assumed by this method to be a Kettle
 * variable. The parameter "delimiterVariableValue" should contain the variables value.
 *
 * The "isDelimiterRegex" parameter will process the use regex for pattern matching if true.
 *
 * @param testName
 * @param stringToSplit
 * @param isDelimiterRegex
 * @param delimiter
 * @param delimiterVariableValue
 * @return
 * @throws Exception
 */
private List<RowMetaAndData> splitFieldToRows(String testName, String stringToSplit, boolean isDelimiterRegex, String delimiter, String delimiterVariableValue) {
    RowStepCollector rc = new RowStepCollector();
    try {
        KettleEnvironment.init();
        // Create a new transformation...
        TransMeta transMeta = new TransMeta();
        transMeta.setName("Split field to rows test");
        PluginRegistry registry = PluginRegistry.getInstance();
        // create an injector step...
        String injectorStepname = "injector step";
        InjectorMeta im = new InjectorMeta();
        // Set the information of the injector.
        String injectorPid = registry.getPluginId(StepPluginType.class, im);
        StepMeta injectorStep = new StepMeta(injectorPid, injectorStepname, im);
        transMeta.addStep(injectorStep);
        // Create a Split Field to Rows step
        String splitfieldToRowsName = "Split field to rows";
        SplitFieldToRowsMeta splitFieldtoRowsMeta = new SplitFieldToRowsMeta();
        splitFieldtoRowsMeta.setDelimiter(delimiter);
        splitFieldtoRowsMeta.setDelimiterRegex(isDelimiterRegex);
        splitFieldtoRowsMeta.setSplitField(FIELD_TO_SPLIT_NAME);
        splitFieldtoRowsMeta.setNewFieldname(NEW_FIELD_NAME);
        String splitFieldTotRowsPid = registry.getPluginId(StepPluginType.class, splitFieldtoRowsMeta);
        StepMeta splitFieldToRows = new StepMeta(splitFieldTotRowsPid, splitfieldToRowsName, splitFieldtoRowsMeta);
        transMeta.addStep(splitFieldToRows);
        // hop the injector to the split field to rows step
        TransHopMeta hop_injector_splitfieldToRows = new TransHopMeta(injectorStep, splitFieldToRows);
        transMeta.addTransHop(hop_injector_splitfieldToRows);
        // Create a dummy step
        String dummyStepname = "dummy step";
        DummyTransMeta dm = new DummyTransMeta();
        String dummyPid = registry.getPluginId(StepPluginType.class, dm);
        StepMeta dummyStep = new StepMeta(dummyPid, dummyStepname, dm);
        transMeta.addStep(dummyStep);
        TransHopMeta hop_SplitFieldToRows_Dummy = new TransHopMeta(splitFieldToRows, dummyStep);
        transMeta.addTransHop(hop_SplitFieldToRows_Dummy);
        if (!Utils.isEmpty(delimiterVariableValue)) {
            String delimiterVariableName = delimiter.replace("${", "");
            delimiterVariableName = delimiterVariableName.replace("}", "");
            transMeta.setVariable(delimiterVariableName, delimiterVariableValue);
        }
        // Now execute the transformation...
        Trans trans = new Trans(transMeta);
        trans.prepareExecution(null);
        StepInterface si = trans.getStepInterface(dummyStepname, 0);
        si.addRowListener(rc);
        RowProducer rowProducer = trans.addRowProducer(injectorStepname, 0);
        trans.startThreads();
        // add rows
        List<RowMetaAndData> inputList = createData(stringToSplit);
        for (RowMetaAndData rm : inputList) {
            rowProducer.putRow(rm.getRowMeta(), rm.getData());
        }
        rowProducer.finished();
        trans.waitUntilFinished();
    } catch (KettleException e) {
        fail("KettleEnvironment exception" + e.getMessage());
    }
    List<RowMetaAndData> resultRows = rc.getRowsWritten();
    return resultRows;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowProducer(org.pentaho.di.trans.RowProducer) RowStepCollector(org.pentaho.di.trans.RowStepCollector) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) StepInterface(org.pentaho.di.trans.step.StepInterface) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans)

Aggregations

RowProducer (org.pentaho.di.trans.RowProducer)66 Trans (org.pentaho.di.trans.Trans)58 StepInterface (org.pentaho.di.trans.step.StepInterface)57 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)56 StepMeta (org.pentaho.di.trans.step.StepMeta)51 RowStepCollector (org.pentaho.di.trans.RowStepCollector)50 TransHopMeta (org.pentaho.di.trans.TransHopMeta)49 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)48 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)47 TransMeta (org.pentaho.di.trans.TransMeta)47 InjectorMeta (org.pentaho.di.trans.steps.injector.InjectorMeta)45 DummyTransMeta (org.pentaho.di.trans.steps.dummytrans.DummyTransMeta)35 Test (org.junit.Test)22 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)12 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)9 KettleException (org.pentaho.di.core.exception.KettleException)7 UniqueRowsMeta (org.pentaho.di.trans.steps.uniquerows.UniqueRowsMeta)7 ArrayList (java.util.ArrayList)5 Database (org.pentaho.di.core.database.Database)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3