Querying a List of Secondary Indexes

Prerequisites

This function is available only in MRS 1.7 or later version.

Function Description

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

HIndexAdmin provides the following API for listing indexes in a specified table:

  • listIndices (): This API can be used to list all indexes of a specified table.

Sample Code

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

  public void listIndicesIntable() {
    LOG.info("Entering Listing Hindex.");
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // Retreive the list of indices and print it
      List<Pair<HIndexSpecification, IndexState>> indicesList = iAdmin.listIndices(tableName);
      LOG.info("indicesList:" + indicesList);
      LOG.info("Successfully listed indices for table " + tableName + ".");
    } catch (IOException e) {
      LOG.error("Failed to list indices for table " + tableName + "." + e);
    } 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 Listing Hindex.");
  }