public static ResultScanner scan(String tableName, String family, List<String> columns, List<String> patterns) throws IOException {
Connection connection = connHolder.get();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//过滤器列表
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
for (int i = 0; i <columns.size(); i++) {
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(columns.get(i)),
CompareOperator.EQUAL, Bytes.toBytes(patterns.get(i)));
//设置如果没有符合的就不返回所有列信息
singleColumnValueFilter.setFilterIfMissing(true);
list.addFilter(singleColumnValueFilter);
}
//设置过滤器
scan.setFilter(list);
ResultScanner results = table.getScanner(scan);
return results;
}
-
colums为多个条件所在的列
-
patterns为多个要查询的值
-
FilterList.Operator.MUST_PASS_ONE 的意思为只要符合其中一个条件就通过,等同于sql中的or
-
singleColumnValueFilter.setFilterIfMissing(true) true必设,不然会返回所有的信息