Updated on 2024-08-16 GMT+08:00

Deleting an HBase Secondary Index

Function Description

You can use the methods provided by org.apache.hadoop.hbase.hindex.client.HIndexAdmin to manage HIndexes. This class provides methods of deleting all indexes from a table.

Based on whether the user wants to delete index data and indexes, this class provide two APIs to delete indexes.

  • dropIndices()
  • dropIndicesWithData()

Sample Code

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

dropIndices (): Delete the specified index from the specified table, excluding index data.

  public void dropIndicesExample() {
    LOG.info("Entering Deleting a Hindex.");
    List<String> indexNameList = new ArrayList<String>();
    indexNameList.add(indexNameToAdd);
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // Drop the specified indices without dropping index data
      iAdmin.dropIndices(tableName, indexNameList);
      // Alternately, drop the specified indices with data
      // iAdmin.dropIndicesWithData(tableName, indexNameList);
      LOG.info("Successfully dropped indices " + indexNameList + " from the table " + tableName);
    } catch (IOException e) {
      LOG.error("Failed to drop indices " + indexNameList + " from the table " + tableName);
    } finally {
      if (iAdmin != null) {
        try {
          // Close the HIndexAdmin object.
          iAdmin.close();
        } catch (IOException e) {
          LOG.error("Failed to close HIndexAdmin ", e);
        }
      }
      if (admin != null) {
        try {
          // Close the Admin object.
          admin.close();
        } catch (IOException e) {
          LOG.error("Failed to close admin ", e);
        }
      }
    }
    LOG.info("Exiting Deleting a Hindex.");
  }

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

dropIndicesWithData (): Delete a specified index from a specified table, including all related index data, from the user table.

   public void dropIndicesExampleWithData() {
    LOG.info("Entering Deleting a Hindex With Data.");
    List<String> indexNameList = new ArrayList<String>();
    indexNameList.add(indexNameToAdd);
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // Drop the specified indices without dropping index data
      iAdmin.dropIndicesWithData(tableName, indexNameList);
      // Alternately, drop the specified indices with data
      // iAdmin.dropIndicesWithData(tableName, indexNameList);
      LOG.info("Successfully dropped indices " + indexNameList + " from the table " + tableName);
    } catch (IOException e) {
      LOG.error("Failed to drop indices " + indexNameList + " from the table " + tableName);
    } finally {
      if (iAdmin != null) {
        try {
          // Close the HIndexAdmin object.
          iAdmin.close();
        } catch (IOException e) {
          LOG.error("Failed to close HIndexAdmin ", e);
        }
      }
      if (admin != null) {
        try {
          // Close the Admin object.
          admin.close();
        } catch (IOException e) {
          LOG.error("Failed to close admin ", e);
        }
      }
    }
    LOG.info("Exiting Deleting a Hindex With Data.");
  }