云服务器内容精选

  • 功能简介 通过调用“org.apache.hadoop.hbase.hindex.global.GlobalIndexAdmin”中的方法进行HBase全局二级索引的管理,该类中addIndices用于创建全局二级索引。 全局二级索引的创建需要指定索引列、覆盖列(可选)、索引表预分区(可选,建议指定)。 在已有存量数据的表上创建全局二级索引,需要创建索引预分区,防止索引表出现热点,索引表数据的rowkey由索引列构成,并且包含分隔符,格式为“\x01索引值\x00”,因此预分区需要指定成对应格式,例如,当使用id列和age列作为索引列时,两个列均为整数,使用id列完成预分区,可以指定索引表预分区点为: \x010,\x011,\x012....
  • 代码样例 以下代码片段在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."); }
  • 代码样例 以下代码片段在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."); }