Search in sources :

Example 1 with SelectValuesMeta

use of org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta in project pentaho-kettle by pentaho.

the class SpoonDBDelegate method copyTable.

public boolean copyTable(DatabaseMeta sourceDBInfo, DatabaseMeta targetDBInfo, String tablename) {
    try {
        // 
        // Create a new transformation...
        // 
        TransMeta meta = new TransMeta();
        meta.addDatabase(sourceDBInfo);
        meta.addDatabase(targetDBInfo);
        // 
        // Add a note
        // 
        String note = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()) + // "Reads information from table ["+tablename+"]
        Const.CR;
        // on database ["+sourceDBInfo+"]"
        note += BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB", tablename, targetDBInfo.getDatabaseName());
        // the information to table
        // ["+tablename+"] on
        // database
        // ["+targetDBInfo+"]"
        NotePadMeta ni = new NotePadMeta(note, 150, 10, -1, -1);
        meta.addNote(ni);
        // 
        // create the source step...
        // 
        // "read
        String fromstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadFromTable", tablename);
        // from
        // ["+tablename+"]";
        TableInputMeta tii = new TableInputMeta();
        tii.setDatabaseMeta(sourceDBInfo);
        tii.setSQL("SELECT * FROM " + tablename);
        PluginRegistry registry = PluginRegistry.getInstance();
        String fromstepid = registry.getPluginId(StepPluginType.class, tii);
        StepMeta fromstep = new StepMeta(fromstepid, fromstepname, tii);
        fromstep.setLocation(150, 100);
        fromstep.setDraw(true);
        fromstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()));
        meta.addStep(fromstep);
        // 
        // add logic to rename fields in case any of the field names contain
        // reserved words...
        // Use metadata logic in SelectValues, use SelectValueInfo...
        // 
        Database sourceDB = new Database(loggingObject, sourceDBInfo);
        sourceDB.shareVariablesWith(meta);
        sourceDB.connect();
        try {
            // Get the fields for the input table...
            RowMetaInterface fields = sourceDB.getTableFields(tablename);
            // See if we need to deal with reserved words...
            int nrReserved = targetDBInfo.getNrReservedWords(fields);
            if (nrReserved > 0) {
                SelectValuesMeta svi = new SelectValuesMeta();
                svi.allocate(0, 0, nrReserved);
                int nr = 0;
                // CHECKSTYLE:Indentation:OFF
                for (int i = 0; i < fields.size(); i++) {
                    ValueMetaInterface v = fields.getValueMeta(i);
                    if (targetDBInfo.isReservedWord(v.getName())) {
                        if (svi.getMeta()[nr] == null) {
                            svi.getMeta()[nr] = new SelectMetadataChange(svi);
                        }
                        svi.getMeta()[nr].setName(v.getName());
                        svi.getMeta()[nr].setRename(targetDBInfo.quoteField(v.getName()));
                        nr++;
                    }
                }
                String selstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.HandleReservedWords");
                String selstepid = registry.getPluginId(StepPluginType.class, svi);
                StepMeta selstep = new StepMeta(selstepid, selstepname, svi);
                selstep.setLocation(350, 100);
                selstep.setDraw(true);
                selstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.RenamesReservedWords", // 
                targetDBInfo.getPluginId()));
                meta.addStep(selstep);
                TransHopMeta shi = new TransHopMeta(fromstep, selstep);
                meta.addTransHop(shi);
                fromstep = selstep;
            }
            // 
            // Create the target step...
            // 
            // 
            // Add the TableOutputMeta step...
            // 
            String tostepname = BaseMessages.getString(PKG, "Spoon.Message.Note.WriteToTable", tablename);
            TableOutputMeta toi = new TableOutputMeta();
            toi.setDatabaseMeta(targetDBInfo);
            toi.setTableName(tablename);
            toi.setCommitSize(200);
            toi.setTruncateTable(true);
            String tostepid = registry.getPluginId(StepPluginType.class, toi);
            StepMeta tostep = new StepMeta(tostepid, tostepname, toi);
            tostep.setLocation(550, 100);
            tostep.setDraw(true);
            tostep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB2", tablename, targetDBInfo.getDatabaseName()));
            meta.addStep(tostep);
            // 
            // Add a hop between the two steps...
            // 
            TransHopMeta hi = new TransHopMeta(fromstep, tostep);
            meta.addTransHop(hi);
            // OK, if we're still here: overwrite the current transformation...
            // Set a name on this generated transformation
            // 
            String name = "Copy table from [" + sourceDBInfo.getName() + "] to [" + targetDBInfo.getName() + "]";
            String transName = name;
            int nr = 1;
            if (spoon.delegates.trans.getTransformation(transName) != null) {
                nr++;
                transName = name + " " + nr;
            }
            meta.setName(transName);
            spoon.delegates.trans.addTransGraph(meta);
            spoon.refreshGraph();
            spoon.refreshTree();
        } finally {
            sourceDB.disconnect();
        }
    } catch (Exception e) {
        new ErrorDialog(spoon.getShell(), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Title"), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Message"), new KettleException(e.getMessage(), e));
        return false;
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TransMeta(org.pentaho.di.trans.TransMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) TableOutputMeta(org.pentaho.di.trans.steps.tableoutput.TableOutputMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) TableInputMeta(org.pentaho.di.trans.steps.tableinput.TableInputMeta) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) Database(org.pentaho.di.core.database.Database) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) NotePadMeta(org.pentaho.di.core.NotePadMeta) TransHopMeta(org.pentaho.di.trans.TransHopMeta)

Example 2 with SelectValuesMeta

use of org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta in project pentaho-kettle by pentaho.

the class Spoon method generateFieldMapping.

/**
 * Create a new SelectValues step in between this step and the previous. If the previous fields are not there, no
 * mapping can be made, same with the required fields.
 *
 * @param stepMeta
 *          The target step to map against.
 */
// retry of required fields acquisition
public void generateFieldMapping(TransMeta transMeta, StepMeta stepMeta) {
    try {
        if (stepMeta != null) {
            StepMetaInterface smi = stepMeta.getStepMetaInterface();
            RowMetaInterface targetFields = smi.getRequiredFields(transMeta);
            RowMetaInterface sourceFields = transMeta.getPrevStepFields(stepMeta);
            // Build the mapping: let the user decide!!
            String[] source = sourceFields.getFieldNames();
            for (int i = 0; i < source.length; i++) {
                ValueMetaInterface v = sourceFields.getValueMeta(i);
                source[i] += EnterMappingDialog.STRING_ORIGIN_SEPARATOR + v.getOrigin() + ")";
            }
            String[] target = targetFields.getFieldNames();
            EnterMappingDialog dialog = new EnterMappingDialog(shell, source, target);
            List<SourceToTargetMapping> mappings = dialog.open();
            if (mappings != null) {
                // OK, so we now know which field maps where.
                // This allows us to generate the mapping using a
                // SelectValues Step...
                SelectValuesMeta svm = new SelectValuesMeta();
                svm.allocate(mappings.size(), 0, 0);
                // CHECKSTYLE:Indentation:OFF
                for (int i = 0; i < mappings.size(); i++) {
                    SourceToTargetMapping mapping = mappings.get(i);
                    svm.getSelectFields()[i].setName(sourceFields.getValueMeta(mapping.getSourcePosition()).getName());
                    svm.getSelectFields()[i].setRename(target[mapping.getTargetPosition()]);
                    svm.getSelectFields()[i].setLength(-1);
                    svm.getSelectFields()[i].setPrecision(-1);
                }
                // a new comment. Sincerely yours CO ;)
                // Now that we have the meta-data, create a new step info object
                String stepName = stepMeta.getName() + " Mapping";
                // if
                stepName = transMeta.getAlternativeStepname(stepName);
                // it's already there, rename it.
                StepMeta newStep = new StepMeta("SelectValues", stepName, svm);
                newStep.setLocation(stepMeta.getLocation().x + 20, stepMeta.getLocation().y + 20);
                newStep.setDraw(true);
                transMeta.addStep(newStep);
                addUndoNew(transMeta, new StepMeta[] { newStep }, new int[] { transMeta.indexOfStep(newStep) });
                // Redraw stuff...
                refreshTree();
                refreshGraph();
            }
        } else {
            throw new KettleException("There is no target to do a field mapping against!");
        }
    } catch (KettleException e) {
        new ErrorDialog(shell, "Error creating mapping", "There was an error when Kettle tried to generate a field mapping against the target step", e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) Point(org.pentaho.di.core.gui.Point) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping)

Example 3 with SelectValuesMeta

use of org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta in project pentaho-metaverse by pentaho.

the class MetaverseValidationIT method testSelectValuesStep.

@Test
public void testSelectValuesStep() throws Exception {
    // this tests a specific select values step. the one in trans "Populate Table From File"
    SelectValuesTransStepNode selectValues = root.getSelectValuesStepNode();
    assertNotNull(selectValues);
    int countUses = getIterableSize(selectValues.getStreamFieldNodesUses());
    int countOutputs = getIterableSize(selectValues.getOutputStreamFields());
    int countInputs = getIterableSize(selectValues.getInputStreamFields());
    assertEquals(9, countUses);
    SelectValuesMeta meta = (SelectValuesMeta) getStepMeta(selectValues);
    assertEquals(getExpectedOutputFieldCount(meta), countOutputs);
    assertEquals(9, countInputs);
    assertEquals("Select values", selectValues.getStepType());
    for (StreamFieldNode node : selectValues.getOutputStreamFields()) {
        // check for operations
        if (node.getOperations() != null) {
            Operations ops = MetaverseUtil.convertOperationsStringToMap(node.getOperations());
            assertNotNull(ops);
            List<IOperation> metadataOps = ops.get(ChangeType.METADATA);
            assertNotNull(metadataOps);
            assertTrue(metadataOps.size() > 0);
            // there should not be any data operations on nodes touched by this step
            assertNull(ops.get(ChangeType.DATA));
        }
        // check the created node is derived from something
        Iterable<StreamFieldNode> deriveNodes = node.getFieldNodesThatDeriveMe();
        for (StreamFieldNode deriveNode : deriveNodes) {
            assertNotNull(deriveNode);
        }
    }
}
Also used : IOperation(org.pentaho.metaverse.api.model.IOperation) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) StreamFieldNode(org.pentaho.metaverse.frames.StreamFieldNode) Operations(org.pentaho.metaverse.api.model.Operations) SelectValuesTransStepNode(org.pentaho.metaverse.frames.SelectValuesTransStepNode) Test(org.junit.Test)

Example 4 with SelectValuesMeta

use of org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGenerator method generateSelectStep.

private StepMeta generateSelectStep() {
    List<SQLField> fields = sql.getSelectFields().getFields();
    SelectValuesMeta meta = new SelectValuesMeta();
    meta.allocate(fields.size(), 0, 0);
    String[] selectNames = new String[fields.size()];
    String[] selectRename = new String[fields.size()];
    for (int i = 0; i < fields.size(); i++) {
        SQLField sqlField = fields.get(i);
        if (sqlField.getAggregation() == null) {
            selectNames[i] = sqlField.getField();
            selectRename[i] = sqlField.getAlias();
        } else {
            // agg field names are assigned in the group by
            selectNames[i] = Const.NVL(sqlField.getAlias(), sqlField.getField());
        }
    }
    meta.setSelectName(selectNames);
    meta.setSelectRename(selectRename);
    StepMeta stepMeta = new StepMeta("Select values", meta);
    stepMeta.setLocation(xLocation, 50);
    xLocation += 100;
    stepMeta.setDraw(true);
    return stepMeta;
}
Also used : SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SQLField(org.pentaho.di.core.sql.SQLField) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 5 with SelectValuesMeta

use of org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGenerator method generateConversionStep.

private StepMeta generateConversionStep() {
    // Set conversion masks for each column, modify type where required.
    SelectValuesMeta meta = new SelectValuesMeta();
    meta.allocate(0, 0, serviceFields.size());
    meta.setMeta(serviceFields.getValueMetaList().stream().map(valueMeta -> getSelectMetadataChange(meta, valueMeta)).collect(Collectors.toList()).toArray(new SelectMetadataChange[serviceFields.size()]));
    StepMeta stepMeta = new StepMeta("Set Conversion", meta);
    stepMeta.setLocation(xLocation, 50);
    xLocation += 100;
    stepMeta.setDraw(true);
    return stepMeta;
}
Also used : SQL(org.pentaho.di.core.sql.SQL) CalculatorMetaFunction(org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction) KettleException(org.pentaho.di.core.exception.KettleException) CalculatorMeta(org.pentaho.di.trans.steps.calculator.CalculatorMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) SampleRowsMeta(org.pentaho.di.trans.steps.samplerows.SampleRowsMeta) TYPE_NONE(org.pentaho.di.core.row.ValueMetaInterface.TYPE_NONE) TransMeta(org.pentaho.di.trans.TransMeta) Const(org.pentaho.di.core.Const) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) SQLAggregation(org.pentaho.di.core.sql.SQLAggregation) Condition(org.pentaho.di.core.Condition) StepMeta(org.pentaho.di.trans.step.StepMeta) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) MemoryGroupByMeta(org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) ConstantMeta(org.pentaho.di.trans.steps.constant.ConstantMeta) TYPE_NUMBER(org.pentaho.di.core.row.ValueMetaInterface.TYPE_NUMBER) TYPE_INTEGER(org.pentaho.di.core.row.ValueMetaInterface.TYPE_INTEGER) SQLField(org.pentaho.di.core.sql.SQLField) Utils(org.pentaho.di.core.util.Utils) IifFunction(org.pentaho.di.core.sql.IifFunction) Collectors(java.util.stream.Collectors) List(java.util.List) StreamInterface(org.pentaho.di.trans.step.errorhandling.StreamInterface) TransHopMeta(org.pentaho.di.trans.TransHopMeta) FilterRowsMeta(org.pentaho.di.trans.steps.filterrows.FilterRowsMeta) SQLFields(org.pentaho.di.core.sql.SQLFields) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SortRowsMeta(org.pentaho.di.trans.steps.sort.SortRowsMeta) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) StepMeta(org.pentaho.di.trans.step.StepMeta)

Aggregations

SelectValuesMeta (org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta)18 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)15 Test (org.junit.Test)13 SQL (org.pentaho.di.core.sql.SQL)13 RowMeta (org.pentaho.di.core.row.RowMeta)12 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)12 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)9 StepMeta (org.pentaho.di.trans.step.StepMeta)5 KettleException (org.pentaho.di.core.exception.KettleException)3 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)3 SQLField (org.pentaho.di.core.sql.SQLField)2 TransHopMeta (org.pentaho.di.trans.TransHopMeta)2 TransMeta (org.pentaho.di.trans.TransMeta)2 StepMetaInterface (org.pentaho.di.trans.step.StepMetaInterface)2 SelectMetadataChange (org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange)2 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Condition (org.pentaho.di.core.Condition)1