Search in sources :

Example 11 with Put

use of co.cask.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);
    }
}
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) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord) ReflectionPutWriter(co.cask.cdap.internal.io.ReflectionPutWriter) Row(co.cask.cdap.api.dataset.table.Row) ReflectionRowRecordReader(co.cask.cdap.internal.io.ReflectionRowRecordReader) Test(org.junit.Test)

Example 12 with Put

use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.

the class IndexedTableTest method testMultipleIndexedColumns.

@Test
public void testMultipleIndexedColumns() throws Exception {
    DatasetId multiColumnTabInstance = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("multicolumntab");
    dsFrameworkUtil.createInstance("indexedTable", multiColumnTabInstance, DatasetProperties.builder().add(IndexedTable.INDEX_COLUMNS_CONF_KEY, "idx1,idx2,idx3").build());
    final byte[] idxCol1 = Bytes.toBytes("idx1");
    final byte[] idxCol2 = Bytes.toBytes("idx2");
    final byte[] idxCol3 = Bytes.toBytes("idx3");
    final IndexedTable mcTable = dsFrameworkUtil.getInstance(multiColumnTabInstance);
    try {
        TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor(mcTable);
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                // every row has idx3 = index mod 3
                for (int i = 1; i < 10; i++) {
                    Put put = new Put(Bytes.toBytes("row" + i));
                    put.add(idxCol1, idx1);
                    if (i % 2 == 0) {
                        put.add(idxCol2, idx2);
                    }
                    put.add(idxCol3, Bytes.toBytes(i % 3));
                    put.add(valCol, valA);
                    mcTable.put(put);
                }
            }
        });
        final byte[][] allColumns = new byte[][] { idxCol1, idxCol2, idxCol3, valCol };
        final byte[][] oddColumns = new byte[][] { idxCol1, idxCol3, valCol };
        final byte[] zero = Bytes.toBytes(0);
        final byte[] one = Bytes.toBytes(1);
        final byte[] two = Bytes.toBytes(2);
        // read by index 1
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Scanner scanner = mcTable.readByIndex(idxCol1, idx1);
                try {
                    // should have all rows, all data
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row1"), oddColumns, new byte[][] { idx1, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row2"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row3"), oddColumns, new byte[][] { idx1, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row4"), allColumns, new byte[][] { idx1, idx2, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row5"), oddColumns, new byte[][] { idx1, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row6"), allColumns, new byte[][] { idx1, idx2, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row7"), oddColumns, new byte[][] { idx1, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row8"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row9"), oddColumns, new byte[][] { idx1, zero, valA });
                    // should be end of rows
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
            }
        });
        // read by index 2
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Scanner scanner = mcTable.readByIndex(idxCol2, idx2);
                try {
                    // Should have only even rows
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row2"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row4"), allColumns, new byte[][] { idx1, idx2, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row6"), allColumns, new byte[][] { idx1, idx2, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row8"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    // should be at the end
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
            }
        });
        // read by index 3
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                // 0 should have rows 3, 6, 9
                Scanner scanner = mcTable.readByIndex(idxCol3, zero);
                try {
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row3"), oddColumns, new byte[][] { idx1, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row6"), allColumns, new byte[][] { idx1, idx2, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row9"), oddColumns, new byte[][] { idx1, zero, valA });
                    // should be end of rows
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
                // 1 should have rows 1, 4, 7
                scanner = mcTable.readByIndex(idxCol3, one);
                try {
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row1"), oddColumns, new byte[][] { idx1, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row4"), allColumns, new byte[][] { idx1, idx2, one, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row7"), oddColumns, new byte[][] { idx1, one, valA });
                    // should be end of rows
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
                // 2 should have rows 2, 5, 8
                scanner = mcTable.readByIndex(idxCol3, two);
                try {
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row2"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row5"), oddColumns, new byte[][] { idx1, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row8"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    // should be end of rows
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
            }
        });
        // update idx2 value for rows 2 & 4
        final byte[] idx2b = new byte[] { '2', 'b' };
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                mcTable.put(Bytes.toBytes("row2"), idxCol2, idx2b);
                mcTable.put(Bytes.toBytes("row4"), idxCol2, idx2b);
            }
        });
        // only rows 6 & 8 should be returned for idx2 now
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Scanner scanner = mcTable.readByIndex(idxCol2, idx2);
                try {
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row6"), allColumns, new byte[][] { idx1, idx2, zero, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row8"), allColumns, new byte[][] { idx1, idx2, two, valA });
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
                scanner = mcTable.readByIndex(idxCol2, idx2b);
                try {
                    Row row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row2"), allColumns, new byte[][] { idx1, idx2b, two, valA });
                    row = scanner.next();
                    TableAssert.assertRow(row, Bytes.toBytes("row4"), allColumns, new byte[][] { idx1, idx2b, one, valA });
                    assertEmpty(scanner);
                } finally {
                    scanner.close();
                }
            }
        });
    // rows 2 & 4 should be returned for idx2b
    } finally {
        dsFrameworkUtil.deleteInstance(multiColumnTabInstance);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(co.cask.cdap.api.dataset.table.Row) Put(co.cask.cdap.api.dataset.table.Put) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 13 with Put

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

Example 14 with Put

use of co.cask.cdap.api.dataset.table.Put 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 15 with Put

use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.

the class TableTest method testMultiGetWithTx.

@Test
public void testMultiGetWithTx() throws Exception {
    String testMultiGet = "testMultiGet";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, testMultiGet);
    admin.create();
    try {
        Transaction tx = txClient.startShort();
        Table table = getTable(CONTEXT1, testMultiGet);
        ((TransactionAware) table).startTx(tx);
        for (int i = 0; i < 100; i++) {
            table.put(new Put(Bytes.toBytes("r" + i)).add(C1, V1).add(C2, V2));
        }
        Assert.assertTrue(txClient.canCommit(tx, ((TransactionAware) table).getTxChanges()));
        Assert.assertTrue(((TransactionAware) table).commitTx());
        Assert.assertTrue(txClient.commit(tx));
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) table).startTx(tx2);
        List<Get> gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get(Bytes.toBytes("r" + i)));
        }
        List<Row> results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx2));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            byte[] val2 = row.get(C2);
            Assert.assertNotNull(val2);
            Assert.assertArrayEquals(V2, val2);
        }
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) table).startTx(tx3);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get("r" + i).add(C1));
        }
        results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx3));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            // should have only returned column 1
            byte[] val2 = row.get(C2);
            Assert.assertNull(val2);
        }
        // retrieve different columns per row
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) table).startTx(tx4);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            Get get = new Get("r" + i);
            // evens get C1, odds get C2
            get.add(i % 2 == 0 ? C1 : C2);
            gets.add(get);
        }
        results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx4));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val1 = row.get(C1);
            byte[] val2 = row.get(C2);
            if (i % 2 == 0) {
                Assert.assertNotNull(val1);
                Assert.assertArrayEquals(V1, val1);
                Assert.assertNull(val2);
            } else {
                Assert.assertNull(val1);
                Assert.assertNotNull(val2);
                Assert.assertArrayEquals(V2, val2);
            }
        }
    } finally {
        admin.drop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Row(co.cask.cdap.api.dataset.table.Row) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Aggregations

Put (co.cask.cdap.api.dataset.table.Put)52 Test (org.junit.Test)21 Table (co.cask.cdap.api.dataset.table.Table)20 Row (co.cask.cdap.api.dataset.table.Row)14 Get (co.cask.cdap.api.dataset.table.Get)13 TransactionExecutor (org.apache.tephra.TransactionExecutor)10 Schema (co.cask.cdap.api.data.schema.Schema)9 TransactionAware (org.apache.tephra.TransactionAware)9 Map (java.util.Map)8 Transaction (org.apache.tephra.Transaction)8 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)7 IOException (java.io.IOException)7 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)6 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)6 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)5 DataSetException (co.cask.cdap.api.dataset.DataSetException)5 Scanner (co.cask.cdap.api.dataset.table.Scanner)5 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)4 ReflectionPutWriter (co.cask.cdap.internal.io.ReflectionPutWriter)4 ImmutableMap (com.google.common.collect.ImmutableMap)4