您的位置:首页 > 其它

hbase中根据Rowkey后缀进行查询

2017-11-23 11:28 1331 查看
假如hbase表设计时,Rowkey设计为“time+uid”(这里不考虑hbase的Rowkey设计合理性,只是简单说明)。现在有一个需求,筛选出某一uid在一个时间段[time1,time2)的值,该怎么办呢?

如果是在命令行中进行查询,可以结合startrow,endrow,filter,(column),具体如下所示:

scan ‘tablename’,

{STARTROW=>’time1+uid’,ENDROW=>’time2+uid’,FILTER=>”RowFilter(=,’regexstring:.*uid’)”}

如果是在代码中进行查询,

Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "10.1.1.1");//需根据hbase配置进行设置
HBaseConfiguration hbaseConfig = new HBaseConfiguration(config);

Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(time1+uid));
scan.setStopRow(Bytes.toBytes(time2+uid));
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*"+uid));
scan.setFilter(filter);

ResultScanner rs = null;
HTable table = new HTable(hbaseConfig, Bytes.toBytes(tableName));
String rowkey = null;
rs = table.getScanner(scan);
for (Result r : rs) {
for (KeyValue kv : r.list()) {
rowkey = Bytes.toString(kv.getRow());
System.out.println(rowkey);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hbase filter 后缀查询