Search in sources :

Example 1 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class SqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    initIfNeeded();
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        StructuredTableId tableId = specification.getTableId();
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (optional.isPresent()) {
            throw new TableAlreadyExistsException(tableId);
        }
        LOG.debug("Registering table specification {}", specification);
        registry.upsert(Arrays.asList(Fields.stringField(TABLE_NAME_FIELD, tableId.getName()), Fields.stringField(TABLE_SPEC_FIELD, GSON.toJson(specification))));
    }, TableAlreadyExistsException.class);
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 2 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class SqlStructuredTableRegistry method getSpecification.

@Override
@Nullable
public StructuredTableSpecification getSpecification(StructuredTableId tableId) {
    initIfNeeded();
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (!optional.isPresent()) {
            return null;
        }
        String specString = optional.get().getString(TABLE_SPEC_FIELD);
        LOG.trace("Got specification {} from registry", specString);
        return GSON.fromJson(specString, StructuredTableSpecification.class);
    });
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Nullable(javax.annotation.Nullable)

Example 3 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

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 4 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

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 5 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class PostgreSqlStructuredTable method multiRead.

@Override
public Collection<StructuredRow> multiRead(Collection<? extends Collection<Field<?>>> multiKeys) throws InvalidFieldException, IOException {
    LOG.trace("Table {}: Read with multiple keys {}", tableSchema.getTableId(), multiKeys);
    if (multiKeys.isEmpty()) {
        return Collections.emptyList();
    }
    for (Collection<Field<?>> keys : multiKeys) {
        fieldValidator.validatePrimaryKeys(keys, false);
    }
    // Collapse values of each key
    Map<String, Set<Field<?>>> keyFields = new LinkedHashMap<>();
    multiKeys.stream().flatMap(Collection::stream).forEach(field -> keyFields.computeIfAbsent(field.getName(), k -> new LinkedHashSet<>()).add(field));
    try (PreparedStatement statement = prepareMultiReadQuery(keyFields)) {
        LOG.trace("SQL statement: {}", statement);
        Collection<StructuredRow> result = new ArrayList<>();
        try (ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                result.add(resultSetToRow(resultSet));
            }
            return result;
        }
    } catch (SQLException e) {
        throw new IOException(String.format("Failed to read from table %s with multi keys %s", tableSchema.getTableId().getName(), multiKeys), e);
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SQLException(java.sql.SQLException) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Field(io.cdap.cdap.spi.data.table.field.Field) ResultSet(java.sql.ResultSet)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)71 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)34 Field (io.cdap.cdap.spi.data.table.field.Field)33 ArrayList (java.util.ArrayList)27 Range (io.cdap.cdap.spi.data.table.field.Range)18 HashSet (java.util.HashSet)14 IOException (java.io.IOException)11 HashMap (java.util.HashMap)11 LinkedHashSet (java.util.LinkedHashSet)11 List (java.util.List)10 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)9 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)8 Collection (java.util.Collection)8 Nullable (javax.annotation.Nullable)8 ImmutableList (com.google.common.collect.ImmutableList)7 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)7 Fields (io.cdap.cdap.spi.data.table.field.Fields)7 Collections (java.util.Collections)7 Set (java.util.Set)7