Beispiel #1
0
 @AfterClass
 public static void afterClass() throws Exception {
   hBaseTableUtil.deleteAllInNamespace(testHBase.getHBaseAdmin(), NAMESPACE1);
   hBaseTableUtil.deleteAllInNamespace(testHBase.getHBaseAdmin(), NAMESPACE2);
   hBaseTableUtil.deleteNamespaceIfExists(testHBase.getHBaseAdmin(), NAMESPACE1);
   hBaseTableUtil.deleteNamespaceIfExists(testHBase.getHBaseAdmin(), NAMESPACE2);
   testHBase.stopHBase();
 }
Beispiel #2
0
 @BeforeClass
 public static void beforeClass() throws Exception {
   testHBase = new HBaseTestFactory().get();
   testHBase.startHBase();
   cConf = CConfiguration.create();
   hBaseTableUtil = new HBaseTableUtilFactory(cConf).get();
   // TODO: CDAP-1634 - Explore a way to not have every HBase test class do this.
   hBaseTableUtil.createNamespaceIfNotExists(testHBase.getHBaseAdmin(), NAMESPACE1);
   hBaseTableUtil.createNamespaceIfNotExists(testHBase.getHBaseAdmin(), NAMESPACE2);
 }
Beispiel #3
0
  @Test
  public void testColumnFamily() throws Exception {
    DatasetProperties props =
        DatasetProperties.builder().add(Table.PROPERTY_COLUMN_FAMILY, "t").build();
    HBaseTableDefinition tableDefinition = new HBaseTableDefinition("foo");
    String tableName = "testcf";
    DatasetSpecification spec = tableDefinition.configure(tableName, props);

    DatasetAdmin admin =
        new HBaseTableAdmin(
            CONTEXT1,
            spec,
            testHBase.getConfiguration(),
            hBaseTableUtil,
            CConfiguration.create(),
            new LocalLocationFactory(tmpFolder.newFolder()));
    admin.create();
    final HBaseTable table =
        new HBaseTable(CONTEXT1, spec, cConf, testHBase.getConfiguration(), hBaseTableUtil);

    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 HBaseTable table2 =
        new HBaseTable(CONTEXT1, spec, cConf, testHBase.getConfiguration(), hBaseTableUtil);
    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
    HTableDescriptor htd =
        hBaseTableUtil.getHTableDescriptor(
            testHBase.getHBaseAdmin(), TableId.from(CONTEXT1.getNamespaceId(), tableName));
    HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
    Assert.assertNotNull(hcd);
    Assert.assertEquals("t", hcd.getNameAsString());
  }
Beispiel #4
0
  @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();

    HBaseAdmin hBaseAdmin = testHBase.getHBaseAdmin();
    try {
      List<HRegionInfo> regions =
          hBaseTableUtil.getTableRegions(
              hBaseAdmin, TableId.from(NAMESPACE1.getId(), presplittedTable));
      // 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());
    } finally {
      hBaseAdmin.close();
    }
  }
Beispiel #5
0
  @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 = TableId.from(NAMESPACE1.getId(), disableTableName);
    TableId enabledTableId = TableId.from(NAMESPACE1.getId(), enabledTableName);
    HBaseTableAdmin disabledAdmin =
        getTableAdmin(CONTEXT1, disableTableName, DatasetProperties.EMPTY);
    disabledAdmin.create();
    HBaseAdmin admin = testHBase.getHBaseAdmin();

    DatasetProperties props =
        DatasetProperties.builder().add(Table.PROPERTY_READLESS_INCREMENT, "true").build();
    HBaseTableAdmin enabledAdmin = getTableAdmin(CONTEXT1, enabledTableName, props);
    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, ConflictDetection.COLUMN);
      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();
      testHBase.forEachRegion(
          enabledTableNameBytes,
          new Function<HRegion, Object>() {
            @Nullable
            @Override
            public Object apply(@Nullable HRegion hRegion) {
              Scan scan = new Scan();
              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();
    }
  }