use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testGeneratorRecursion.
@SuppressWarnings("ConstantConditions")
@Test
public void testGeneratorRecursion() throws Exception {
// This test the schema generator be able to handle recursive schema between multiple classes
Schema generatedSchema = new ReflectionSchemaGenerator(false).generate(NodeParent.class);
// Validate the NodeChild.parent schema is the same as the schema of the Parent class
// The schema generator always generate array value as nullable union
Schema childSchema = generatedSchema.getField("children").getSchema().getComponentSchema().getNonNullable();
Assert.assertEquals(generatedSchema, childSchema.getField("parent").getSchema());
// Serialize the schema and then parse it back
Schema deserializedSchema = Schema.parseJson(generatedSchema.toString());
Assert.assertEquals(generatedSchema, deserializedSchema);
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testCompatible.
@Test
public void testCompatible() throws UnsupportedTypeException {
Schema s1 = new ReflectionSchemaGenerator().generate(Node.class);
Schema s2 = new ReflectionSchemaGenerator().generate(Node3.class);
Schema s3 = new ReflectionSchemaGenerator().generate(Node4.class);
Assert.assertNotEquals(s1, s2);
Assert.assertTrue(s1.isCompatible(s2));
Assert.assertFalse(s2.isCompatible(s1));
Assert.assertTrue(s2.isCompatible(s3));
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class StreamEventCodecTest method testEncodeDecodeWithDatumDecoder.
@Test
public void testEncodeDecodeWithDatumDecoder() throws UnsupportedTypeException, IOException {
StreamEvent event = new StreamEvent(Maps.<String, String>newHashMap(), ByteBuffer.wrap("Event string".getBytes(Charsets.UTF_8)));
StreamEventCodec codec = new StreamEventCodec();
ByteBuffer payload = ByteBuffer.wrap(codec.encodePayload(event));
SchemaHash schemaHash = new SchemaHash(payload);
Schema schema = new ReflectionSchemaGenerator().generate(StreamEvent.class);
Assert.assertEquals(schema.getSchemaHash(), schemaHash);
StreamEvent decoded = new ReflectionDatumReader<>(schema, TypeToken.of(StreamEvent.class)).read(new BinaryDecoder(new ByteBufferInputStream(payload)), schema);
Assert.assertEquals(event.getHeaders(), decoded.getHeaders());
Assert.assertEquals(event.getBody(), decoded.getBody());
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class ReflectionTableTest method testTypeProjection.
@Test
public void testTypeProjection() 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);
ReflectionRowReader<User2> rowReader = new ReflectionRowReader<>(projSchema, TypeToken.of(User2.class));
User2 actual = rowReader.read(row, fullSchema);
Assert.assertEquals(projected, actual);
}
});
} finally {
dsFrameworkUtil.deleteInstance(users);
}
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class ReflectionTableTest method testNullFields.
@Test
public void testNullFields() throws Exception {
dsFrameworkUtil.createInstance("table", users, DatasetProperties.builder().build());
try {
final Table usersTable = dsFrameworkUtil.getInstance(users);
final byte[] rowKey = Bytes.toBytes(123);
final Schema schema = new ReflectionSchemaGenerator().generate(User.class);
assertGetAndPut(usersTable, rowKey, SAMUEL, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_TS, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_ID, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_FIRST, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_SALARY, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_PURCHASE, schema);
assertGetAndPut(usersTable, rowKey, SAMUEL_NO_BLOB, schema);
} finally {
dsFrameworkUtil.deleteInstance(users);
}
}
Aggregations