Search in sources :

Example 6 with ReflectionSchemaGenerator

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);
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Example 7 with ReflectionSchemaGenerator

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));
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Example 8 with ReflectionSchemaGenerator

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());
}
Also used : SchemaHash(co.cask.cdap.api.data.schema.SchemaHash) StreamEventCodec(co.cask.cdap.common.stream.StreamEventCodec) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) Schema(co.cask.cdap.api.data.schema.Schema) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 9 with ReflectionSchemaGenerator

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);
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) Schema(co.cask.cdap.api.data.schema.Schema) TransactionExecutor(org.apache.tephra.TransactionExecutor) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) Put(co.cask.cdap.api.dataset.table.Put) ReflectionRowReader(co.cask.cdap.internal.io.ReflectionRowReader) ReflectionPutWriter(co.cask.cdap.internal.io.ReflectionPutWriter) Row(co.cask.cdap.api.dataset.table.Row) Test(org.junit.Test)

Example 10 with ReflectionSchemaGenerator

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);
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) Schema(co.cask.cdap.api.data.schema.Schema) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Aggregations

ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)42 Test (org.junit.Test)37 Schema (co.cask.cdap.api.data.schema.Schema)23 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)12 ApplicationSpecificationAdapter (co.cask.cdap.internal.app.ApplicationSpecificationAdapter)10 Id (co.cask.cdap.common.id.Id)6 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)6 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)6 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)5 Location (org.apache.twill.filesystem.Location)5 Table (co.cask.cdap.api.dataset.table.Table)4 PluginClass (co.cask.cdap.api.plugin.PluginClass)4 InspectionApp (co.cask.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp)4 Gson (com.google.gson.Gson)4 TransactionExecutor (org.apache.tephra.TransactionExecutor)4 WordCountApp (co.cask.cdap.WordCountApp)3 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)3 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)3 VerifyResult (co.cask.cdap.app.verification.VerifyResult)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3