Updated on 2024-04-02 GMT+08:00

Deleting a Table

Function

Delete a table using the deleteTable method of org.apache.hadoop.hbase.client.Admin.

Example Code

The following code snippet belongs to the dropTable method in the HBaseSample class of the com.huawei.bigdata.hbase.examples package.

public void dropTable() {
    LOG.info("Entering dropTable.");
    Admin admin = null;
    try {
      admin = conn.getAdmin();
      if (admin.tableExists(tableName)) {
        // Disable the table before deleting it.
        admin.disableTable(tableName);
        // Delete table.
        admin.deleteTable(tableName);// Note[1]
      }
      LOG.info("Drop table successfully.");
    } catch (IOException e) {
      LOG.error("Drop table failed " ,e);
    } finally {
      if (admin != null) {
        try {
          // Close the Admin object.
          admin.close();
        } catch (IOException e) {
          LOG.error("Close admin failed " ,e);
        }
      }
    }
    LOG.info("Exiting dropTable.");
  }

Precautions

A table can be deleted only when the table is disabled. Therefore, deleteTable is used together with disableTable, enableTable, tableExists, isTableEnabled, and isTableDisabled.