Search in sources :

Example 61 with TableId

use of co.cask.cdap.data2.util.TableId 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) {
                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();
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) IOException(java.io.IOException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) BufferingTable(co.cask.cdap.data2.dataset2.lib.table.BufferingTable) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) Transaction(org.apache.tephra.Transaction) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) DetachedTxSystemClient(org.apache.tephra.inmemory.DetachedTxSystemClient) Scan(org.apache.hadoop.hbase.client.Scan) List(java.util.List) Cell(org.apache.hadoop.hbase.Cell) BufferingTableTest(co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest) Test(org.junit.Test)

Example 62 with TableId

use of co.cask.cdap.data2.util.TableId 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());
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) IOException(java.io.IOException) Put(co.cask.cdap.api.dataset.table.Put) BufferingTable(co.cask.cdap.data2.dataset2.lib.table.BufferingTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) Get(co.cask.cdap.api.dataset.table.Get) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) DetachedTxSystemClient(org.apache.tephra.inmemory.DetachedTxSystemClient) NamespaceId(co.cask.cdap.proto.id.NamespaceId) BufferingTableTest(co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest) Test(org.junit.Test)

Example 63 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class HBaseTableTest method testPreSplit.

@Test
public void testPreSplit() throws Exception {
    byte[][] splits = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
    DatasetProperties props = DatasetProperties.builder().add("hbase.splits", new Gson().toJson(splits)).build();
    String presplittedTable = "presplitted";
    getTableAdmin(CONTEXT1, presplittedTable, props).create();
    try (HBaseAdmin hBaseAdmin = TEST_HBASE.getHBaseAdmin()) {
        TableId hTableId = hBaseTableUtil.createHTableId(NAMESPACE1, presplittedTable);
        List<HRegionInfo> regions = hBaseTableUtil.getTableRegions(hBaseAdmin, hTableId);
        // note: first region starts at very first row key, so we have one extra to the splits count
        Assert.assertEquals(4, regions.size());
        Assert.assertArrayEquals(Bytes.toBytes("a"), regions.get(1).getStartKey());
        Assert.assertArrayEquals(Bytes.toBytes("b"), regions.get(2).getStartKey());
        Assert.assertArrayEquals(Bytes.toBytes("c"), regions.get(3).getStartKey());
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) Gson(com.google.gson.Gson) BufferingTableTest(co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest) Test(org.junit.Test)

Example 64 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class MetricHBaseTableUtilTest method testGetVersion.

@Test
public void testGetVersion() throws Exception {
    // Verify new metric datasets are properly recognized as 2.8+ version from now on
    HBaseMetricsTableDefinition definition = new HBaseMetricsTableDefinition("foo", TEST_HBASE.getConfiguration(), hBaseTableUtil, new LocalLocationFactory(TMP_FOLDER.newFolder()), cConf);
    DatasetSpecification spec = definition.configure("metricV2.8", DatasetProperties.EMPTY);
    DatasetAdmin admin = definition.getAdmin(DatasetContext.from(NamespaceId.SYSTEM.getNamespace()), spec, null);
    admin.create();
    MetricHBaseTableUtil util = new MetricHBaseTableUtil(hBaseTableUtil);
    HBaseAdmin hAdmin = TEST_HBASE.getHBaseAdmin();
    TableId hTableId = hBaseTableUtil.createHTableId(NamespaceId.SYSTEM, spec.getName());
    HTableDescriptor desc = hBaseTableUtil.getHTableDescriptor(hAdmin, hTableId);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_8_OR_HIGHER, util.getVersion(desc));
    // Verify HBase table without coprocessor is properly recognized as 2.6- version
    TableName table26 = TableName.valueOf("metricV2.6");
    hAdmin.createTable(new HTableDescriptor(table26));
    desc = hAdmin.getTableDescriptor(table26);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_6_OR_LOWER, util.getVersion(desc));
    // Verify HBase table with IncrementHandler coprocessor but without cdap.version on it is properly recognized as
    // 2.7 version
    TableName table27 = TableName.valueOf("metricV2.7");
    desc = new HTableDescriptor(table27);
    desc.addCoprocessor(hBaseTableUtil.getIncrementHandlerClassForVersion().getName());
    hAdmin.createTable(desc);
    desc = hAdmin.getTableDescriptor(table27);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_7, util.getVersion(desc));
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableName(org.apache.hadoop.hbase.TableName) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Example 65 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class InMemoryStreamConsumerStateStoreFactory method dropAllInNamespace.

@Override
public synchronized void dropAllInNamespace(NamespaceId namespace) throws IOException {
    TableId tableId = StreamUtils.getStateStoreTableId(namespace);
    InMemoryTableAdmin admin = new InMemoryTableAdmin(DatasetContext.from(tableId.getNamespace()), tableId.getTableName(), cConf);
    admin.drop();
}
Also used : TableId(co.cask.cdap.data2.util.TableId) InMemoryTableAdmin(co.cask.cdap.data2.dataset2.lib.table.inmemory.InMemoryTableAdmin)

Aggregations

TableId (co.cask.cdap.data2.util.TableId)100 Test (org.junit.Test)49 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)40 HRegion (org.apache.hadoop.hbase.regionserver.HRegion)39 Put (org.apache.hadoop.hbase.client.Put)34 NamespaceId (co.cask.cdap.proto.id.NamespaceId)26 Cell (org.apache.hadoop.hbase.Cell)24 Scan (org.apache.hadoop.hbase.client.Scan)24 HTableDescriptorBuilder (co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder)18 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)17 HBaseTableUtil (co.cask.cdap.data2.util.hbase.HBaseTableUtil)15 HBaseTableUtilFactory (co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory)15 IOException (java.io.IOException)13 HTable (org.apache.hadoop.hbase.client.HTable)11 Delete (org.apache.hadoop.hbase.client.Delete)9 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)9 HBaseDDLExecutor (co.cask.cdap.spi.hbase.HBaseDDLExecutor)8 Path (org.apache.hadoop.fs.Path)8 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)8 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)8