MAPREDUCE服务 MRS-基于全局二级索引查询HBase表数据:代码样例

时间:2024-11-01 09:29:14

代码样例

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

本样例用于查询指定id对应的idagename信息,并命中idx_id_age索引,由于查询结果被完全覆盖,且无需回原表查询,能够达到最优的查询性能。

/**
    * Scan data by secondary index.
    */
public void testScanDataByIndex() {
     LOG .info("Entering testScanDataByIndex.");

    Scan scan = new Scan();
    // Create a filter for indexed column.
    SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("id"),
        CompareOperator.EQUAL, Bytes.toBytes("3"));
    filter.setFilterIfMissing(true);
    scan.setFilter(filter);

    // Specify returned columns
    // If returned columns not included in index table, will query back user table,
    // it's not the fast way to get data, suggest to cover all needed columns.
    // If you want to confirm whether using index for scanning, please set hbase client log level to DEBUG.
    scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"));
    scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
    scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));

    LOG.info("Scan indexed data.");
    try (Table table = conn.getTable(tableName); ResultScanner scanner = table.getScanner(scan)) {
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                LOG.info("{}:{},{},{}", Bytes.toString(CellUtil.cloneRow(cell)),
                    Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)),
                    Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        LOG.info("Scan data by index successfully.");
    } catch (IOException e) {
        LOG.error("Scan data by index failed ", e);
    }
    LOG.info("Exiting testScanDataByIndex.");
}
support.huaweicloud.com/devg-lts-mrs/mrs_07_290084.html