基于全局二级索引查询HBase表数据
功能简介
添加了全局二级索引的用户表,在使用索引条件进行查询时,可以转换为对索引表的范围查询,性能高于针对无二级索引用户表的数据查询。
代码样例
以下代码片段在com.huawei.bigdata.hbase.examples包的“GlobalSecondaryIndexSample”类中。
本样例用于查询指定id对应的id、age、name信息,并命中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."); }