文档简介:
使用过滤器Filter
功能简介
HBase Filter主要在Scan和Get过程中进行数据过滤,通过设置一些过滤条件来实现,如
设置RowKey、列名或者列值的过滤条件。
代码样例
以下代码片段在cn.chinatelecom.hbase.sample包的“HBaseSample”类的
sampleScanFilter方法中。
public static List<String> sampleScanFilter(Connection conn) {
List<String> list = new ArrayList<>();
TableName tableName = TableName.valueOf("hbase-sample");
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("user"), Bytes.toBytes("name"));
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("user"), Bytes.toBytes("name"), CompareOperator.EQUAL,
Bytes.toBytes("Zhang San"));
scan.setFilter(filter);
try (Table table = conn.getTable(tableName); ResultScanner rScanner = table.getScanner(scan)) {
for (Result result : rScanner) {
showData(list, result);
}
log.info("Scan filter data successfully");
} catch (IOException e) {
log.error("Scan filter data failed ", e);
}
return list;
}
private static void showData(List<String> list, Result result) {
for (Cell cell : result.rawCells()) {
String temp = Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell));
log.info(temp);
list.add(temp);
}
}
注意事项
当前二级索引不支持使用SubstringComparator类定义的对象作为Filter的比较器。
例如,如下示例中的用法当前不支持:
Scan scan = new Scan();
filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new SingleColumnValueFilter(Bytes
.toBytes(columnFamily), Bytes.toBytes(qualifier),
CompareOperator.EQUAL, new SubstringComparator(substring)));
scan.setFilter(filterList);