Updated on 2022-11-04 GMT+08:00

Deleting a Table

Function Description

HBase allows you to delete a table using the deleteTable method of org.apache.hadoop.hbase.client.Admin.

Sample Code

The following code snippets are in the dropTable method in the HBaseExample class of the com.huawei.bigdata.hbase.examples packet.

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);//Note [1]

            // Delete table.
            admin.deleteTable(tableName);
        }
        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

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