HBase总结--附加过滤器、FilterList

一、介绍

本节介绍HBase提供的最后两种过滤器,并且也介绍多个过滤器配合使用的方法。

二、详解

1、附加过滤器

(1)跳转过滤器:SkipFilter(Filter filter)

该过滤器的参数为一个过滤器。该过滤器的作用为:当参数中的过滤器过滤一个某一个KeyValue对象时,则跳转过滤器会将整行的数据进行过滤。

  1. public void example(String tableName)
  2. {
  3. Configuration conf=init();
  4. try {
  5. HTable table= new HTable(conf, tableName);
  6. //创建过滤器
  7. ValueFilter filter= new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes( "row-10")));
  8. SkipFilter skipFilter= new SkipFilter(filter);
  9. Scan scan= new Scan();
  10. scan.setFilter(filter);
  11. //
  12. ResultScanner rs=table.getScanner(scan);
  13. Result result= null;
  14. while((result=rs.next())!= null)
  15. {
  16. KeyValue[] kvs=result.raw();
  17. for(KeyValue kv:kvs)
  18. {
  19. System.out.println(kv.toString());
  20. }
  21. }
  22. //释放资源
  23. rs.close();
  24. table.close();
  25. } catch (Exception e) {
  26. // TODO: handle exception
  27. }
  28. }

(2)全匹配过滤器:WhileMatchFilter(Filter filter)

该过滤器需要填入一个过滤器作为参数。

该过滤器的作用于跳转过滤器相似,不过该过滤器当遇到第一个过滤整行数据的时候则会停止扫描过程。这样即使后面有数据符合条件也不会被传送到客户端。

  1. public void example(String tableName)
  2. {
  3. Configuration conf=init();
  4. try {
  5. HTable table= new HTable(conf, tableName);
  6. //创建过滤器
  7. ValueFilter filter= new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes( "row-10")));
  8. WhileMatchFilter filter= new WhileMatchFilter(filter);
  9. Scan scan= new Scan();
  10. scan.setFilter(filter);
  11. //
  12. ResultScanner rs=table.getScanner(scan);
  13. Result result= null;
  14. while((result=rs.next())!= null)
  15. {
  16. KeyValue[] kvs=result.raw();
  17. for(KeyValue kv:kvs)
  18. {
  19. System.out.println(kv.toString());
  20. }
  21. }
  22. //释放资源
  23. rs.close();
  24. table.close();
  25. } catch (Exception e) {
  26. // TODO: handle exception
  27. }
  28. }

2、FilterList:过滤器容器

FilterList的作用就是为了多个filter配合使用,因为在scan中,只能设置一个filter,因此在需要使用多个filter对象时,就需要使用FilterList容器将所有的filter装入,然后传入scan对象中。在Filterlist对象中,提供了多个filter的过滤关系:

(1)MUST_PASS_ALL :一行数据必须通过所有的过滤器才能被返回客户端

(2)MUST_PASS_ONE:一行数据中只要通过一个过滤器即可返回给客户端

在初始化FilterList对象时,可以进行模式的设置,FilterList对象的构造函数有三个:

FilterList(List<Filter> rowFilters)

FilterList(Operator operator)

FilterList(Operator operator,List<Filter> rowFilters)

  1. public void mutilRowFilter(String tableName,CompareOp[] compareOps,ByteArrayComparable[] comparables)
  2. {
  3. Configuration conf=init();
  4. try {
  5. //创建表连接
  6. HTable table= new HTable(conf, tableName);
  7. //创建scan
  8. Scan scan= new Scan();
  9. int length=compareOps.length;
  10. FilterList filterList= new FilterList(FilterList.Operator.MUST_PASS_ALL);
  11. for( int i= 0;i<length;i++)
  12. {
  13. RowFilter filter= new RowFilter(compareOps[i], comparables[i]);
  14. filterList.addFilter(filter);
  15. }
  16. scan.setFilter(filterList);
  17. //执行返回结果
  18. ResultScanner rs=table.getScanner(scan);
  19. Result result= null;
  20. while((result=rs.next())!= null)
  21. {
  22. KeyValue[] kvs=result.raw();
  23. for(KeyValue kv:kvs)
  24. {
  25. System.out.println(kv.toString());
  26. }
  27. }
  28. rs.close();
  29. table.close();
  30. } catch (Exception e) {
  31. // TODO: handle exception
  32. e.printStackTrace();
  33. }

猜你喜欢

转载自blog.csdn.net/qq_39736482/article/details/80881324
今日推荐