Search in sources :

Example 71 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class PostgreSqlStructuredTable method increment.

@Override
public void increment(Collection<Field<?>> keys, String column, long amount) throws InvalidFieldException, IOException {
    LOG.trace("Table {}: Increment with keys {}, column {}, amount {}", tableSchema.getTableId(), keys, column, amount);
    FieldType.Type colType = tableSchema.getType(column);
    if (colType == null) {
        throw new InvalidFieldException(tableSchema.getTableId(), column);
    } else if (colType != FieldType.Type.LONG) {
        throw new IllegalArgumentException(String.format("Trying to increment a column of type %s. Only %s column type can be incremented", colType, FieldType.Type.LONG));
    }
    if (tableSchema.isPrimaryKeyColumn(column)) {
        throw new IllegalArgumentException("Cannot use increment on a primary key field");
    }
    fieldValidator.validatePrimaryKeys(keys, false);
    List<Field<?>> fieldsWithValue = new ArrayList<>(keys);
    // If the row does not exist, insert it with long field = amount
    fieldsWithValue.add(Fields.longField(column, amount));
    String sql = getWriteSqlQuery(fieldsWithValue, column);
    try (PreparedStatement statement = connection.prepareStatement(sql)) {
        int index = 1;
        for (Field<?> key : fieldsWithValue) {
            setField(statement, key, index);
            index++;
        }
        // populate increment amount
        statement.setLong(index, amount);
        LOG.trace("SQL statement: {}", statement);
        statement.executeUpdate();
    } catch (SQLException e) {
        throw new IOException(String.format("Failed to increment column %s of table %s with increment value %d", column, tableSchema.getTableId().getName(), amount), e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) Field(io.cdap.cdap.spi.data.table.field.Field) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException)

Example 72 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

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)

Aggregations

Field (io.cdap.cdap.spi.data.table.field.Field)72 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)40 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)33 ArrayList (java.util.ArrayList)33 Range (io.cdap.cdap.spi.data.table.field.Range)24 HashSet (java.util.HashSet)18 Map (java.util.Map)17 List (java.util.List)16 Nullable (javax.annotation.Nullable)16 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)15 Collections (java.util.Collections)15 HashMap (java.util.HashMap)15 LinkedHashSet (java.util.LinkedHashSet)15 Set (java.util.Set)15 Fields (io.cdap.cdap.spi.data.table.field.Fields)14 Collection (java.util.Collection)14 LinkedHashMap (java.util.LinkedHashMap)14 Collectors (java.util.stream.Collectors)14 Optional (java.util.Optional)13 IOException (java.io.IOException)12