Search in sources :

Example 21 with DataTableRowInput

use of org.knime.core.node.streamable.DataTableRowInput in project knime-core by knime.

the class RowKeyUtil2 method changeRowKey.

/**
 * <p>Replaces the row key by the values of the column with the given name
 * and appends a new column with the old key values if the
 * <code>newColName</code> variable is a non empty <code>String</code>.</p>
 * <p>
 * Call the {@link RowKeyUtil2#getDuplicatesCounter()} and
 * {@link RowKeyUtil2#getMissingValueCounter()}
 * methods to get information about the replaced duplicates and missing
 * values after this method is completed.
 * </p>
 * @param inData The {@link BufferedDataTable} with the input data
 * @param exec the {@link ExecutionContext} to check for cancel and to
 * provide status messages
 * @param selRowKeyColName the name of the column which should replace
 * the row key or <code>null</code> if a new one should be created
 * @param appendColumn <code>true</code> if a new column should be created
 * @param newColSpec the {@link DataColumnSpec} of the new column or
 * <code>null</code>  if no column should be created at all
 * @param ensureUniqueness if set to <code>true</code> the method ensures
 * the uniqueness of the row key even if the values of the selected row
 * aren't unique
 * @param replaceMissingVals if set to <code>true</code> the method
 * replaces missing values with ?
 * @param removeRowKeyCol removes the selected row key column if set
 * to <code>true</code>
 * @param hiliteMap <code>true</code> if a map should be maintained that
 * maps the new row id to the old row id
 * @return the {@link BufferedDataTable} with the replaced row key and
 * the optional appended new column with the old row keys.
 * @throws Exception if the cancel button was pressed or the input data
 * isn't valid.
 */
public BufferedDataTable changeRowKey(final BufferedDataTable inData, final ExecutionContext exec, final String selRowKeyColName, final boolean appendColumn, final DataColumnSpec newColSpec, final boolean ensureUniqueness, final boolean replaceMissingVals, final boolean removeRowKeyCol, final boolean hiliteMap) throws Exception {
    LOGGER.debug("Entering changeRowKey(inData, exec, selRowKeyColName, " + "newColName) of class RowKeyUtil.");
    DataTableSpec outSpec = inData.getDataTableSpec();
    if (removeRowKeyCol) {
        outSpec = createTableSpec(outSpec, selRowKeyColName);
    }
    if (appendColumn) {
        if (newColSpec == null) {
            throw new NullPointerException("NewColumnSpec must not be null");
        }
        outSpec = AppendedColumnTable.getTableSpec(outSpec, newColSpec);
    }
    final BufferedDataContainer newContainer = exec.createDataContainer(outSpec, true);
    RowInput rowInput = new DataTableRowInput(inData);
    RowOutput rowOutput = new BufferedDataTableRowOutput(newContainer);
    changeRowKey(rowInput, rowOutput, exec, selRowKeyColName, appendColumn, newColSpec, ensureUniqueness, replaceMissingVals, removeRowKeyCol, hiliteMap, outSpec.getNumColumns(), inData.getRowCount());
    newContainer.close();
    return newContainer.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput)

Example 22 with DataTableRowInput

use of org.knime.core.node.streamable.DataTableRowInput in project knime-core by knime.

the class RowKeyNodeModel2 method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    LOGGER.debug("Entering execute(inData, exec) of class RowKeyNodeModel");
    // check input data
    if (inData == null || inData.length != 1 || inData[DATA_IN_PORT] == null) {
        throw new IllegalArgumentException("No input data available.");
    }
    final BufferedDataTable data = inData[DATA_IN_PORT];
    BufferedDataTable outData = null;
    if (m_replaceKey.getBooleanValue()) {
        // create outspec
        DataTableSpec outSpec = configure(data.getDataTableSpec(), true);
        // create table
        final BufferedDataContainer newContainer = exec.createDataContainer(outSpec, true);
        RowInput rowInput = new DataTableRowInput(data);
        RowOutput rowOutput = new BufferedDataTableRowOutput(newContainer);
        replaceKey(rowInput, rowOutput, outSpec.getNumColumns(), data.getRowCount(), exec);
        newContainer.close();
        outData = newContainer.getTable();
    } else if (m_appendRowKey.getBooleanValue()) {
        LOGGER.debug("The user only wants to append a new column with " + "name " + m_newColumnName);
        // the user wants only a column with the given name which
        // contains the rowkey as value
        final DataTableSpec tableSpec = data.getDataTableSpec();
        final String newColumnName = m_newColumnName.getStringValue();
        final ColumnRearranger c = RowKeyUtil2.createColumnRearranger(tableSpec, newColumnName, StringCell.TYPE);
        outData = exec.createColumnRearrangeTable(data, c, exec);
        exec.setMessage("New column created");
        LOGGER.debug("Column appended successfully");
    } else {
        // the user doesn't want to do anything at all so we simply return
        // the given data
        outData = data;
        LOGGER.debug("The user hasn't selected a new row ID column" + " and hasn't entered a new column name.");
    }
    LOGGER.debug("Exiting execute(inData, exec) of class RowKeyNodeModel.");
    return new BufferedDataTable[] { outData };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput)

Example 23 with DataTableRowInput

use of org.knime.core.node.streamable.DataTableRowInput in project knime-core by knime.

the class AppendedRowsNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] rawInData, final ExecutionContext exec) throws Exception {
    // remove all null tables first (optional input data)
    BufferedDataTable[] noNullArray = noNullArray(rawInData);
    DataTableSpec[] noNullSpecs = new DataTableSpec[noNullArray.length];
    for (int i = 0; i < noNullArray.length; i++) {
        noNullSpecs[i] = noNullArray[i].getDataTableSpec();
    }
    // table can only be wrapped if a suffix is to be append or the node fails in case of duplicate row ID's
    if (m_isAppendSuffix || m_isFailOnDuplicate) {
        // just wrap the tables virtually instead of traversing it and copying the rows
        // virtually create the concatenated table (no traverse necessary)
        Optional<String> suffix = m_isAppendSuffix ? Optional.of(m_suffix) : Optional.empty();
        BufferedDataTable concatTable = exec.createConcatenateTable(exec, suffix, m_isFailOnDuplicate, noNullArray);
        if (m_isIntersection) {
            // wrap the table and filter the non-intersecting columns
            DataTableSpec actualOutSpec = getOutputSpec(noNullSpecs);
            DataTableSpec currentOutSpec = concatTable.getDataTableSpec();
            String[] intersectCols = getIntersection(actualOutSpec, currentOutSpec);
            ColumnRearranger cr = new ColumnRearranger(currentOutSpec);
            cr.keepOnly(intersectCols);
            concatTable = exec.createColumnRearrangeTable(concatTable, cr, exec);
        }
        if (m_enableHiliting) {
            AppendedRowsTable tmp = new AppendedRowsTable(DuplicatePolicy.Fail, null, noNullArray);
            Map<RowKey, Set<RowKey>> map = createHiliteTranslationMap(createDuplicateMap(tmp, exec, m_suffix == null ? "" : m_suffix));
            m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
        }
        return new BufferedDataTable[] { concatTable };
    } else {
        // traverse the table and copy the rows
        long totalRowCount = 0L;
        RowInput[] inputs = new RowInput[noNullArray.length];
        for (int i = 0; i < noNullArray.length; i++) {
            totalRowCount += noNullArray[i].size();
            inputs[i] = new DataTableRowInput(noNullArray[i]);
        }
        DataTableSpec outputSpec = getOutputSpec(noNullSpecs);
        BufferedDataTableRowOutput output = new BufferedDataTableRowOutput(exec.createDataContainer(outputSpec));
        run(inputs, output, exec, totalRowCount);
        return new BufferedDataTable[] { output.getDataTable() };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RowKey(org.knime.core.data.RowKey) FilterColumnRowInput(org.knime.base.data.filter.column.FilterColumnRowInput) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) AppendedRowsRowInput(org.knime.core.data.append.AppendedRowsRowInput) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) AppendedRowsTable(org.knime.core.data.append.AppendedRowsTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput)

Example 24 with DataTableRowInput

use of org.knime.core.node.streamable.DataTableRowInput in project knime-core by knime.

the class StreamingTestNodeExecutionJob method createPortInputs.

/* multiple port inputs for each chunk -> return PortInput[chunks][ports]
     * PortInput[chunk][port] might be null if the number of chunks is smaller than the number of rows at the given port */
private PortInput[][] createPortInputs(final InputPortRole[] inputPortRoles, final PortObject[] inPortObjects, final PortType[] inPortTypes, final int numChunks, final ExecutionContext exec) throws CanceledExecutionException, IOException {
    PortInput[][] portInputs = new PortInput[numChunks][inputPortRoles.length];
    for (int i = 0; i < inputPortRoles.length; i++) {
        if (inPortObjects[i + 1] == null) {
            // nothing to do here
            continue;
        }
        // if distributed: create chunks of the input table(s)
        if (inputPortRoles[i].isDistributable() && numChunks > 1) {
            // create a own DataTableRowInput for each chunk, but only
            // if input is distributable
            BufferedDataTable[] tables = createTableChunks((BufferedDataTable) inPortObjects[i + 1], numChunks, exec);
            for (int j = 0; j < tables.length; j++) {
                if (inputPortRoles[i].isStreamable()) {
                    portInputs[j][i] = new DataTableRowInput(tables[j]);
                } else {
                    portInputs[j][i] = new PortObjectInput(tables[j]);
                }
            }
        } else {
            // no distributed execution
            for (int j = 0; j < numChunks; j++) {
                if (inputPortRoles[i].isStreamable()) {
                    portInputs[j][i] = new DataTableRowInput((BufferedDataTable) inPortObjects[i + 1]);
                } else {
                    // i here and i+1 below because port type array does not contain flow variable port, but inPortObjects does
                    final PortType portType = inPortTypes[i];
                    final boolean isData = BufferedDataTable.TYPE.equals(portType) || BufferedDataTable.TYPE_OPTIONAL.equals(portType);
                    if (isData) {
                        portInputs[j][i] = new PortObjectInput(inPortObjects[i + 1]);
                    } else {
                        portInputs[j][i] = new PortObjectInput(Node.copyPortObject(inPortObjects[i + 1], exec));
                    }
                }
            }
        }
    }
    return portInputs;
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) PortInput(org.knime.core.node.streamable.PortInput) PortType(org.knime.core.node.port.PortType)

Aggregations

DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)24 BufferedDataTableRowOutput (org.knime.core.node.streamable.BufferedDataTableRowOutput)16 BufferedDataTable (org.knime.core.node.BufferedDataTable)15 DataTableSpec (org.knime.core.data.DataTableSpec)9 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)9 RowInput (org.knime.core.node.streamable.RowInput)7 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)6 RowOutput (org.knime.core.node.streamable.RowOutput)6 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)4 DataRow (org.knime.core.data.DataRow)3 ExecutionContext (org.knime.core.node.ExecutionContext)3 PortObject (org.knime.core.node.port.PortObject)3 PortInput (org.knime.core.node.streamable.PortInput)3 DataCell (org.knime.core.data.DataCell)2 PartitionInfo (org.knime.core.node.streamable.PartitionInfo)2 PortObjectInput (org.knime.core.node.streamable.PortObjectInput)2 PortOutput (org.knime.core.node.streamable.PortOutput)2 SimpleStreamableOperatorInternals (org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)2 File (java.io.File)1 IOException (java.io.IOException)1