Search in sources :

Example 21 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class SqlResultSetReader method read.

/**
 * Returns a new table with the given tableName, constructed from the given result set
 *
 * @throws SQLException if there is a problem detected in the database
 */
public static Table read(ResultSet resultSet) throws SQLException {
    ResultSetMetaData metaData = resultSet.getMetaData();
    Table table = Table.create();
    // Setup the columns and add to the table
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
        ColumnType type = getColumnType(metaData.getColumnType(i), metaData.getScale(i), metaData.getPrecision(i));
        Preconditions.checkState(type != null, "No column type found for %s as specified for column %s", metaData.getColumnType(i), metaData.getColumnName(i));
        Column<?> newColumn = type.create(metaData.getColumnLabel(i));
        table.addColumns(newColumn);
    }
    // Add the rows
    while (resultSet.next()) {
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            Column<?> column = // subtract 1 because results sets originate at 1 not 0
            table.column(i - 1);
            if (column instanceof ShortColumn) {
                appendToColumn(column, resultSet, resultSet.getShort(i));
            } else if (column instanceof IntColumn) {
                appendToColumn(column, resultSet, resultSet.getInt(i));
            } else if (column instanceof LongColumn) {
                appendToColumn(column, resultSet, resultSet.getLong(i));
            } else if (column instanceof FloatColumn) {
                appendToColumn(column, resultSet, resultSet.getFloat(i));
            } else if (column instanceof DoubleColumn) {
                appendToColumn(column, resultSet, resultSet.getDouble(i));
            } else if (column instanceof BooleanColumn) {
                appendToColumn(column, resultSet, resultSet.getBoolean(i));
            } else {
                column.appendObj(resultSet.getObject(i));
            }
        }
    }
    return table;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) LongColumn(tech.tablesaw.api.LongColumn) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) DoubleColumn(tech.tablesaw.api.DoubleColumn) ShortColumn(tech.tablesaw.api.ShortColumn) BooleanColumn(tech.tablesaw.api.BooleanColumn) FloatColumn(tech.tablesaw.api.FloatColumn) IntColumn(tech.tablesaw.api.IntColumn)

Example 22 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class TableSliceGroup method aggregate.

/**
 * Applies the given aggregations to the given columns. The apply and combine steps of a
 * split-apply-combine.
 *
 * @param functions map from column name to aggregation to apply on that function
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
    Table groupTable = summaryTableName(sourceTable);
    StringColumn groupColumn = StringColumn.create("Group");
    groupTable.addColumns(groupColumn);
    boolean firstFunction = true;
    for (Map.Entry<String, Collection<AggregateFunction<?, ?>>> entry : functions.asMap().entrySet()) {
        String columnName = entry.getKey();
        for (AggregateFunction function : entry.getValue()) {
            String colName = aggregateColumnName(columnName, function.functionName());
            ColumnType type = function.returnType();
            Column resultColumn = type.create(colName);
            for (TableSlice subTable : getSlices()) {
                Object result = function.summarize(subTable.column(columnName));
                if (firstFunction) {
                    groupColumn.append(subTable.name());
                }
                if (function.returnType().equals(ColumnType.DOUBLE)) {
                    Number number = (Number) result;
                    resultColumn.append(number.doubleValue());
                } else {
                    resultColumn.append(result);
                }
            }
            groupTable.addColumns(resultColumn);
            firstFunction = false;
        }
    }
    return splitGroupingColumn(groupTable);
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) StringColumn(tech.tablesaw.api.StringColumn) Column(tech.tablesaw.columns.Column) AggregateFunction(tech.tablesaw.aggregate.AggregateFunction) Collection(java.util.Collection) Map(java.util.Map)

Example 23 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class XlsxReader method readMultiple.

/**
 * Read at most a table from every sheet.
 *
 * @param includeNulls include nulls for sheets without a table
 * @return a list of tables, at most one for every sheet
 */
protected List<Table> readMultiple(XlsxReadOptions options, boolean includeNulls) throws IOException {
    byte[] bytes = null;
    InputStream input = getInputStream(options, bytes);
    List<Table> tables = new ArrayList<>();
    try (XSSFWorkbook workbook = new XSSFWorkbook(input)) {
        for (Sheet sheet : workbook) {
            TableRange tableArea = findTableArea(sheet);
            if (tableArea != null) {
                Table table = createTable(sheet, tableArea, options);
                tables.add(table);
            } else if (includeNulls) {
                tables.add(null);
            }
        }
        return tables;
    } finally {
        if (options.source().reader() == null) {
            // if we get a reader back from options it means the client opened it, so let
            // the client close it
            // if it's null, we close it here.
            input.close();
        }
    }
}
Also used : Table(tech.tablesaw.api.Table) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 24 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class XlsxReader method read.

@Override
public Table read(XlsxReadOptions options) throws IOException {
    List<Table> tables = readMultiple(options, true);
    if (options.sheetIndex() != null) {
        int index = options.sheetIndex();
        if (index < 0 || index >= tables.size()) {
            throw new IndexOutOfBoundsException(String.format("Sheet index %d outside bounds. %d sheets found.", index, tables.size()));
        }
        Table table = tables.get(index);
        if (table == null) {
            throw new IllegalArgumentException(String.format("No table found at sheet index %d.", index));
        }
        return table;
    }
    // since no specific sheetIndex asked, return first table
    return tables.stream().filter(t -> t != null).findFirst().orElseThrow(() -> new IllegalArgumentException("No tables found."));
}
Also used : Table(tech.tablesaw.api.Table)

Example 25 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class Relation method summary.

public Table summary() {
    Table summaryTable = Table.create(this.name());
    if (this.columnCount() == 0) {
        return summaryTable;
    }
    summaryTable.addColumns(StringColumn.create("Measure"));
    for (int i = 0; i < this.columnCount(); i++) {
        Table columnSummary = this.column(i).summary();
        columnSummary.column(1).setName(this.column(i).name());
        summaryTable = summaryTable.joinOn("Measure").fullOuter(columnSummary, columnSummary.column(0).name());
    }
    summaryTable.column(0).setName("Summary");
    return summaryTable;
}
Also used : Table(tech.tablesaw.api.Table)

Aggregations

Table (tech.tablesaw.api.Table)42 StringColumn (tech.tablesaw.api.StringColumn)10 ArrayList (java.util.ArrayList)9 DoubleColumn (tech.tablesaw.api.DoubleColumn)8 IntColumn (tech.tablesaw.api.IntColumn)8 ColumnType (tech.tablesaw.api.ColumnType)7 Column (tech.tablesaw.columns.Column)7 TreeBasedTable (com.google.common.collect.TreeBasedTable)5 Map (java.util.Map)5 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExprColumn (tech.tablesaw.api.ExprColumn)3 Row (tech.tablesaw.api.Row)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 IAST (org.matheclipse.core.interfaces.IAST)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1