Search in sources :

Example 1 with MappingOutputMeta

use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta in project pentaho-kettle by pentaho.

the class MappingOutputMetaIT method testGetFields_OutputValueRenames_WillRenameOutputIfValueMetaExist.

@Test
public void testGetFields_OutputValueRenames_WillRenameOutputIfValueMetaExist() throws KettleStepException {
    ValueMetaInterface valueMeta1 = new ValueMetaBase("valueMeta1");
    ValueMetaInterface valueMeta2 = new ValueMetaBase("valueMeta2");
    RowMetaInterface rowMeta = new RowMeta();
    rowMeta.addValueMeta(valueMeta1);
    rowMeta.addValueMeta(valueMeta2);
    List<MappingValueRename> outputValueRenames = new ArrayList<MappingValueRename>();
    outputValueRenames.add(new MappingValueRename("valueMeta2", "valueMeta1"));
    MappingOutputMeta meta = new MappingOutputMeta();
    meta.setOutputValueRenames(outputValueRenames);
    meta.getFields(rowMeta, null, info, nextStep, space, repository, metaStore);
    // we must not add additional field
    assertEquals(2, rowMeta.getValueMetaList().size());
    // we must not keep the first value meta since we want to rename second
    assertEquals(valueMeta1, rowMeta.getValueMeta(0));
    // the second value meta must be other than we want to rename since we already have value meta with such name
    assertFalse("valueMeta1".equals(rowMeta.getValueMeta(1).getName()));
    // the second value meta must be other than we want to rename since we already have value meta with such name.
    // It must be renamed according the rules from the #RowMeta
    assertTrue("valueMeta1_1".equals(rowMeta.getValueMeta(1).getName()));
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ValueMetaBase(org.pentaho.di.core.row.value.ValueMetaBase) MappingOutputMeta(org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta) Test(org.junit.Test)

Example 2 with MappingOutputMeta

use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta in project pentaho-kettle by pentaho.

the class MappingMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // First load some interesting data...
    // Then see which fields get added to the row.
    // 
    TransMeta mappingTransMeta = null;
    try {
        mappingTransMeta = loadMappingMeta(this, repository, metaStore, space);
    } catch (KettleException e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToLoadMappingTransformation"), e);
    }
    // for instance)
    if (mappingParameters != null) {
        // 
        if (mappingParameters.isInheritingAllVariables()) {
            mappingTransMeta.copyVariablesFrom(space);
        }
        // Just set the variables in the transformation statically.
        // This just means: set a number of variables or parameter values:
        // 
        List<String> subParams = Arrays.asList(mappingTransMeta.listParameters());
        for (int i = 0; i < mappingParameters.getVariable().length; i++) {
            String name = mappingParameters.getVariable()[i];
            String value = space.environmentSubstitute(mappingParameters.getInputField()[i]);
            if (!Utils.isEmpty(name) && !Utils.isEmpty(value)) {
                if (subParams.contains(name)) {
                    try {
                        mappingTransMeta.setParameterValue(name, value);
                    } catch (UnknownParamException e) {
                    // this is explicitly checked for up front
                    }
                }
                mappingTransMeta.setVariable(name, value);
            }
        }
    }
    // Keep track of all the fields that need renaming...
    // 
    List<MappingValueRename> inputRenameList = new ArrayList<MappingValueRename>();
    /*
     * Before we ask the mapping outputs anything, we should teach the mapping input steps in the sub-transformation
     * about the data coming in...
     */
    for (MappingIODefinition definition : inputMappings) {
        RowMetaInterface inputRowMeta;
        if (definition.isMainDataPath() || Utils.isEmpty(definition.getInputStepname())) {
            // The row metadata, what we pass to the mapping input step
            // definition.getOutputStep(), is "row"
            // However, we do need to re-map some fields...
            // 
            inputRowMeta = row.clone();
            if (!inputRowMeta.isEmpty()) {
                for (MappingValueRename valueRename : definition.getValueRenames()) {
                    ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta(valueRename.getSourceValueName());
                    if (valueMeta == null) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindField", valueRename.getSourceValueName()));
                    }
                    valueMeta.setName(valueRename.getTargetValueName());
                }
            }
        } else {
            // The row metadata that goes to the info mapping input comes from the
            // specified step
            // In fact, it's one of the info steps that is going to contain this
            // information...
            // 
            String[] infoSteps = getInfoSteps();
            int infoStepIndex = Const.indexOfString(definition.getInputStepname(), infoSteps);
            if (infoStepIndex < 0) {
                throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindMetadataInfo", definition.getInputStepname()));
            }
            if (info[infoStepIndex] != null) {
                inputRowMeta = info[infoStepIndex].clone();
            } else {
                inputRowMeta = null;
            }
        }
        // What is this mapping input step?
        // 
        StepMeta mappingInputStep = mappingTransMeta.findMappingInputStep(definition.getOutputStepname());
        // We're certain it's a MappingInput step...
        // 
        MappingInputMeta mappingInputMeta = (MappingInputMeta) mappingInputStep.getStepMetaInterface();
        // Inform the mapping input step about what it's going to receive...
        // 
        mappingInputMeta.setInputRowMeta(inputRowMeta);
        // What values are we changing names for?
        // 
        mappingInputMeta.setValueRenames(definition.getValueRenames());
        // 
        if (definition.isRenamingOnOutput()) {
            Mapping.addInputRenames(inputRenameList, definition.getValueRenames());
        }
    }
    // All the mapping steps now know what they will be receiving.
    // That also means that the sub-transformation / mapping has everything it
    // needs.
    // So that means that the MappingOutput steps know exactly what the output
    // is going to be.
    // That could basically be anything.
    // It also could have absolutely no resemblance to what came in on the
    // input.
    // The relative old approach is therefore no longer suited.
    // 
    // OK, but what we *can* do is have the MappingOutput step rename the
    // appropriate fields.
    // The mapping step will tell this step how it's done.
    // 
    // Let's look for the mapping output step that is relevant for this actual
    // call...
    // 
    MappingIODefinition mappingOutputDefinition = null;
    if (nextStep == null) {
        // 
        for (MappingIODefinition definition : outputMappings) {
            if (definition.isMainDataPath() || Utils.isEmpty(definition.getOutputStepname())) {
                // This is the definition to use...
                // 
                mappingOutputDefinition = definition;
            }
        }
    } else {
        for (MappingIODefinition definition : outputMappings) {
            if (nextStep.getName().equals(definition.getOutputStepname()) || definition.isMainDataPath() || Utils.isEmpty(definition.getOutputStepname())) {
                mappingOutputDefinition = definition;
            }
        }
    }
    if (mappingOutputDefinition == null) {
        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindMappingDefinition"));
    }
    // OK, now find the mapping output step in the mapping...
    // This method in TransMeta takes into account a number of things, such as
    // the step not specified, etc.
    // The method never returns null but throws an exception.
    // 
    StepMeta mappingOutputStep = mappingTransMeta.findMappingOutputStep(mappingOutputDefinition.getInputStepname());
    // We know it's a mapping output step...
    MappingOutputMeta mappingOutputMeta = (MappingOutputMeta) mappingOutputStep.getStepMetaInterface();
    // Change a few columns.
    mappingOutputMeta.setOutputValueRenames(mappingOutputDefinition.getValueRenames());
    // Perhaps we need to change a few input columns back to the original?
    // 
    mappingOutputMeta.setInputValueRenames(inputRenameList);
    // Now we know wat's going to come out of there...
    // This is going to be the full row, including all the remapping, etc.
    // 
    RowMetaInterface mappingOutputRowMeta = mappingTransMeta.getStepFields(mappingOutputStep);
    row.clear();
    row.addRowMeta(mappingOutputRowMeta);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) TransMeta(org.pentaho.di.trans.TransMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) MappingOutputMeta(org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta) MappingInputMeta(org.pentaho.di.trans.steps.mappinginput.MappingInputMeta) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException)

Example 3 with MappingOutputMeta

use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta in project pentaho-kettle by pentaho.

the class SimpleMappingMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // First load some interesting data...
    // Then see which fields get added to the row.
    // 
    TransMeta mappingTransMeta = null;
    try {
        mappingTransMeta = loadMappingMeta(this, repository, metaStore, space, mappingParameters.isInheritingAllVariables());
    } catch (KettleException e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "SimpleMappingMeta.Exception.UnableToLoadMappingTransformation"), e);
    }
    // for instance)
    if (mappingParameters != null && mappingTransMeta != null) {
        // Just set the variables in the transformation statically.
        // This just means: set a number of variables or parameter values:
        // 
        StepWithMappingMeta.activateParams(mappingTransMeta, mappingTransMeta, space, mappingTransMeta.listParameters(), mappingParameters.getVariable(), mappingParameters.getInputField(), mappingParameters.isInheritingAllVariables());
    }
    // Keep track of all the fields that need renaming...
    // 
    List<MappingValueRename> inputRenameList = new ArrayList<MappingValueRename>();
    // 
    // Before we ask the mapping outputs anything, we should teach the mapping
    // input steps in the sub-transformation about the data coming in...
    // 
    RowMetaInterface inputRowMeta;
    // The row metadata, what we pass to the mapping input step
    // definition.getOutputStep(), is "row"
    // However, we do need to re-map some fields...
    // 
    inputRowMeta = row.clone();
    if (!inputRowMeta.isEmpty()) {
        for (MappingValueRename valueRename : inputMapping.getValueRenames()) {
            ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta(valueRename.getSourceValueName());
            if (valueMeta == null) {
                throw new KettleStepException(BaseMessages.getString(PKG, "SimpleMappingMeta.Exception.UnableToFindField", valueRename.getSourceValueName()));
            }
            valueMeta.setName(valueRename.getTargetValueName());
        }
    }
    // What is this mapping input step?
    // 
    StepMeta mappingInputStep = mappingTransMeta.findMappingInputStep(null);
    // We're certain it's a MappingInput step...
    // 
    MappingInputMeta mappingInputMeta = (MappingInputMeta) mappingInputStep.getStepMetaInterface();
    // Inform the mapping input step about what it's going to receive...
    // 
    mappingInputMeta.setInputRowMeta(inputRowMeta);
    // What values are we changing names for: already done!
    // 
    mappingInputMeta.setValueRenames(null);
    // 
    if (inputMapping.isRenamingOnOutput()) {
        SimpleMapping.addInputRenames(inputRenameList, inputMapping.getValueRenames());
    }
    StepMeta mappingOutputStep = mappingTransMeta.findMappingOutputStep(null);
    // We know it's a mapping output step...
    MappingOutputMeta mappingOutputMeta = (MappingOutputMeta) mappingOutputStep.getStepMetaInterface();
    // Change a few columns.
    mappingOutputMeta.setOutputValueRenames(outputMapping.getValueRenames());
    // Perhaps we need to change a few input columns back to the original?
    // 
    mappingOutputMeta.setInputValueRenames(inputRenameList);
    // Now we know wat's going to come out of there...
    // This is going to be the full row, including all the remapping, etc.
    // 
    RowMetaInterface mappingOutputRowMeta = mappingTransMeta.getStepFields(mappingOutputStep);
    row.clear();
    row.addRowMeta(mappingOutputRowMeta);
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) TransMeta(org.pentaho.di.trans.TransMeta) ArrayList(java.util.ArrayList) MappingInputMeta(org.pentaho.di.trans.steps.mappinginput.MappingInputMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) MappingOutputMeta(org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta)

Aggregations

ArrayList (java.util.ArrayList)3 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)3 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)3 MappingOutputMeta (org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta)3 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 TransMeta (org.pentaho.di.trans.TransMeta)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 MappingInputMeta (org.pentaho.di.trans.steps.mappinginput.MappingInputMeta)2 Test (org.junit.Test)1 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 ValueMetaBase (org.pentaho.di.core.row.value.ValueMetaBase)1 MappingValueRename (org.pentaho.di.trans.steps.mapping.MappingValueRename)1