Search in sources :

Example 41 with StructuredRow

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);
}
Also used : TransactionRunners(io.cdap.cdap.spi.data.transaction.TransactionRunners) StoreDefinition(io.cdap.cdap.store.StoreDefinition) Inject(com.google.inject.Inject) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Collection(java.util.Collection) Fields(io.cdap.cdap.spi.data.table.field.Fields) IOException(java.io.IOException) HashMap(java.util.HashMap) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Gson(com.google.gson.Gson) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Collections(java.util.Collections) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with StructuredRow

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));
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Row(io.cdap.cdap.api.dataset.table.Row)

Example 43 with StructuredRow

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));
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Row(io.cdap.cdap.api.dataset.table.Row)

Example 44 with StructuredRow

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

Example 45 with StructuredRow

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Collection(java.util.Collection) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14