use of io.cdap.cdap.spi.data.table.field.Fields in project cdap by caskdata.
the class PostgreSqlStructuredTable method updateInternal.
private void updateInternal(Collection<Field<?>> fields) throws IOException {
String sqlQuery = getUpdateSqlQuery(fields);
try (PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
Map<Boolean, List<Field<?>>> lists = fields.stream().collect(Collectors.partitioningBy(field -> tableSchema.isPrimaryKeyColumn(field.getName())));
int index = 1;
// set field values to update
for (Field<?> field : lists.get(false)) {
setField(statement, field, index);
index++;
}
// set field values in the WHERE clause
for (Field<?> field : lists.get(true)) {
setField(statement, field, index);
index++;
}
LOG.trace("SQL statement: {}", statement);
statement.executeUpdate();
} catch (SQLException e) {
throw new IOException(String.format("Failed to write to table %s with fields %s", tableSchema.getTableId().getName(), fields), e);
}
}
use of io.cdap.cdap.spi.data.table.field.Fields in project cdap by caskdata.
the class StructuredTableConcurrencyTest method testConcurrentUpsert.
@Test
public void testConcurrentUpsert() throws Exception {
// Concurrently write rows
int numThreads = 20;
AtomicInteger idGenerator = new AtomicInteger();
String strVal = "write-val-";
runConcurrentOperation("concurrent-upsert", numThreads, () -> TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TEST_TABLE);
int id = idGenerator.getAndIncrement();
List<Field<?>> fields = Arrays.asList(Fields.intField(KEY, id), Fields.stringField(STRING_COL, strVal + id), Fields.longField(LONG_COL, (long) id));
table.upsert(fields);
}));
// Verify all writes
AtomicInteger count = new AtomicInteger();
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TEST_TABLE);
try (CloseableIterator<StructuredRow> iterator = table.scan(Range.all(), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
Assert.assertEquals(Integer.valueOf(count.get()), row.getInteger(KEY));
Assert.assertEquals(strVal + count.get(), row.getString(STRING_COL));
Assert.assertEquals(Long.valueOf(count.get()), row.getLong(LONG_COL));
count.incrementAndGet();
}
}
});
Assert.assertEquals(numThreads, count.get());
}
use of io.cdap.cdap.spi.data.table.field.Fields 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.table.field.Fields 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);
}
Aggregations