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);
}
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);
});
}
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));
}
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));
}
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);
}
}
Aggregations