Search in sources :

Example 46 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class BufferingTable method get.

@ReadOnly
@Override
public List<Row> get(List<Get> gets) {
    ensureTransactionIsStarted();
    try {
        // get persisted, then overwrite with whats buffered
        List<Map<byte[], byte[]>> persistedRows = getPersisted(gets);
        // gets and rows lists are always of the same size
        Preconditions.checkArgument(gets.size() == persistedRows.size(), "Invalid number of rows fetched when performing multi-get. There must be one row for each get.");
        List<Row> result = Lists.newArrayListWithCapacity(persistedRows.size());
        Iterator<Map<byte[], byte[]>> persistedRowsIter = persistedRows.iterator();
        Iterator<Get> getIter = gets.iterator();
        while (persistedRowsIter.hasNext() && getIter.hasNext()) {
            Get get = getIter.next();
            Map<byte[], byte[]> persistedRow = persistedRowsIter.next();
            // navigable copy of the persisted data. Implementation may return immutable or unmodifiable maps,
            // so we make a copy here.
            NavigableMap<byte[], byte[]> rowColumns = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
            rowColumns.putAll(persistedRow);
            byte[] row = get.getRow();
            NavigableMap<byte[], Update> buffCols = buff.get(row);
            // merge what was in the buffer and what was persisted
            if (buffCols != null) {
                List<byte[]> getColumns = get.getColumns();
                byte[][] columns = getColumns == null ? null : getColumns.toArray(new byte[getColumns.size()][]);
                mergeToPersisted(rowColumns, buffCols, columns);
            }
            result.add(new Result(row, unwrapDeletes(rowColumns)));
        }
        return result;
    } catch (Exception e) {
        LOG.debug("multi-get failed for table: " + getTransactionAwareName(), e);
        throw new DataSetException("multi-get failed", e);
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Result(io.cdap.cdap.api.dataset.table.Result) DataSetException(io.cdap.cdap.api.dataset.DataSetException) Get(io.cdap.cdap.api.dataset.table.Get) Row(io.cdap.cdap.api.dataset.table.Row) Map(java.util.Map) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 47 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class NoSqlStructuredTable method read.

@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys) throws InvalidFieldException {
    LOG.trace("Table {}: Read with keys {}", schema.getTableId(), keys);
    Row row = table.get(convertKeyToBytes(keys, false));
    return row.isEmpty() ? Optional.empty() : Optional.of(new NoSqlStructuredRow(row, schema));
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Row(io.cdap.cdap.api.dataset.table.Row)

Example 48 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class NoSqlStructuredTable method read.

@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys, Collection<String> columns) throws InvalidFieldException {
    LOG.trace("Table {}: Read with keys {} and columns {}", schema.getTableId(), keys, columns);
    if (columns == null || columns.isEmpty()) {
        throw new IllegalArgumentException("No columns are specified to read");
    }
    Row row = table.get(convertKeyToBytes(keys, false), convertColumnsToBytes(columns));
    return row.isEmpty() ? Optional.empty() : Optional.of(new NoSqlStructuredRow(row, schema));
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Row(io.cdap.cdap.api.dataset.table.Row)

Example 49 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class NoSqlStructuredTable method updateFieldsToBytes.

/**
 * Updates fields in the row and converts to a {@link Put} to write to table. The primary key must be provided.
 *
 * @param fields the fields to update
 * @return a PUT object
 * @throws InvalidFieldException if primary keys are missing or the column is not in schema
 */
private Put updateFieldsToBytes(Collection<Field<?>> fields) throws InvalidFieldException {
    Set<String> fieldNames = fields.stream().map(Field::getName).collect(Collectors.toSet());
    if (!fieldNames.containsAll(schema.getPrimaryKeys())) {
        throw new InvalidFieldException(schema.getTableId(), fields, String.format("Given fields %s does not contain all the " + "primary keys %s", fieldNames, schema.getPrimaryKeys()));
    }
    Set<String> primaryKeys = new HashSet<>(schema.getPrimaryKeys());
    Collection<Field<?>> keyFields = fields.stream().filter(field -> primaryKeys.contains(field.getName())).collect(Collectors.toList());
    byte[] primaryKey = convertKeyToBytes(keyFields, false);
    Put put = new Put(primaryKey);
    Row row = table.get(primaryKey);
    Map<String, Field<?>> fieldMap = fields.stream().collect(Collectors.toMap(Field::getName, Function.identity()));
    for (Map.Entry<byte[], byte[]> entry : row.getColumns().entrySet()) {
        String columnName = Bytes.toString(entry.getKey());
        if (!fieldMap.containsKey(columnName) || primaryKeys.contains(columnName)) {
            put.add(entry.getKey(), entry.getValue());
        } else {
            put.add(entry.getKey(), fieldToBytes(fieldMap.get(columnName)));
        }
    }
    return put;
}
Also used : ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) LoggerFactory(org.slf4j.LoggerFactory) FieldValidator(io.cdap.cdap.spi.data.table.field.FieldValidator) Bytes(io.cdap.cdap.api.common.Bytes) Deque(java.util.Deque) Function(java.util.function.Function) Row(io.cdap.cdap.api.dataset.table.Row) HashSet(java.util.HashSet) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) Scanner(io.cdap.cdap.api.dataset.table.Scanner) Get(io.cdap.cdap.api.dataset.table.Get) LinkedList(java.util.LinkedList) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Put(io.cdap.cdap.api.dataset.table.Put) Collection(java.util.Collection) AbstractIterator(com.google.common.collect.AbstractIterator) Set(java.util.Set) IOException(java.io.IOException) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) List(java.util.List) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) IndexedTable(io.cdap.cdap.api.dataset.lib.IndexedTable) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Optional(java.util.Optional) StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Put(io.cdap.cdap.api.dataset.table.Put) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Row(io.cdap.cdap.api.dataset.table.Row) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) Map(java.util.Map) HashSet(java.util.HashSet)

Example 50 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class ReflectionTableTest method assertGetAndPut.

private void assertGetAndPut(final Table table, final byte[] rowKey, final User obj, final Schema schema) throws Exception {
    // TableDataset is not accessible here, but we know that's the underlying implementation...
    TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) table);
    tx.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Put put = new Put(rowKey);
            ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(schema);
            putWriter.write(obj, put);
            table.put(put);
            Row row = table.get(rowKey);
            ReflectionRowReader<User> rowReader = new ReflectionRowReader<>(schema, TypeToken.of(User.class));
            User actual = rowReader.read(row, schema);
            Assert.assertEquals(obj, actual);
        }
    });
}
Also used : ReflectionRowReader(io.cdap.cdap.internal.io.ReflectionRowReader) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put)

Aggregations

Row (io.cdap.cdap.api.dataset.table.Row)166 Scanner (io.cdap.cdap.api.dataset.table.Scanner)81 Test (org.junit.Test)50 Table (io.cdap.cdap.api.dataset.table.Table)34 Put (io.cdap.cdap.api.dataset.table.Put)29 ArrayList (java.util.ArrayList)26 TransactionExecutor (org.apache.tephra.TransactionExecutor)26 Get (io.cdap.cdap.api.dataset.table.Get)24 Schema (io.cdap.cdap.api.data.schema.Schema)21 HashMap (java.util.HashMap)19 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)16 Transaction (org.apache.tephra.Transaction)16 TransactionAware (org.apache.tephra.TransactionAware)16 IOException (java.io.IOException)14 Map (java.util.Map)14 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)13 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)12 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)10 DimensionValue (io.cdap.cdap.api.dataset.lib.cube.DimensionValue)10 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)10