use of co.cask.cdap.api.dataset.table.Row 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);
}
}
use of co.cask.cdap.api.dataset.table.Row in project cdap by caskdata.
the class IndexedTableTest method testIndexKeyDelimiterHandling.
/**
* Test conditions where the indexed column name or column value may contain the key delimiter.
* @throws Exception
*/
@Test
public void testIndexKeyDelimiterHandling() throws Exception {
DatasetId delimTabInstance = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("delimtab");
dsFrameworkUtil.createInstance("indexedTable", delimTabInstance, DatasetProperties.builder().add(IndexedTable.INDEX_COLUMNS_CONF_KEY, idxColString).build());
final IndexedTable iTable = dsFrameworkUtil.getInstance(delimTabInstance);
final byte[] delim = new byte[] { 0 };
try {
final byte[] valueWithDelimiter = Bytes.concat(idx1, delim, idx2);
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor(iTable);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
iTable.put(keyA, idxCol, idx1);
iTable.put(keyB, idxCol, valueWithDelimiter);
iTable.put(keyC, idxCol, idx2);
}
});
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Scanner scanner = iTable.readByIndex(idxCol, idx1);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyA, new byte[][] { idxCol }, new byte[][] { idx1 });
assertEmpty(scanner);
} finally {
scanner.close();
}
scanner = iTable.readByIndex(idxCol, idx2);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyC, new byte[][] { idxCol }, new byte[][] { idx2 });
assertEmpty(scanner);
} finally {
scanner.close();
}
scanner = iTable.readByIndex(idxCol, valueWithDelimiter);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyB, new byte[][] { idxCol }, new byte[][] { valueWithDelimiter });
assertEmpty(scanner);
} finally {
scanner.close();
}
}
});
} finally {
dsFrameworkUtil.deleteInstance(delimTabInstance);
}
}
use of co.cask.cdap.api.dataset.table.Row 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 co.cask.cdap.api.dataset.table.Row 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.api.dataset.table.Row in project cdap by caskdata.
the class TableTest method testEmptyGet.
@Test
public void testEmptyGet() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
Transaction tx = txClient.startShort();
Table myTable = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable).startTx(tx);
myTable.put(R1, C1, V1);
myTable.put(R1, C2, V2);
// to be used for validation later
TreeMap<byte[], byte[]> expectedColumns = new TreeMap<>(Bytes.BYTES_COMPARATOR);
expectedColumns.put(C1, V1);
expectedColumns.put(C2, V2);
Result expectedResult = new Result(R1, expectedColumns);
Result emptyResult = new Result(R1, ImmutableMap.<byte[], byte[]>of());
((TransactionAware) myTable).commitTx();
txClient.commit(tx);
// start another transaction, so that the buffering table doesn't cache the values; the underlying Table
// implementations are tested this way.
tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
Row row = myTable.get(R1, new byte[][] { C1, C2 });
assertEquals(expectedResult, row);
// passing in empty columns returns empty result
row = myTable.get(R1, new byte[][] {});
assertEquals(emptyResult, row);
// test all the Get constructors and their behavior
// constructors specifying only rowkey retrieve all columns
Get get = new Get(R1);
Assert.assertNull(get.getColumns());
assertEquals(expectedResult, myTable.get(get));
get = new Get(Bytes.toString(R1));
Assert.assertNull(get.getColumns());
assertEquals(expectedResult, myTable.get(get));
get.add(C1);
get.add(Bytes.toString(C2));
assertEquals(expectedResult, myTable.get(get));
// constructor specifying columns, but with an empty array/collection retrieve 0 columns
get = new Get(R1, new byte[][] {});
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(R1, ImmutableList.<byte[]>of());
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(Bytes.toString(R1), new String[] {});
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(Bytes.toString(R1), ImmutableList.<String>of());
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
row = myTable.get(R1, new byte[][] {});
assertEquals(emptyResult, row);
txClient.abort(tx);
} finally {
admin.drop();
}
}
Aggregations