use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class CapabilityStatusStore method getConfigs.
@Override
public Map<String, CapabilityConfig> getConfigs(Collection<String> capabilities) throws IOException {
List<List<Field<?>>> multiKeys = new ArrayList<>();
capabilities.forEach(capability -> multiKeys.add(Collections.singletonList(Fields.stringField(StoreDefinition.CapabilitiesStore.NAME_FIELD, capability))));
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable capabilityTable = context.getTable(StoreDefinition.CapabilitiesStore.CAPABILITIES);
return capabilityTable.multiRead(multiKeys).stream().collect(Collectors.toMap(structuredRow -> structuredRow.getString(StoreDefinition.CapabilitiesStore.NAME_FIELD), structuredRow -> GSON.fromJson(structuredRow.getString(StoreDefinition.CapabilitiesStore.CONFIG_FIELD), CapabilityConfig.class)));
}, IOException.class);
}
use of io.cdap.cdap.spi.data.StructuredRow 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));
}
use of io.cdap.cdap.spi.data.StructuredRow 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));
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
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);
}
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class OwnerTable method getOwners.
/**
* Batch version of {@link #getOwner(NamespacedEntityId)} for getting Kerberos principals for a set of entity ids.
*
* @param ids set of ids to get the Kerberos principals
* @param <T> type of the entity id
* @return A {@link Map} from the request id to the Kerberos principal. There will be no entry for entity id that
* doesn't have an owner principal.
* @throws IOException if failed to read from the table
*/
public <T extends NamespacedEntityId> Map<T, KerberosPrincipalId> getOwners(Set<T> ids) throws IOException {
List<Collection<Field<?>>> keys = new ArrayList<>();
Map<String, T> rowKeys = new HashMap<>();
for (T id : ids) {
String rowKey = createRowKey(id);
keys.add(Collections.singleton(Fields.stringField(StoreDefinition.OwnerStore.PRINCIPAL_FIELD, rowKey)));
rowKeys.put(rowKey, id);
}
Map<T, KerberosPrincipalId> result = new HashMap<>();
for (StructuredRow row : table.multiRead(keys)) {
String principalField = row.getString(StoreDefinition.OwnerStore.PRINCIPAL_FIELD);
T id = rowKeys.get(principalField);
if (id == null) {
// This shouldn't happen as the table shouldn't return a row that is not part of the query.
throw new IllegalStateException("Row key doesn't present in the set of requested ids: " + principalField);
}
result.put(id, new KerberosPrincipalId(Bytes.toString(row.getBytes(StoreDefinition.OwnerStore.KEYTAB_FIELD))));
}
return result;
}
Aggregations