Java Code Examples for org.apache.hadoop.hbase.client.Admin#deleteTable()

The following examples show how to use org.apache.hadoop.hbase.client.Admin#deleteTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestMetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableExists() throws IOException {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  assertFalse(MetaTableAccessor.tableExists(connection, tableName));
  UTIL.createTable(tableName, HConstants.CATALOG_FAMILY);
  assertTrue(MetaTableAccessor.tableExists(connection, tableName));
  Admin admin = UTIL.getAdmin();
  admin.disableTable(tableName);
  admin.deleteTable(tableName);
  assertFalse(MetaTableAccessor.tableExists(connection, tableName));
  assertTrue(MetaTableAccessor.tableExists(connection, TableName.META_TABLE_NAME));
  UTIL.createTable(tableName, HConstants.CATALOG_FAMILY);
  assertTrue(MetaTableAccessor.tableExists(connection, tableName));
  admin.disableTable(tableName);
  admin.deleteTable(tableName);
  assertFalse(MetaTableAccessor.tableExists(connection, tableName));
}
 
Example 2
Source File: CubeHTableUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
/** create a HTable that has the same performance settings as normal cube table, for benchmark purpose */
public static void createBenchmarkHTable(TableName tableName, String cfName) throws IOException {
    Admin admin = HBaseConnection.get(KylinConfig.getInstanceFromEnv().getStorageUrl()).getAdmin();
    try {
        if (admin.tableExists(tableName)) {
            logger.info("disabling hbase table " + tableName);
            admin.disableTable(tableName);
            logger.info("deleting hbase table " + tableName);
            admin.deleteTable(tableName);
        }

        HTableDescriptor tableDesc = new HTableDescriptor(tableName);
        tableDesc.setValue(HTableDescriptor.SPLIT_POLICY, DisabledRegionSplitPolicy.class.getName());

        KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
        tableDesc.addFamily(createColumnFamily(kylinConfig, cfName, false));

        logger.info("creating hbase table " + tableName);
        admin.createTable(tableDesc, null);
        Preconditions.checkArgument(admin.isTableAvailable(tableName), "table " + tableName + " created, but is not available due to some reasons");
        logger.info("create hbase table " + tableName + " done.");
    } finally {
        IOUtils.closeQuietly(admin);
    }
}
 
Example 3
Source File: TestTableDescriptorModificationFromClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteColumn() throws IOException {
  Admin admin = TEST_UTIL.getAdmin();
  // Create a table with two families
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);

  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_0));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_1));
  admin.createTable(tableDescriptor);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the table descriptor
    verifyTableDescriptor(TABLE_NAME, FAMILY_0, FAMILY_1);

    // Modify the table removing one family and verify the descriptor
    admin.deleteColumnFamily(TABLE_NAME, FAMILY_1);
    verifyTableDescriptor(TABLE_NAME, FAMILY_0);
  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
 
Example 4
Source File: SpaceQuotaHelperForTests.java    From hbase with Apache License 2.0 6 votes vote down vote up
TableName createTableInNamespace(NamespaceDescriptor nd) throws Exception {
  final Admin admin = testUtil.getAdmin();
  final TableName tn = TableName.valueOf(nd.getName(),
      testName.getMethodName() + counter.getAndIncrement());

  // Delete the old table
  if (admin.tableExists(tn)) {
    admin.disableTable(tn);
    admin.deleteTable(tn);
  }

  // Create the table
  TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tn)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(F1)).build();

  admin.createTable(tableDesc);
  return tn;
}
 
Example 5
Source File: TestTableDescriptorModificationFromClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteSameColumnFamilyTwice() throws IOException {
  Admin admin = TEST_UTIL.getAdmin();
  // Create a table with two families
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);

  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_0));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_1));
  admin.createTable(tableDescriptor);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the table descriptor
    verifyTableDescriptor(TABLE_NAME, FAMILY_0, FAMILY_1);

    // Modify the table removing one family and verify the descriptor
    admin.deleteColumnFamily(TABLE_NAME, FAMILY_1);
    verifyTableDescriptor(TABLE_NAME, FAMILY_0);

    try {
      // Delete again - expect failure
      admin.deleteColumnFamily(TABLE_NAME, FAMILY_1);
      Assert.fail("Delete a non-exist column family should fail");
    } catch (Exception e) {
      // Expected.
    }
  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
 
Example 6
Source File: HBaseTables.java    From presto-hbase-connector with Apache License 2.0 5 votes vote down vote up
void dropTable(String schema, String tableName) {
    Admin admin = null;
    try {
        admin = hbaseClientManager.getAdmin();
        admin.disableTable(TableName.valueOf(schema + ":" + tableName));
        admin.deleteTable(TableName.valueOf(schema + ":" + tableName));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (admin != null) {
            hbaseClientManager.close(admin);
        }
    }
}
 
Example 7
Source File: TestTableDescriptorModificationFromClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifyNonExistingColumnFamily() throws IOException {
  Admin admin = TEST_UTIL.getAdmin();

  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor cfDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_1);
  int blockSize = cfDescriptor.getBlocksize();
  // Create a table with one families
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);

  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_0));
  admin.createTable(tableDescriptor);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the table descriptor
    verifyTableDescriptor(TABLE_NAME, FAMILY_0);

    int newBlockSize = 2 * blockSize;
    cfDescriptor.setBlocksize(newBlockSize);

    // Modify a column family that is not in the table.
    try {
      admin.modifyColumnFamily(TABLE_NAME, cfDescriptor);
      Assert.fail("Modify a non-exist column family should fail");
    } catch (InvalidFamilyOperationException e) {
      // Expected.
    }

  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
 
Example 8
Source File: TestSchemaResource.java    From hbase with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();

  for (String table : new String[] {TABLE1, TABLE2}) {
    TableName t = TableName.valueOf(table);
    if (admin.tableExists(t)) {
      admin.disableTable(t);
      admin.deleteTable(t);
    }
  }

  conf.set("hbase.rest.readonly", "false");
}
 
Example 9
Source File: TestMasterObserverPostCalls.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostDeleteNamespace() throws IOException {
  final Admin admin = UTIL.getAdmin();
  final String ns = "postdeletens";
  final TableName tn1 = TableName.valueOf(ns, "table1");

  admin.createNamespace(NamespaceDescriptor.create(ns).build());
  admin.createTable(TableDescriptorBuilder.newBuilder(tn1)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1")).build())
      .build());

  HMaster master = UTIL.getMiniHBaseCluster().getMaster();
  MasterObserverForTest observer = master.getMasterCoprocessorHost().findCoprocessor(
      MasterObserverForTest.class);
  int preCount = observer.postHookCalls.get();
  try {
    admin.deleteNamespace(ns);
    fail("Deleting a non-empty namespace should be disallowed");
  } catch (IOException e) {
    // Pass
  }
  int postCount = observer.postHookCalls.get();
  assertEquals("Expected no invocations of postDeleteNamespace when the operation fails",
      preCount, postCount);

  // Disable and delete the table so that we can delete the NS.
  admin.disableTable(tn1);
  admin.deleteTable(tn1);

  // Validate that the postDeletNS hook is invoked
  preCount = observer.postHookCalls.get();
  admin.deleteNamespace(ns);
  postCount = observer.postHookCalls.get();
  assertEquals("Expected 1 invocation of postDeleteNamespace", preCount + 1, postCount);
}
 
Example 10
Source File: ITAclTableMigrationToolTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
private void dropTestHTables() throws IOException {
    Configuration conf = HBaseConnection.getCurrentHBaseConfiguration();
    Admin hbaseAdmin = new HBaseAdmin(conf);
    if (hbaseAdmin.tableExists(aclTable)) {
        if (hbaseAdmin.isTableEnabled(aclTable))
            hbaseAdmin.disableTable(aclTable);
        hbaseAdmin.deleteTable(aclTable);
    }
    if (hbaseAdmin.tableExists(userTable)) {
        if (hbaseAdmin.isTableEnabled(userTable))
            hbaseAdmin.disableTable(userTable);
        hbaseAdmin.deleteTable(userTable);
    }
    hbaseAdmin.close();
}
 
Example 11
Source File: BigtableTargetIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static void dropTable() {
  Connection conn = BigtableConfiguration.connect(projectID, instanceID);
  try {
    Admin admin = conn.getAdmin();
    admin.disableTable(TableName.valueOf(tableName));
    admin.deleteTable(TableName.valueOf(tableName));
  } catch (Exception ex) {
    LOG.info("dropTable(): exception {} ", ex.toString());
  }
}
 
Example 12
Source File: HBaseCLI.java    From cloud-bigtable-examples with Apache License 2.0 5 votes vote down vote up
public void run(Connection connection, List<String> args) throws InvalidArgsException, IOException {
    if (args.size() < 1) {
        throw new InvalidArgsException(args);
    }
    String tableId = args.get(0);

    // Deletes the table based on the passed tableId in arguments.
    // We used the standard HBase Admin and HTableDescriptor classes.
    Admin admin = connection.getAdmin();
    admin.deleteTable(TableName.valueOf(tableId));
}
 
Example 13
Source File: TestFileArchiverNotifierImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnapshotSizePersistence() throws IOException {
  final Admin admin = TEST_UTIL.getAdmin();
  final TableName tn = TableName.valueOf(testName.getMethodName());
  if (admin.tableExists(tn)) {
    admin.disableTable(tn);
    admin.deleteTable(tn);
  }
  TableDescriptor desc = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(
      ColumnFamilyDescriptorBuilder.of(QuotaTableUtil.QUOTA_FAMILY_USAGE)).build();
  admin.createTable(desc);

  FileArchiverNotifierImpl notifier = new FileArchiverNotifierImpl(conn, conf, fs, tn);
  List<SnapshotWithSize> snapshotsWithSizes = new ArrayList<>();
  try (Table table = conn.getTable(tn)) {
    // Writing no values will result in no records written.
    verify(table, () -> {
      notifier.persistSnapshotSizes(table, snapshotsWithSizes);
      assertEquals(0, count(table));
    });

    verify(table, () -> {
      snapshotsWithSizes.add(new SnapshotWithSize("ss1", 1024L));
      snapshotsWithSizes.add(new SnapshotWithSize("ss2", 4096L));
      notifier.persistSnapshotSizes(table, snapshotsWithSizes);
      assertEquals(2, count(table));
      assertEquals(1024L, extractSnapshotSize(table, tn, "ss1"));
      assertEquals(4096L, extractSnapshotSize(table, tn, "ss2"));
    });
  }
}
 
Example 14
Source File: TestMasterObserverPostCalls.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostDeleteTable() throws IOException {
  final Admin admin = UTIL.getAdmin();
  final TableName tn = TableName.valueOf("postdeletetable");
  final TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1")).build()).build();

  HMaster master = UTIL.getMiniHBaseCluster().getMaster();
  MasterObserverForTest observer = master.getMasterCoprocessorHost().findCoprocessor(
      MasterObserverForTest.class);

  // Create the table and disable it
  admin.createTable(td);
  admin.disableTable(td.getTableName());

  // Validate that the post hook is called
  int preCount = observer.postHookCalls.get();
  admin.deleteTable(td.getTableName());
  int postCount = observer.postHookCalls.get();
  assertEquals("Expected 1 invocation of postDeleteTable", preCount + 1, postCount);

  // Then, validate that it's not called when the call fails
  preCount = observer.postHookCalls.get();
  try {
    admin.deleteTable(TableName.valueOf("missing"));
    fail("Deleting a missing table should fail");
  } catch (IOException e) {
    // Pass
  }
  postCount = observer.postHookCalls.get();
  assertEquals("Expected no invocations of postDeleteTable when the operation fails",
      preCount, postCount);
}
 
Example 15
Source File: TestMasterQuotasObserver.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void dropTable(Admin admin, TableName tn) throws  Exception {
  admin.disableTable(tn);
  admin.deleteTable(tn);
}
 
Example 16
Source File: TestRegionSizeUse.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Writes at least {@code sizeInBytes} bytes of data to HBase and returns the TableName used.
 *
 * @param sizeInBytes The amount of data to write in bytes.
 * @return The table the data was written to
 */
private TableName writeData(long sizeInBytes) throws IOException {
  final Connection conn = TEST_UTIL.getConnection();
  final Admin admin = TEST_UTIL.getAdmin();
  final TableName tn = TableName.valueOf(testName.getMethodName());

  // Delete the old table
  if (admin.tableExists(tn)) {
    admin.disableTable(tn);
    admin.deleteTable(tn);
  }

  // Create the table
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(tn);
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(F1)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);

  admin.createTable(tableDescriptorBuilder.build(), Bytes.toBytes("1"),
    Bytes.toBytes("9"), NUM_SPLITS);

  final Table table = conn.getTable(tn);
  try {
    List<Put> updates = new ArrayList<>();
    long bytesToWrite = sizeInBytes;
    long rowKeyId = 0L;
    final StringBuilder sb = new StringBuilder();
    final Random r = new Random();
    while (bytesToWrite > 0L) {
      sb.setLength(0);
      sb.append(Long.toString(rowKeyId));
      // Use the reverse counter as the rowKey to get even spread across all regions
      Put p = new Put(Bytes.toBytes(sb.reverse().toString()));
      byte[] value = new byte[SIZE_PER_VALUE];
      r.nextBytes(value);
      p.addColumn(Bytes.toBytes(F1), Bytes.toBytes("q1"), value);
      updates.add(p);

      // Batch 50K worth of updates
      if (updates.size() > 50) {
        table.put(updates);
        updates.clear();
      }

      // Just count the value size, ignore the size of rowkey + column
      bytesToWrite -= SIZE_PER_VALUE;
      rowKeyId++;
    }

    // Write the final batch
    if (!updates.isEmpty()) {
      table.put(updates);
    }

    return tn;
  } finally {
    table.close();
  }
}
 
Example 17
Source File: WALReplayWithIndexWritesAndCompressedWALIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
   * Test writing edits into an region, closing it, splitting logs, opening Region again. Verify
   * seqids.
   * @throws Exception on failure
   */
@Test
  public void testReplayEditsWrittenViaHRegion() throws Exception {
    final String tableNameStr = "testReplayEditsWrittenViaHRegion";
    final RegionInfo hri = RegionInfoBuilder.newBuilder(org.apache.hadoop.hbase.TableName.valueOf(tableNameStr)).setSplit(false).build();
    final Path basedir = FSUtils.getTableDir(hbaseRootDir, org.apache.hadoop.hbase.TableName.valueOf(tableNameStr));
    deleteDir(basedir);
    final TableDescriptor htd = createBasic3FamilyHTD(tableNameStr);
    
    //setup basic indexing for the table
    // enable indexing to a non-existant index table
    byte[] family = new byte[] { 'a' };
    ColumnGroup fam1 = new ColumnGroup(INDEX_TABLE_NAME);
    fam1.add(new CoveredColumn(family, CoveredColumn.ALL_QUALIFIERS));
    CoveredColumnIndexSpecifierBuilder builder = new CoveredColumnIndexSpecifierBuilder();
    builder.addIndexGroup(fam1);
    builder.build(htd);
    WALFactory walFactory = new WALFactory(this.conf, "localhost,1234");

    WAL wal = createWAL(this.conf, walFactory);
    // create the region + its WAL
    HRegion region0 = HRegion.createHRegion(hri, hbaseRootDir, this.conf, htd, wal); // FIXME: Uses private type
    region0.close();
    region0.getWAL().close();

    HRegionServer mockRS = Mockito.mock(HRegionServer.class);
    // mock out some of the internals of the RSS, so we can run CPs
    when(mockRS.getWAL(null)).thenReturn(wal);
    RegionServerAccounting rsa = Mockito.mock(RegionServerAccounting.class);
    when(mockRS.getRegionServerAccounting()).thenReturn(rsa);
    ServerName mockServerName = Mockito.mock(ServerName.class);
    when(mockServerName.getServerName()).thenReturn(tableNameStr + ",1234");
    when(mockRS.getServerName()).thenReturn(mockServerName);
    HRegion region = spy(new HRegion(basedir, wal, this.fs, this.conf, hri, htd, mockRS));
    region.initialize();


    //make an attempted write to the primary that should also be indexed
    byte[] rowkey = Bytes.toBytes("indexed_row_key");
    Put p = new Put(rowkey);
    p.addColumn(family, Bytes.toBytes("qual"), Bytes.toBytes("value"));
    region.put(p);

    // we should then see the server go down
    Mockito.verify(mockRS, Mockito.times(1)).abort(Mockito.anyString(),
      Mockito.any(Exception.class));

    // then create the index table so we are successful on WAL replay
    TestIndexManagementUtil.createIndexTable(UTIL.getAdmin(), INDEX_TABLE_NAME);

    // run the WAL split and setup the region
    runWALSplit(this.conf, walFactory);
    WAL wal2 = createWAL(this.conf, walFactory);
    HRegion region1 = new HRegion(basedir, wal2, this.fs, this.conf, hri, htd, mockRS);

    // initialize the region - this should replay the WALEdits from the WAL
    region1.initialize();
    org.apache.hadoop.hbase.client.Connection hbaseConn =
            ConnectionFactory.createConnection(UTIL.getConfiguration());

    // now check to ensure that we wrote to the index table
    Table index = hbaseConn.getTable(org.apache.hadoop.hbase.TableName.valueOf(INDEX_TABLE_NAME));
    int indexSize = getKeyValueCount(index);
    assertEquals("Index wasn't propertly updated from WAL replay!", 1, indexSize);
    Get g = new Get(rowkey);
    final Result result = region1.get(g);
    assertEquals("Primary region wasn't updated from WAL replay!", 1, result.size());

    // cleanup the index table
    Admin admin = UTIL.getAdmin();
    admin.disableTable(TableName.valueOf(INDEX_TABLE_NAME));
    admin.deleteTable(TableName.valueOf(INDEX_TABLE_NAME));
    admin.close();
  }
 
Example 18
Source File: TestNamespaceReplicationWithBulkLoadedData.java    From hbase with Apache License 2.0 4 votes vote down vote up
@After
@Override
public void tearDownBase() throws Exception {
  super.tearDownBase();
  TableDescriptor table1 = TableDescriptorBuilder.newBuilder(NS1_TABLE)
      .setColumnFamily(
          ColumnFamilyDescriptorBuilder.newBuilder(famName)
              .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(noRepfamName)).build();

  TableDescriptor table2 = TableDescriptorBuilder.newBuilder(NS2_TABLE)
      .setColumnFamily(
          ColumnFamilyDescriptorBuilder.newBuilder(famName)
              .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(noRepfamName)).build();
  Admin admin1 = UTIL1.getAdmin();
  admin1.disableTable(table1.getTableName());
  admin1.deleteTable(table1.getTableName());
  admin1.disableTable(table2.getTableName());
  admin1.deleteTable(table2.getTableName());
  admin1.deleteNamespace(NS1);
  admin1.deleteNamespace(NS2);

  Admin admin2 = UTIL2.getAdmin();
  admin2.disableTable(table1.getTableName());
  admin2.deleteTable(table1.getTableName());
  admin2.disableTable(table2.getTableName());
  admin2.deleteTable(table2.getTableName());
  admin2.deleteNamespace(NS1);
  admin2.deleteNamespace(NS2);

  Admin admin3 = UTIL3.getAdmin();
  admin3.disableTable(table1.getTableName());
  admin3.deleteTable(table1.getTableName());
  admin3.disableTable(table2.getTableName());
  admin3.deleteTable(table2.getTableName());
  admin3.deleteNamespace(NS1);
  admin3.deleteNamespace(NS2);

  Admin admin4 = UTIL4.getAdmin();
  admin4.disableTable(table1.getTableName());
  admin4.deleteTable(table1.getTableName());
  admin4.disableTable(table2.getTableName());
  admin4.deleteTable(table2.getTableName());
  admin4.deleteNamespace(NS1);
  admin4.deleteNamespace(NS2);
  UTIL1.getAdmin().removeReplicationPeer(PEER4_NS);
  UTIL1.getAdmin().removeReplicationPeer(PEER4_NS_TABLE);
}
 
Example 19
Source File: TestPerColumnFamilyFlush.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  int numRegions = Integer.parseInt(args[0]);
  long numRows = Long.parseLong(args[1]);

  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLENAME);
  tableDescriptor.setMaxFileSize(10L * 1024 * 1024 * 1024);
  tableDescriptor.setValue(HTableDescriptor.SPLIT_POLICY,
    ConstantSizeRegionSplitPolicy.class.getName());
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY1));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY2));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY3));

  Configuration conf = HBaseConfiguration.create();
  Connection conn = ConnectionFactory.createConnection(conf);
  Admin admin = conn.getAdmin();
  if (admin.tableExists(TABLENAME)) {
    admin.disableTable(TABLENAME);
    admin.deleteTable(TABLENAME);
  }
  if (numRegions >= 3) {
    byte[] startKey = new byte[16];
    byte[] endKey = new byte[16];
    Arrays.fill(endKey, (byte) 0xFF);
    admin.createTable(tableDescriptor, startKey, endKey, numRegions);
  } else {
    admin.createTable(tableDescriptor);
  }
  admin.close();

  Table table = conn.getTable(TABLENAME);
  byte[] qf = Bytes.toBytes("qf");
  Random rand = new Random();
  byte[] value1 = new byte[16];
  byte[] value2 = new byte[256];
  byte[] value3 = new byte[4096];
  for (long i = 0; i < numRows; i++) {
    Put put = new Put(Hashing.md5().hashLong(i).asBytes());
    rand.setSeed(i);
    rand.nextBytes(value1);
    rand.nextBytes(value2);
    rand.nextBytes(value3);
    put.addColumn(FAMILY1, qf, value1);
    put.addColumn(FAMILY2, qf, value2);
    put.addColumn(FAMILY3, qf, value3);
    table.put(put);
    if (i % 10000 == 0) {
      LOG.info(i + " rows put");
    }
  }
  table.close();
  conn.close();
}
 
Example 20
Source File: PerformanceEvaluation.java    From hbase with Apache License 2.0 4 votes vote down vote up
static boolean checkTable(Admin admin, TestOptions opts) throws IOException {
  TableName tableName = TableName.valueOf(opts.tableName);
  boolean needsDelete = false, exists = admin.tableExists(tableName);
  boolean isReadCmd = opts.cmdName.toLowerCase(Locale.ROOT).contains("read")
    || opts.cmdName.toLowerCase(Locale.ROOT).contains("scan");
  if (!exists && isReadCmd) {
    throw new IllegalStateException(
      "Must specify an existing table for read commands. Run a write command first.");
  }
  TableDescriptor desc =
    exists ? admin.getDescriptor(TableName.valueOf(opts.tableName)) : null;
  byte[][] splits = getSplits(opts);

  // recreate the table when user has requested presplit or when existing
  // {RegionSplitPolicy,replica count} does not match requested, or when the
  // number of column families does not match requested.
  if ((exists && opts.presplitRegions != DEFAULT_OPTS.presplitRegions)
    || (!isReadCmd && desc != null &&
        !StringUtils.equals(desc.getRegionSplitPolicyClassName(), opts.splitPolicy))
    || (!isReadCmd && desc != null && desc.getRegionReplication() != opts.replicas)
    || (desc != null && desc.getColumnFamilyCount() != opts.families)) {
    needsDelete = true;
    // wait, why did it delete my table?!?
    LOG.debug(MoreObjects.toStringHelper("needsDelete")
      .add("needsDelete", needsDelete)
      .add("isReadCmd", isReadCmd)
      .add("exists", exists)
      .add("desc", desc)
      .add("presplit", opts.presplitRegions)
      .add("splitPolicy", opts.splitPolicy)
      .add("replicas", opts.replicas)
      .add("families", opts.families)
      .toString());
  }

  // remove an existing table
  if (needsDelete) {
    if (admin.isTableEnabled(tableName)) {
      admin.disableTable(tableName);
    }
    admin.deleteTable(tableName);
  }

  // table creation is necessary
  if (!exists || needsDelete) {
    desc = getTableDescriptor(opts);
    if (splits != null) {
      if (LOG.isDebugEnabled()) {
        for (int i = 0; i < splits.length; i++) {
          LOG.debug(" split " + i + ": " + Bytes.toStringBinary(splits[i]));
        }
      }
    }
    if (splits != null) {
      admin.createTable(desc, splits);
    } else {
      admin.createTable(desc);
    }
    LOG.info("Table " + desc + " created");
  }
  return admin.tableExists(tableName);
}