Hbase的api中可以通过filter来实现like查询,详情如下:
对行key进行like查询:
private void test() throws Exception {
Configuration conf = getCfg();
Scan scan = new Scan();
RegexStringComparator comp = new RegexStringComparator("(##test)");
RowFilter filter = new RowFilter(CompareOp.EQUAL, comp);
scan.setFilter(filter);
scan.setCaching(200);
scan.setCacheBlocks(false);
HTable hTable = new HTable(conf, "Test");
ResultScanner scanner = hTable.getScanner(scan);
byte[] bytes = Bytes.toBytes("T");
for (Result result : scanner) {
String all = Bytes.toString(result.getValue(bytes, bytes));
System.out.println(all);
}
}
主要借助于RegexStringComparator
对列值进行like查询:
private void test() throws Exception {
Configuration conf = getCfg();
Scan scan = new Scan();
RegexStringComparator comp = new RegexStringComparator("(##test)");
byte[] bytes = Bytes.toBytes("T");
Filter filter = new SingleColumnValueFilter(bytes, bytes, CompareOp.EQUAL, comp);
scan.setFilter(filter);
scan.setCaching(200);
scan.setCacheBlocks(false);
HTable hTable = new HTable(conf, "Test");
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
String all = Bytes.toString(result.getValue(bytes , bytes ));
System.out.println(all);
}
}
仍然是借助于RegexStringComparator
本文介绍了如何使用HBase API中的filter实现类似SQL的like查询功能,包括对行键及列值的模糊匹配方法,并提供了具体的Java代码示例。

6465

被折叠的 条评论
为什么被折叠?



