Search in sources :

Example 26 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class HierarchicalClusterNodeModel method createResultTable.

/**
 * Creates a standard table as the result table. The result table is
 * constructed for the desired number of clusters.
 *
 * @param inputData
 *            the input data table which has the meta information like
 *            column names classes, and so on
 *
 * @param clusters
 *            the vector with the clusters
 * @param exec
 *            to check for user cancelations
 * @return the result data table which contains the data rows with the class
 *         information
 * @throws CanceledExecutionException
 *             if user canceled
 */
private DataTable createResultTable(final DataTable inputData, final List<ClusterNode> clusters, final ExecutionContext exec) throws CanceledExecutionException {
    DataTableSpec inputSpec = inputData.getDataTableSpec();
    DataTableSpec outputSpec = generateOutSpec(inputSpec);
    DataContainer resultTable = exec.createDataContainer(outputSpec);
    for (int i = 0; i < clusters.size(); i++) {
        DataRow[] memberRows = clusters.get(i).getAllDataRows();
        for (DataRow dataRow : memberRows) {
            DataCell[] cells = new DataCell[dataRow.getNumCells() + 1];
            for (int j = 0; j < dataRow.getNumCells(); j++) {
                cells[j] = dataRow.getCell(j);
            }
            // append the cluster id the row belongs to
            cells[cells.length - 1] = new StringCell("cluster_" + i);
            resultTable.addRowToTable(new DefaultRow(dataRow.getKey(), cells));
            exec.checkCanceled();
        }
    }
    resultTable.close();
    return resultTable.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataContainer(org.knime.core.data.container.DataContainer) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow)

Example 27 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class DBReaderImpl method createTable.

/**
 * Called from the database port to read the first n-number of rows.
 *
 * @param useDbRowId <code>true</code> if the KNIME row id should based on the db row id
 * @param cachedNoRows number of rows cached for data preview
 * @param cp {@link CredentialsProvider} providing user/password
 * @return buffered data table read from database
 * @throws SQLException if the connection could not be opened
 */
DataTable createTable(final boolean useDbRowId, final int cachedNoRows, final CredentialsProvider cp) throws SQLException {
    if (m_blobFactory == null) {
        m_blobFactory = new BinaryObjectCellFactory();
    }
    // retrieve connection
    // final Connection conn = initConnection(cp);
    final DatabaseQueryConnectionSettings dbConn = getQueryConnection();
    // synchronized (dbConn.syncConnection(conn)) {
    return dbConn.execute(cp, conn -> {
        // remember auto-commit flag
        final boolean autoCommit = conn.getAutoCommit();
        final Statement stmt = initStatement(cp, conn);
        try {
            final String[] oQueries = dbConn.getQuery().split(SQL_QUERY_SEPARATOR);
            if (cachedNoRows < 0) {
                int fetchsize = (DatabaseConnectionSettings.FETCH_SIZE != null) ? DatabaseConnectionSettings.FETCH_SIZE : -1;
                dbConn.getUtility().getStatementManipulator().setFetchSize(stmt, fetchsize);
            } else {
                final int hashAlias = System.identityHashCode(this);
                final int selectIdx = oQueries.length - 1;
                // replace last element in statement(s) with wrapped SQL
                oQueries[selectIdx] = "SELECT * FROM (" + oQueries[selectIdx] + ") table_" + hashAlias;
                try {
                    // bugfix 2925: may fail, e.g. on sqlite
                    stmt.setMaxRows(cachedNoRows);
                } catch (SQLException ex) {
                    Throwable cause = ExceptionUtils.getRootCause(ex);
                    if (cause == null) {
                        cause = ex;
                    }
                    LOGGER.warn("Can't set max rows on statement, reason: " + cause.getMessage(), ex);
                }
            }
            // execute all except the last query
            for (int i = 0; i < oQueries.length - 1; i++) {
                LOGGER.debug("Executing SQL statement as execute: " + oQueries[i]);
                stmt.execute(oQueries[i]);
            }
            final String lastQuery = oQueries[oQueries.length - 1];
            LOGGER.debug("Executing SQL statement as executeQuery: " + lastQuery);
            final ResultSet result = stmt.executeQuery(lastQuery);
            LOGGER.debug("Reading meta data from database ResultSet...");
            m_spec = createTableSpec(result.getMetaData());
            LOGGER.debug("Parsing database ResultSet...");
            // final DBRowIterator dbIt = createRowIterator(useDbRowId, result);
            final RowIterator it = createDBRowIterator(m_spec, dbConn, m_blobFactory, useDbRowId, result);
            DataContainer buf = new DataContainer(m_spec);
            while (it.hasNext()) {
                buf.addRowToTable(it.next());
            }
            buf.close();
            return buf.getTable();
        } finally {
            if (stmt != null) {
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
                DatabaseConnectionSettings.setAutoCommit(conn, autoCommit);
                stmt.close();
            }
        }
    });
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataContainer(org.knime.core.data.container.DataContainer) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) BinaryObjectCellFactory(org.knime.core.data.blob.BinaryObjectCellFactory) RowIterator(org.knime.core.data.RowIterator) ResultSet(java.sql.ResultSet)

Example 28 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class MissingValueApplyNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[DATA_PORT_IDX];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    PMMLPortObject pmmlIn = (PMMLPortObject) inData[PMML_PORT_IDX];
    MissingCellReplacingDataTable mvTable;
    try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
        mvTable = new MissingCellReplacingDataTable(inSpec, PMMLDocument.Factory.parse(supplier.get()));
    }
    // Calculate the statistics
    mvTable.init(inTable, exec.createSubExecutionContext(0.5));
    long rowCounter = 0;
    final long numOfRows = inTable.size();
    DataContainer container = exec.createDataContainer(mvTable.getDataTableSpec());
    for (DataRow row : mvTable) {
        exec.checkCanceled();
        if (row != null) {
            exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows + " (\"" + row.getKey() + "\")");
            container.addRowToTable(row);
        } else {
            exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows);
        }
    }
    container.close();
    // Collect warning messages
    String warnings = mvTable.finish();
    // Handle the warnings
    if (warnings.length() > 0) {
        setWarningMessage(warnings);
    }
    return new PortObject[] { (BufferedDataTable) container.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataContainer(org.knime.core.data.container.DataContainer) MissingCellReplacingDataTable(org.knime.base.node.preproc.pmml.missingval.MissingCellReplacingDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) DataRow(org.knime.core.data.DataRow) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject)

Example 29 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class InteractiveHiLiteCollectorNodeModel method getHiLiteAnnotationsTable.

/**
 * @return table with hilit rows first and then all rows with annotations
 */
DataTable getHiLiteAnnotationsTable() {
    // return null if not executed
    if (m_data == null) {
        return null;
    }
    DataContainer buf;
    if (m_annotationMap.isEmpty()) {
        buf = new DataContainer(new DataTableSpec());
    } else {
        buf = new DataContainer(new DataTableSpec(createSpecs(null)));
    }
    HiLiteHandler hdl = getInHiLiteHandler(0);
    if (hdl != null) {
        for (RowKey key : hdl.getHiLitKeys()) {
            DataCell[] cells = new DataCell[buf.getTableSpec().getNumColumns()];
            for (int i = 0; i < cells.length; i++) {
                Map<Integer, String> map = m_annotationMap.get(key);
                if (map == null) {
                    cells[i] = DataType.getMissingCell();
                } else {
                    String str = m_annotationMap.get(key).get(i);
                    if (str == null) {
                        cells[i] = DataType.getMissingCell();
                    } else {
                        cells[i] = new StringCell(str);
                    }
                }
            }
            buf.addRowToTable(new DefaultRow(key, cells));
        }
        for (RowKey key : m_annotationMap.keySet()) {
            if (!hdl.isHiLit(key)) {
                DataCell[] cells = new DataCell[buf.getTableSpec().getNumColumns()];
                for (int i = 0; i < cells.length; i++) {
                    String str = m_annotationMap.get(key).get(i);
                    if (str == null) {
                        cells[i] = DataType.getMissingCell();
                    } else {
                        cells[i] = new StringCell(str);
                    }
                }
                buf.addRowToTable(new DefaultRow(key, cells));
            }
        }
    }
    buf.close();
    return buf.getTable();
}
Also used : DataContainer(org.knime.core.data.container.DataContainer) DataTableSpec(org.knime.core.data.DataTableSpec) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) RowKey(org.knime.core.data.RowKey) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 30 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class NominalValueRowFilterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("null")
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // include data container
    DataContainer positive = exec.createDataContainer(inData[0].getDataTableSpec());
    DataContainer negative = m_splitter ? exec.createDataContainer(inData[0].getDataTableSpec()) : null;
    long currentRow = 0;
    for (DataRow row : inData[0]) {
        // if row matches to included...
        if (matches(row)) {
            positive.addRowToTable(row);
        } else if (m_splitter) {
            negative.addRowToTable(row);
        }
        exec.setProgress(currentRow / (double) inData[0].size(), "filtering row # " + currentRow);
        currentRow++;
        exec.checkCanceled();
    }
    positive.close();
    BufferedDataTable positiveTable = exec.createBufferedDataTable(positive.getTable(), exec);
    if (positiveTable.size() <= 0) {
        String warning = "No rows matched!";
        if (m_splitter) {
            warning = warning + " Input mirrored at out-port 1 (excluded)";
        }
        setWarningMessage(warning);
    }
    if (m_splitter) {
        negative.close();
        BufferedDataTable negativeTable = exec.createBufferedDataTable(negative.getTable(), exec);
        if (negativeTable.size() <= 0) {
            setWarningMessage("All rows matched! Input mirrored at out-port 0 (included)");
        }
        return new BufferedDataTable[] { positiveTable, negativeTable };
    }
    return new BufferedDataTable[] { positiveTable };
}
Also used : DataContainer(org.knime.core.data.container.DataContainer) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataRow(org.knime.core.data.DataRow)

Aggregations

DataContainer (org.knime.core.data.container.DataContainer)35 DataTableSpec (org.knime.core.data.DataTableSpec)25 DefaultRow (org.knime.core.data.def.DefaultRow)21 DataRow (org.knime.core.data.DataRow)19 DataCell (org.knime.core.data.DataCell)17 BufferedDataTable (org.knime.core.node.BufferedDataTable)15 RowKey (org.knime.core.data.RowKey)10 ArrayList (java.util.ArrayList)9 DoubleCell (org.knime.core.data.def.DoubleCell)9 IntCell (org.knime.core.data.def.IntCell)8 LinkedHashMap (java.util.LinkedHashMap)7 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)7 HashSet (java.util.HashSet)6 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 RowIterator (org.knime.core.data.RowIterator)6 StringCell (org.knime.core.data.def.StringCell)6 Map (java.util.Map)5 Set (java.util.Set)5 SortedTable (org.knime.base.data.sort.SortedTable)5 DataTable (org.knime.core.data.DataTable)5