更新时间:2024-11-25 GMT+08:00
分享

删除HBase二级索引

功能介绍

您可以使用org.apache.hadoop.hbase.hindex.client.HIndexAdmin中提供的方法来管理HIndexes。 该类提供了从表中删除现有索引的方法。

根据用户是否希望删除索引数据以及索引删除操作,有两种不同的API可将索引删除到表中:

  • dropIndices()
  • dropIndicesWithData()

代码样例

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

dropIndices():从指定的表中删除指定的索引,但索引数据不会被删除。

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

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

dropIndicesWithData():从指定的表中删除指定的索引,并从用户表中删除与这些索引对应的所有索引数据。

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

相关文档