需求:参数是多个没有顺序的rowKey,在某张表中批量查询。一个一个rowKey查询的话,效率太低。

实现:需要在scan中添加filter。filter中添加多个rowKey,对需要查询的rowKey进行限制。x代表rowKey。

val rowKeyFilter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(x)))

  RowFilter用于过滤row key

Operator

Description

LESS

小于

LESS_OR_EQUAL

小于等于

EQUAL

等于

NOT_EQUAL

不等于

GREATER_OR_EQUAL

大于等于

GREATER

大于

NO_OP

排除所有

 

Comparator

Description

BinaryComparator

使用Bytes.compareTo()比较

BinaryPrefixComparator

和BinaryComparator差不多,从前面开始比较

NullComparator

Does not compare against an actual value but whether a given one is null, or not  null.

BitComparator

Performs a bitwise comparison, providing a BitwiseOp class with OR, and XOR

RegexStringComparator

正则表达式

SubstringComparator

把数据当成字符串,用contains()来判断

在这里使用EQUAL,BinaryComparator。

多个rowKey,rowKeyList是个rowKey的集合

//存放rowKeyFilter的filter
var filters = new util.ArrayList[Filter]
rowKeyList.foreach(x=>{
val rowKeyFilter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(x)))
      filters.add(rowKeyFilter)
    })

  然后将filters添加到 FilterList中

val filterList: FilterList = new FilterList(FilterList.Operator.MUST_PASS_ONE,filters)

    scan.setFilter(filterList)
FilterList.Operator.MUST_PASS_ALL --> 取交集 相当一and操作
    FilterList.Operator.MUST_PASS_ONE --> 取并集 相当于or 操作