更新时间:2022-07-19 GMT+08:00

启用/禁用二级索引

功能介绍

您可以使用org.apache.hadoop.hbase.hindex.client.HIndexAdmin中提供的方法来管理HIndexes。 这个类提供了启用/禁用现有索引的方法。

根据用户是否想要启用/禁用表,HIndexAdmin提供以下API:

  • disableIndices ()
  • enableIndices ()

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HIndexExample”类的enableIndicesExample方法中

enableIndices ():启用指定的索引(索引状态将从INACTIVE变为ACTIVE状态),因此可用于扫描索引。

  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.");
  }

以下代码片段在com.huawei.bigdata.hbase.examples包的“HIndexExample”类的disableIndicesExample方法中

disableIndices (): 禁用指定的索引(索引状态将从ACTIVE更改为INACTIVE状态),因此对于索引扫描将变得无法使用

  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.");
  }