use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class RecordPutTransformer method toPut.
public Put toPut(StructuredRecord record) {
Schema recordSchema = record.getSchema();
Preconditions.checkArgument(recordSchema.getType() == Schema.Type.RECORD, "input must be a record.");
Schema.Field keyField = getKeyField(recordSchema);
Preconditions.checkArgument(keyField != null, "Could not find key field in record.");
Put output = createPut(record, keyField);
for (Schema.Field field : recordSchema.getFields()) {
if (field.getName().equals(keyField.getName())) {
continue;
}
// Skip fields that are not present in the Output Schema
if (outputSchema != null && outputSchema.getField(field.getName()) == null) {
continue;
}
setField(output, field, record.get(field.getName()));
}
return output;
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class DatasetOpExecutorServiceTest method testRest.
@Test
public void testRest() throws Exception {
// check non-existence with 404
testAdminOp(bob, "exists", 404, null);
// add instance, should automatically create an instance
dsFramework.addInstance("table", bob, DatasetProperties.EMPTY);
testAdminOp(bob, "exists", 200, true);
testAdminOp("bob", "exists", 404, null);
// check truncate
final Table table = dsFramework.getDataset(bob, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
TransactionExecutor txExecutor = new DefaultTransactionExecutor(new InMemoryTxSystemClient(txManager), ImmutableList.of((TransactionAware) table));
// writing smth to table
txExecutor.execute(() -> table.put(new Put("key1", "col1", "val1")));
// verify that we can read the data
txExecutor.execute(() -> Assert.assertEquals("val1", table.get(new Get("key1", "col1")).getString("col1")));
testAdminOp(bob, "truncate", 200, null);
// verify that data is no longer there
txExecutor.execute(() -> Assert.assertTrue(table.get(new Get("key1", "col1")).isEmpty()));
// check upgrade
testAdminOp(bob, "upgrade", 200, null);
// drop and check non-existence
dsFramework.deleteInstance(bob);
testAdminOp(bob, "exists", 404, null);
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class ReflectionTableTest method assertGetAndPut.
private void assertGetAndPut(final Table table, final byte[] rowKey, final User obj, final Schema schema) throws Exception {
// TableDataset is not accessible here, but we know that's the underlying implementation...
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) table);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Put put = new Put(rowKey);
ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(schema);
putWriter.write(obj, put);
table.put(put);
Row row = table.get(rowKey);
ReflectionRowReader<User> rowReader = new ReflectionRowReader<>(schema, TypeToken.of(User.class));
User actual = rowReader.read(row, schema);
Assert.assertEquals(obj, actual);
}
});
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class ReflectionTableTest method testStructuredRecordProjection.
@Test
public void testStructuredRecordProjection() throws Exception {
dsFrameworkUtil.createInstance("table", users, DatasetProperties.builder().build());
try {
final Table usersTable = dsFrameworkUtil.getInstance(users);
final byte[] rowKey = Bytes.toBytes(123);
final User2 projected = new User2("Samuel L.", 123L, ((Float) 50000000.02f).doubleValue(), Double.MAX_VALUE, ByteBuffer.wrap(new byte[] { 0, 1, 2 }));
final Schema fullSchema = new ReflectionSchemaGenerator().generate(User.class);
final Schema projSchema = new ReflectionSchemaGenerator().generate(User2.class);
// TableDataset is not accessible here, but we know that's the underlying implementation...
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) usersTable);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Put put = new Put(rowKey);
ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(fullSchema);
putWriter.write(SAMUEL, put);
usersTable.put(put);
Row row = usersTable.get(rowKey);
ReflectionRowRecordReader rowReader = new ReflectionRowRecordReader(projSchema, null);
StructuredRecord actual = rowReader.read(row, fullSchema);
assertRecordEqualsUser(projected, actual);
}
});
} finally {
dsFrameworkUtil.deleteInstance(users);
}
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class RecordPutTransformerTest method testNullableSchema.
@Test
public void testNullableSchema() throws Exception {
// tests that null can be passed in for the schema (in which case it will pickup the schema from the record
Schema schema = Schema.recordOf("record", Schema.Field.of("key", Schema.of(Schema.Type.INT)), Schema.Field.of("nullable", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("non_nullable", Schema.of(Schema.Type.STRING)));
RecordPutTransformer transformer = new RecordPutTransformer("key", null);
// valid record
StructuredRecord record = StructuredRecord.builder(schema).set("key", 1).set("non_nullable", "foo").build();
Put transformed = transformer.toPut(record);
Assert.assertEquals(1, Bytes.toInt(transformed.getRow()));
// expect a null value for the nullable field
Assert.assertEquals(2, transformed.getValues().size());
Assert.assertEquals("foo", Bytes.toString(transformed.getValues().get(Bytes.toBytes("non_nullable"))));
Assert.assertNull(transformed.getValues().get(Bytes.toBytes("nullable")));
}
Aggregations