Updated on 2022-09-14 GMT+08:00

Enabling/Disabling a 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 enabling/disabling an existing index.

The HIndexAdmin provides the following APIs based on whether you want to enable or disable a table.

  • disableIndices ()
  • enableIndices ()

Sample Code

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

enableIndices (): Enable a specified index (the index status changes from INACTIVE to ACTIVE). This API can also be used to scan indexes.

  public void enableIndicesExample() {
    LOG.info("Entering Enabling 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);
      // Disable the specified indices
      iAdmin.enableIndices(tableName, indexNameList);
      // Alternately, disable the specified indices
      // iAdmin.disableIndices(tableName, indexNameList)
      LOG.info("Successfully enable indices " + indexNameList + " of the table " + tableName);
    } catch (IOException e) {
      LOG.error("Failed to enable indices " + indexNameList + " of 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 Enabling a Hindex.");
  }

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

disableIndices (): Disable a specified index (the index status changes from ACTIVE to INACTIVE). Therefore, index scanning becomes unavailable.

  public void disableIndicesExample() {
    LOG.info("Entering Disabling a Hindex.");
    List<String> indexNameList = new ArrayList<~>();
    indexNameList.add(indexNameToAdd);
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // Disable the specified indices
      iAdmin.disableIndices(tableName, indexNameList);
      // Alternately, enable the specified indices
      // iAdmin.enableIndices(tableName, indexNameList);
      LOG.info("Successfully disabled indices " + indexNameList + " of the table " + tableName);
    } catch (IOException e) {
      LOG.error("Failed to disable indices " + indexNameList + " of 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 Disabling a Hindex.");
  }