Search in sources :

Example 6 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class PostgreSqlStructuredTable method scan.

@Override
public CloseableIterator<StructuredRow> scan(Field<?> index) throws InvalidFieldException, IOException {
    LOG.trace("Table {}: Scan index {}", tableSchema.getTableId(), index);
    fieldValidator.validateField(index);
    if (!tableSchema.isIndexColumn(index.getName())) {
        throw new InvalidFieldException(tableSchema.getTableId(), index.getName(), "is not an indexed column");
    }
    String sql = getReadQuery(Collections.singleton(index), null, false);
    // We don't close the statement here because once it is closed, the result set is also closed.
    try {
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setFetchSize(scanFetchSize);
        setField(statement, index, 1);
        LOG.trace("SQL statement: {}", statement);
        ResultSet resultSet = statement.executeQuery();
        return new ResultSetIterator(statement, resultSet, tableSchema);
    } catch (SQLException e) {
        throw new IOException(String.format("Failed to scan from table %s with index %s", tableSchema.getTableId().getName(), index), e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException)

Example 7 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException 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 8 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class SpannerStructuredTable method scan.

@Override
public CloseableIterator<StructuredRow> scan(Field<?> index) throws InvalidFieldException {
    fieldValidator.validateField(index);
    if (!schema.isIndexColumn(index.getName())) {
        throw new InvalidFieldException(schema.getTableId(), index.getName(), "is not an indexed column");
    }
    KeySet keySet = KeySet.singleKey(createKey(Collections.singleton(index)));
    String indexName = SpannerStructuredTableAdmin.getIndexName(schema.getTableId(), index.getName());
    ResultSet resultSet = transactionContext.readUsingIndex(schema.getTableId().getName(), indexName, keySet, schema.getFieldNames());
    return new ResultSetIterator(schema, resultSet);
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException)

Example 9 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class SpannerStructuredTable method read.

@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys, Collection<String> columns) throws InvalidFieldException {
    if (columns.isEmpty()) {
        throw new IllegalArgumentException("No column is specified to read");
    }
    fieldValidator.validatePrimaryKeys(keys, false);
    Set<String> missingColumns = columns.stream().filter(f -> !schema.getFieldNames().contains(f)).collect(Collectors.toSet());
    if (!missingColumns.isEmpty()) {
        throw new IllegalArgumentException("Some columns do not exists in the table schema " + missingColumns);
    }
    // Adds all the keys to the result column as well. This is mirroring the PostgreSQL implementation.
    Set<String> queryColumns = keys.stream().map(Field::getName).collect(Collectors.toSet());
    queryColumns.addAll(columns);
    Struct row = transactionContext.readRow(schema.getTableId().getName(), createKey(keys), queryColumns);
    return Optional.ofNullable(row).map(r -> new SpannerStructuredRow(schema, r));
}
Also used : IntStream(java.util.stream.IntStream) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) LoggerFactory(org.slf4j.LoggerFactory) FieldValidator(io.cdap.cdap.spi.data.table.field.FieldValidator) Fields(io.cdap.cdap.spi.data.table.field.Fields) HashMap(java.util.HashMap) Value(com.google.cloud.spanner.Value) Function(java.util.function.Function) KeyRange(com.google.cloud.spanner.KeyRange) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) ByteArray(com.google.cloud.ByteArray) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) TransactionContext(com.google.cloud.spanner.TransactionContext) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) KeySet(com.google.cloud.spanner.KeySet) Statement(com.google.cloud.spanner.Statement) Objects(java.util.Objects) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) List(java.util.List) Struct(com.google.cloud.spanner.Struct) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) Optional(java.util.Optional) StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) Collections(java.util.Collections) Struct(com.google.cloud.spanner.Struct)

Example 10 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class SpannerStructuredTable method update.

@Override
public void update(Collection<Field<?>> fields) throws InvalidFieldException {
    List<Field<?>> primaryKeyFields = new ArrayList<>();
    List<Field<?>> updateFields = new ArrayList<>();
    Set<String> fieldNames = new HashSet<>();
    for (Field<?> field : fields) {
        fieldValidator.validateField(field);
        if (schema.isPrimaryKeyColumn(field.getName())) {
            primaryKeyFields.add(field);
        } else {
            updateFields.add(field);
        }
        fieldNames.add(field.getName());
    }
    if (!fieldNames.containsAll(schema.getPrimaryKeys())) {
        throw new InvalidFieldException(schema.getTableId(), fields, String.format("Given fields %s do not contain all the " + "primary keys %s", fieldNames, schema.getPrimaryKeys()));
    }
    String sql = "UPDATE " + escapeName(schema.getTableId().getName()) + " SET " + updateFields.stream().map(this::fieldToParam).collect(Collectors.joining(", ")) + " WHERE " + primaryKeyFields.stream().map(this::fieldToParam).collect(Collectors.joining(" AND "));
    LOG.trace("Updating row: {}", sql);
    Statement statement = fields.stream().reduce(Statement.newBuilder(sql), (builder, field) -> builder.bind(field.getName()).to(getValue(field)), (builder1, builder2) -> builder1).build();
    transactionContext.executeUpdate(statement);
}
Also used : IntStream(java.util.stream.IntStream) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) LoggerFactory(org.slf4j.LoggerFactory) FieldValidator(io.cdap.cdap.spi.data.table.field.FieldValidator) Fields(io.cdap.cdap.spi.data.table.field.Fields) HashMap(java.util.HashMap) Value(com.google.cloud.spanner.Value) Function(java.util.function.Function) KeyRange(com.google.cloud.spanner.KeyRange) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) ByteArray(com.google.cloud.ByteArray) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) TransactionContext(com.google.cloud.spanner.TransactionContext) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) KeySet(com.google.cloud.spanner.KeySet) Statement(com.google.cloud.spanner.Statement) Objects(java.util.Objects) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) List(java.util.List) Struct(com.google.cloud.spanner.Struct) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) Optional(java.util.Optional) StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) Collections(java.util.Collections) Field(io.cdap.cdap.spi.data.table.field.Field) Statement(com.google.cloud.spanner.Statement) ArrayList(java.util.ArrayList) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

InvalidFieldException (io.cdap.cdap.spi.data.InvalidFieldException)26 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)14 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)14 Field (io.cdap.cdap.spi.data.table.field.Field)14 FieldType (io.cdap.cdap.spi.data.table.field.FieldType)14 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)10 ResultSet (com.google.cloud.spanner.ResultSet)10 AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)10 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)10 StructuredTableSchema (io.cdap.cdap.spi.data.table.StructuredTableSchema)10 FieldValidator (io.cdap.cdap.spi.data.table.field.FieldValidator)10 Range (io.cdap.cdap.spi.data.table.field.Range)10 IOException (java.io.IOException)10 Collection (java.util.Collection)10 Collections (java.util.Collections)10 HashSet (java.util.HashSet)10 List (java.util.List)10 Map (java.util.Map)10 Optional (java.util.Optional)10