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

Adding 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 adding an index to an existing table.

You can add an index to a table by using either of the following methods based on whether you want to build index data when adding an index:

  • addIndicesWithData()
  • addIndices()

Sample Code

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

addIndices (): Add an index to a table without data.

  public void addIndicesExample() {
    LOG.info("Entering Adding a Hindex.");
    // Create index instance
    TableIndices tableIndices = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
    spec.addIndexColumn(new HColumnDescriptor("info"), "name", ValueType.STRING);
    tableIndices.addIndex(spec);
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // add index to the table
      iAdmin.addIndices(tableName, tableIndices);
      // Alternately, add the specified indices with data
      // iAdmin.addIndicesWithData(tableName, tableIndices);
      LOG.info("Successfully added indices to the table " + tableName);
    } catch (IOException e) {
      LOG.error("Add Indices failed 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 Adding a Hindex.");
  }

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

addIndicesWithData (): Add an index to a table with a large amount of data.

  public void addIndicesExampleWithData() {
    LOG.info("Entering Adding a Hindex With Data.");
    // Create index instance
    TableIndices tableIndices = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
    spec.addIndexColumn(new HColumnDescriptor("info"), "age", ValueType.STRING);
    tableIndices.addIndex(spec);
    Admin admin = null;
    HIndexAdmin iAdmin = null;
    try {
      admin = conn.getAdmin();
      iAdmin = HIndexClient.newHIndexAdmin(admin);
      // add index to the table
      iAdmin.addIndicesWithData(tableName, tableIndices);
      // Alternately, add the specified indices with data
      // iAdmin.addIndicesWithData(tableName, tableIndices);
      LOG.info("Successfully added indices to the table " + tableName);
    } catch (IOException e) {
      LOG.error("Add Indices failed 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 Adding a Hindex With Data.");
  }