use of io.cdap.cdap.data2.dataset2.lib.table.BufferingTable in project cdap by caskdata.
the class HBaseTableTest method testColumnFamily.
@Test
public void testColumnFamily() throws Exception {
DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
String tableName = "testcf";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
final BufferingTable table = getTable(CONTEXT1, tableName, props);
TransactionSystemClient txClient = new DetachedTxSystemClient();
TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(new Put("row", "column", "testValue"));
}
});
final BufferingTable table2 = getTable(CONTEXT1, tableName, props);
executor = new DefaultTransactionExecutor(txClient, table2);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
}
});
// Verify the column family name
TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
Assert.assertNotNull(hcd);
Assert.assertEquals("t", hcd.getNameAsString());
}
use of io.cdap.cdap.data2.dataset2.lib.table.BufferingTable in project cdap by caskdata.
the class HBaseTableTest method testEnableIncrements.
@Test
public void testEnableIncrements() throws Exception {
// setup a table with increments disabled and with it enabled
String disableTableName = "incr-disable";
String enabledTableName = "incr-enable";
TableId disabledTableId = hBaseTableUtil.createHTableId(NAMESPACE1, disableTableName);
TableId enabledTableId = hBaseTableUtil.createHTableId(NAMESPACE1, enabledTableName);
DatasetProperties propsDisabled = TableProperties.builder().setReadlessIncrementSupport(false).setConflictDetection(ConflictDetection.COLUMN).build();
HBaseTableAdmin disabledAdmin = getTableAdmin(CONTEXT1, disableTableName, propsDisabled);
disabledAdmin.create();
HBaseAdmin admin = TEST_HBASE.getHBaseAdmin();
DatasetProperties propsEnabled = TableProperties.builder().setReadlessIncrementSupport(true).setConflictDetection(ConflictDetection.COLUMN).build();
HBaseTableAdmin enabledAdmin = getTableAdmin(CONTEXT1, enabledTableName, propsEnabled);
enabledAdmin.create();
try {
try {
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(admin, disabledTableId);
List<String> cps = htd.getCoprocessors();
assertFalse(cps.contains(IncrementHandler.class.getName()));
htd = hBaseTableUtil.getHTableDescriptor(admin, enabledTableId);
cps = htd.getCoprocessors();
assertTrue(cps.contains(IncrementHandler.class.getName()));
} finally {
admin.close();
}
BufferingTable table = getTable(CONTEXT1, enabledTableName, propsEnabled);
byte[] row = Bytes.toBytes("row1");
byte[] col = Bytes.toBytes("col1");
DetachedTxSystemClient txSystemClient = new DetachedTxSystemClient();
Transaction tx = txSystemClient.startShort();
table.startTx(tx);
table.increment(row, col, 10);
table.commitTx();
// verify that value was written as a delta value
final byte[] expectedValue = Bytes.add(IncrementHandlerState.DELTA_MAGIC_PREFIX, Bytes.toBytes(10L));
final AtomicBoolean foundValue = new AtomicBoolean();
byte[] enabledTableNameBytes = hBaseTableUtil.getHTableDescriptor(admin, enabledTableId).getName();
TEST_HBASE.forEachRegion(enabledTableNameBytes, new Function<HRegion, Object>() {
@Override
public Object apply(HRegion hRegion) {
org.apache.hadoop.hbase.client.Scan scan = hBaseTableUtil.buildScan().build();
try {
RegionScanner scanner = hRegion.getScanner(scan);
List<Cell> results = Lists.newArrayList();
boolean hasMore;
do {
hasMore = scanner.next(results);
for (Cell cell : results) {
if (CellUtil.matchingValue(cell, expectedValue)) {
foundValue.set(true);
}
}
} while (hasMore);
} catch (IOException ioe) {
fail("IOException scanning region: " + ioe.getMessage());
}
return null;
}
});
assertTrue("Should have seen the expected encoded delta value in the " + enabledTableName + " table region", foundValue.get());
} finally {
disabledAdmin.drop();
enabledAdmin.drop();
}
}
use of io.cdap.cdap.data2.dataset2.lib.table.BufferingTable in project cdap by caskdata.
the class HBaseTableTest method testEnableIncrements.
@Test
public void testEnableIncrements() throws Exception {
// setup a table with increments disabled and with it enabled
String disableTableName = "incr-disable";
String enabledTableName = "incr-enable";
TableId disabledTableId = hBaseTableUtil.createHTableId(NAMESPACE1, disableTableName);
TableId enabledTableId = hBaseTableUtil.createHTableId(NAMESPACE1, enabledTableName);
DatasetProperties propsDisabled = TableProperties.builder().setReadlessIncrementSupport(false).setConflictDetection(ConflictDetection.COLUMN).build();
HBaseTableAdmin disabledAdmin = getTableAdmin(CONTEXT1, disableTableName, propsDisabled);
disabledAdmin.create();
HBaseAdmin admin = TEST_HBASE.getHBaseAdmin();
DatasetProperties propsEnabled = TableProperties.builder().setReadlessIncrementSupport(true).setConflictDetection(ConflictDetection.COLUMN).build();
HBaseTableAdmin enabledAdmin = getTableAdmin(CONTEXT1, enabledTableName, propsEnabled);
enabledAdmin.create();
try {
try {
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(admin, disabledTableId);
List<String> cps = htd.getCoprocessors();
assertFalse(cps.contains(IncrementHandler.class.getName()));
htd = hBaseTableUtil.getHTableDescriptor(admin, enabledTableId);
cps = htd.getCoprocessors();
assertTrue(cps.contains(IncrementHandler.class.getName()));
} finally {
admin.close();
}
try (BufferingTable table = getTable(CONTEXT1, enabledTableName, propsEnabled)) {
byte[] row = Bytes.toBytes("row1");
byte[] col = Bytes.toBytes("col1");
DetachedTxSystemClient txSystemClient = new DetachedTxSystemClient();
Transaction tx = txSystemClient.startShort();
table.startTx(tx);
table.increment(row, col, 10);
table.commitTx();
// verify that value was written as a delta value
final byte[] expectedValue = Bytes.add(IncrementHandlerState.DELTA_MAGIC_PREFIX, Bytes.toBytes(10L));
final AtomicBoolean foundValue = new AtomicBoolean();
byte[] enabledTableNameBytes = hBaseTableUtil.getHTableDescriptor(admin, enabledTableId).getName();
TEST_HBASE.forEachRegion(enabledTableNameBytes, new Function<HRegion, Object>() {
@Override
public Object apply(HRegion hRegion) {
org.apache.hadoop.hbase.client.Scan scan = hBaseTableUtil.buildScan().build();
try {
RegionScanner scanner = hRegion.getScanner(scan);
List<Cell> results = Lists.newArrayList();
boolean hasMore;
do {
hasMore = scanner.next(results);
for (Cell cell : results) {
if (CellUtil.matchingValue(cell, expectedValue)) {
foundValue.set(true);
}
}
} while (hasMore);
} catch (IOException ioe) {
fail("IOException scanning region: " + ioe.getMessage());
}
return null;
}
});
assertTrue("Should have seen the expected encoded delta value in the " + enabledTableName + " table region", foundValue.get());
}
} finally {
disabledAdmin.drop();
enabledAdmin.drop();
}
}
use of io.cdap.cdap.data2.dataset2.lib.table.BufferingTable in project cdap by caskdata.
the class HBaseTableTest method testColumnFamily.
@Test
public void testColumnFamily() throws Exception {
DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
String tableName = "testcf";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
try (BufferingTable table = getTable(CONTEXT1, tableName, props);
BufferingTable table2 = getTable(CONTEXT1, tableName, props)) {
TransactionSystemClient txClient = new DetachedTxSystemClient();
TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(new Put("row", "column", "testValue"));
}
});
executor = new DefaultTransactionExecutor(txClient, table2);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
}
});
// Verify the column family name
TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
Assert.assertNotNull(hcd);
Assert.assertEquals("t", hcd.getNameAsString());
}
}
Aggregations