HBase Filter 过滤器之RowFilter详解

缺乏、安全感 2022-09-12 01:53 338阅读 0赞

前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考。RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase Rowkey进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:HBase Filter 过滤器之比较器 Comparator 原理及源码学习

一。Java Api

头部代码

  1. public class RowFilterDemo {
  2. private static boolean isok = false;
  3. private static String tableName = "test";
  4. private static String[] cfs = new String[]{"f"};
  5. private static String[] data = new String[]{"row-ac:f:c1:v1", "row-ab:f:c2:v2", "row-bc:f:c3:v3", "row-abc:f:c4:v4"};
  6. public static void main(String[] args) throws IOException {
  7. MyBase myBase = new MyBase();
  8. Connection connection = myBase.createConnection();
  9. if (isok) {
  10. myBase.deleteTable(connection, tableName);
  11. myBase.createTable(connection, tableName, cfs);
  12. myBase.putRows(connection, tableName, data); // 造数据
  13. }
  14. Table table = connection.getTable(TableName.valueOf(tableName));
  15. Scan scan = new Scan();

中部代码
向右滑动滚动条可查看输出结果。

1. BinaryComparator 构造过滤器

  1. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac]
  2. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-bc]
  3. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-bc]
  4. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac, row-bc]
  5. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc]
  6. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-ac]

2. BinaryPrefixComparator 构造过滤器

  1. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]
  2. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
  3. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
  4. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac, row-bc]
  5. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // []
  6. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]

3. SubstringComparator 构造过滤器

  1. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("ab")); // [row-ab, row-abc]
  2. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("ab")); // [row-ac, row-bc]

4. RegexStringComparator 构造过滤器

  1. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("abc")); // [row-ab, row-ac, row-bc]
  2. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("abc")); // [row-abc]
  3. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("a")); // [row-ab, row-abc, row-ac]

5. NullComparator 构造过滤器

  1. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new NullComparator()); // []
  2. RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new NullComparator()); // [row-ab, row-abc, row-ac, row-bc]

尾部代码

  1. scan.setFilter(rowFilter);
  2. ResultScanner scanner = table.getScanner(scan);
  3. Iterator<Result> iterator = scanner.iterator();
  4. LinkedList<String> rowkeys = new LinkedList<>();
  5. while (iterator.hasNext()) {
  6. Result result = iterator.next();
  7. String rowkey = Bytes.toString(result.getRow());
  8. rowkeys.add(rowkey);
  9. }
  10. System.out.println(rowkeys);
  11. scanner.close();
  12. table.close();
  13. connection.close();
  14. }
  15. }

二。Shell Api

1. BinaryComparator 构造过滤器

方式一:

  1. hbase(main):006:0> scan 'test',{FILTER=>"RowFilter(=,'binary:row-ab')"}
  2. ROW COLUMN+CELL
  3. row-ab column=f:c2, timestamp=1588156704669, value=v2
  4. 1 row(s) in 0.0140 seconds

支持的比较运算符:= != > >= < <=,不再一一举例。

方式二:

  1. import org.apache.hadoop.hbase.filter.CompareFilter
  2. import org.apache.hadoop.hbase.filter.BinaryComparator
  3. import org.apache.hadoop.hbase.filter.RowFilter
  4. hbase(main):016:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('row-ab')))}
  5. ROW COLUMN+CELL
  6. row-ab column=f:c2, timestamp=1588156704669, value=v2
  7. 1 row(s) in 0.0310 seconds

支持的比较运算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,不再一一举例。

推荐使用方式一,更简洁方便。

2. BinaryPrefixComparator 构造过滤器

方式一:

  1. hbase(main):023:0> scan 'test',{FILTER=>"RowFilter(=,'binaryprefix:row-ab')"}
  2. ROW COLUMN+CELL
  3. row-ab column=f:c2, timestamp=1588156704669, value=v2
  4. row-abc column=f:c4, timestamp=1588156704669, value=v4
  5. 2 row(s) in 0.0360 seconds

方式二:

  1. import org.apache.hadoop.hbase.filter.CompareFilter
  2. import org.apache.hadoop.hbase.filter.BinaryPrefixComparator
  3. import org.apache.hadoop.hbase.filter.RowFilter
  4. hbase(main):027:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('row-ab')))}
  5. ROW COLUMN+CELL
  6. row-ab column=f:c2, timestamp=1588156704669, value=v2
  7. row-abc column=f:c4, timestamp=1588156704669, value=v4
  8. 2 row(s) in 0.0110 seconds

其它同上。

3. SubstringComparator 构造过滤器

方式一:

  1. hbase(main):001:0> scan 'test',{FILTER=>"RowFilter(=,'substring:row-ab')"}
  2. ROW COLUMN+CELL
  3. row-ab column=f:c2, timestamp=1588156704669, value=v2
  4. row-abc column=f:c4, timestamp=1588156704669, value=v4
  5. 2 row(s) in 0.3200 seconds

方式二:

  1. import org.apache.hadoop.hbase.filter.CompareFilter
  2. import org.apache.hadoop.hbase.filter.SubstringComparator
  3. import org.apache.hadoop.hbase.filter.RowFilter
  4. hbase(main):007:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('row-ab'))}
  5. ROW COLUMN+CELL
  6. row-ab column=f:c2, timestamp=1588156704669, value=v2
  7. row-abc column=f:c4, timestamp=1588156704669, value=v4
  8. 2 row(s) in 0.0230 seconds

区别于上的是这里直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。

4. RegexStringComparator 构造过滤器

  1. import org.apache.hadoop.hbase.filter.CompareFilter
  2. import org.apache.hadoop.hbase.filter.RegexStringComparator
  3. import org.apache.hadoop.hbase.filter.RowFilter
  4. hbase(main):007:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('row-ab'))}
  5. ROW COLUMN+CELL
  6. row-ab column=f:c2, timestamp=1588156704669, value=v2
  7. row-abc column=f:c4, timestamp=1588156704669, value=v4
  8. 2 row(s) in 0.0230 seconds

该比较器直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。若想使用第一种方式可以传入regexstring试一下,我的版本有点低暂时不支持,不再演示了。

注意这里的正则匹配指包含关系,对应底层find()方法。

此外,RowFilter 不支持使用LongComparator比较器,且BitComparator、NullComparator 比较器用之甚少,也不再介绍。

查看文章全部源代码请访以下GitHub地址:

  1. https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/RowFilterDemo.java

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaFilter过滤器

    简介     filter过滤器主要使用于前台向后台传递数据的过滤操作。通过Filter技术,开发人员可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截。简单