文档首页 > > 开发指南> HBase应用开发> 开发程序> 添加二级索引

添加二级索引

分享
更新时间: 2020/01/11 GMT+08:00

前提条件

此功能在MRS1.7及更高版本才支持。

功能介绍

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

根据用户是否希望在添加索引操作期间构建索引数据,有两种不同的方法可将索引添加到表中:

  • addIndicesWithData()
  • addIndices()

代码样例

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

addIndices(): 将索引添加到没有数据的表中

  public void addIndicesExample() {
    LOG.info("Entering Adding a Hindex.");
    // Create index instance
    TableIndices tableIndices = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
    //MRS 2.0.0及其以后版本推荐使用ColumnFamilyDescriptorBuilder替换HColumnDescriptor来构造添加列操作。如下,
    //spec.addIndexColumn(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(),"name", 
    //ValueType.STRING, null);
    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.");
  }

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

addIndicesWithData():将索引添加到具有大量预先存在数据的表中

  public void addIndicesExampleWithData() {
    LOG.info("Entering Adding a Hindex With Data.");
    // Create index instance
    TableIndices tableIndices = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
    //MRS 2.0.0及其以后版本推荐使用ColumnFamilyDescriptorBuilder替换HColumnDescriptor来构造添加列操作。如下,
    //spec.addIndexColumn(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(),"age", 
    //ValueType.STRING, null);
    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.");
  }
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问