use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
the class NoSqlStructuredTable method convertFieldsToBytes.
/**
* Convert the fields to a {@link Put} to write to table. The primary key must all be provided. The method will
* add the table name as prefix to the row key.
*
* @param fields the fields to write
* @return a PUT object
* @throws InvalidFieldException if primary keys are missing or the column is not in schema
*/
private Put convertFieldsToBytes(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()));
}
int numColumns = fields.size() - schema.getPrimaryKeys().size();
// add the table name as the prefix
MDSKey.Builder key = new MDSKey.Builder(keyPrefix);
byte[][] columns = new byte[numColumns][];
byte[][] values = new byte[numColumns][];
int i = 0;
for (Field<?> field : fields) {
fieldValidator.validateField(field);
if (schema.isPrimaryKeyColumn(field.getName())) {
addKey(key, field, schema.getType(field.getName()));
} else {
if (schema.getType(field.getName()) == null) {
throw new InvalidFieldException(schema.getTableId(), field.getName());
}
columns[i] = Bytes.toBytes(field.getName());
values[i] = fieldToBytes(field);
i++;
}
}
Put put = new Put(key.build().getKey());
for (int index = 0; index < columns.length; index++) {
put.add(columns[index], values[index]);
}
return put;
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
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);
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
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);
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
the class SpannerStructuredTable method insert.
private void insert(Collection<Field<?>> fields) throws InvalidFieldException {
List<Field<?>> insertFields = new ArrayList<>();
for (Field<?> field : fields) {
fieldValidator.validateField(field);
insertFields.add(field);
}
String sql = "INSERT INTO " + escapeName(schema.getTableId().getName()) + " (" + insertFields.stream().map(Field::getName).map(this::escapeName).collect(Collectors.joining(",")) + ") VALUES (" + insertFields.stream().map(f -> "@" + f.getName()).collect(Collectors.joining(",")) + ")";
LOG.trace("Inserting 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);
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
the class SpannerStructuredTable method delete.
@Override
public void delete(Collection<Field<?>> keys) throws InvalidFieldException {
fieldValidator.validatePrimaryKeys(keys, false);
String sql = "DELETE FROM " + escapeName(schema.getTableId().getName()) + " WHERE " + keys.stream().map(f -> escapeName(f.getName()) + " = @" + f.getName()).collect(Collectors.joining(" AND "));
Statement statement = keys.stream().reduce(Statement.newBuilder(sql), (builder, field) -> builder.bind(field.getName()).to(getValue(field)), (builder1, builder2) -> builder1).build();
transactionContext.executeUpdate(statement);
}
Aggregations