Search in sources :

Example 1 with ReflectionPutWriter

use of io.cdap.cdap.internal.io.ReflectionPutWriter 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);
        }
    });
}
Also used : ReflectionRowReader(io.cdap.cdap.internal.io.ReflectionRowReader) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put)

Example 2 with ReflectionPutWriter

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

Example 3 with ReflectionPutWriter

use of io.cdap.cdap.internal.io.ReflectionPutWriter 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(io.cdap.cdap.api.dataset.table.Table) Schema(io.cdap.cdap.api.data.schema.Schema) TransactionExecutor(org.apache.tephra.TransactionExecutor) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Put(io.cdap.cdap.api.dataset.table.Put) ReflectionRowReader(io.cdap.cdap.internal.io.ReflectionRowReader) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) Row(io.cdap.cdap.api.dataset.table.Row) Test(org.junit.Test)

Example 4 with ReflectionPutWriter

use of io.cdap.cdap.internal.io.ReflectionPutWriter in project cdap by caskdata.

the class ReflectionTableTest method testStructuredRecordRepresentation.

@Test
public void testStructuredRecordRepresentation() 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);
        // 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<>(schema);
                putWriter.write(SAMUEL, put);
                usersTable.put(put);
                Row row = usersTable.get(rowKey);
                ReflectionRowRecordReader rowReader = new ReflectionRowRecordReader(schema, null);
                StructuredRecord actual = rowReader.read(row, schema);
                assertRecordEqualsUser(SAMUEL, actual);
            }
        });
    } finally {
        dsFrameworkUtil.deleteInstance(users);
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) Schema(io.cdap.cdap.api.data.schema.Schema) TransactionExecutor(org.apache.tephra.TransactionExecutor) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Put(io.cdap.cdap.api.dataset.table.Put) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) Row(io.cdap.cdap.api.dataset.table.Row) ReflectionRowRecordReader(io.cdap.cdap.internal.io.ReflectionRowRecordReader) Test(org.junit.Test)

Aggregations

Put (io.cdap.cdap.api.dataset.table.Put)4 Row (io.cdap.cdap.api.dataset.table.Row)4 ReflectionPutWriter (io.cdap.cdap.internal.io.ReflectionPutWriter)4 TransactionExecutor (org.apache.tephra.TransactionExecutor)4 Schema (io.cdap.cdap.api.data.schema.Schema)3 Table (io.cdap.cdap.api.dataset.table.Table)3 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)3 Test (org.junit.Test)3 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)2 ReflectionRowReader (io.cdap.cdap.internal.io.ReflectionRowReader)2 ReflectionRowRecordReader (io.cdap.cdap.internal.io.ReflectionRowRecordReader)2