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