博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hbase 学习(二)各种filter
阅读量:5806 次
发布时间:2019-06-18

本文共 5315 字,大约阅读时间需要 17 分钟。

各种filter

今天的主题是Filter,hbase客户端查询的时候,自定义查询filter。

直接上例子吧,不多说别的了,第一个例子是RowFilter的。

Scan scan = new Scan();scan.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("col-0"));Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,                                               new BinaryComparator(Bytes.toBytes("row-22")));scan.setFilter(filter1);ResultScanner scanner1 = table.getScanner(scan);for (Result res : scanner1) {    System.out.println(res);}scanner1.close();Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,                                              new RegexStringComparator(".*-.5"));scan.setFilter(filter2);ResultScanner scanner2 = table.getScanner(scan);for (Result res : scanner2) {     System.out.println(res);}scanner2.close();Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,                                               new SubstringComparator("-5"));scan.setFilter(filter3);ResultScanner scanner3 = table.getScanner(scan);for (Result res : scanner3) {      System.out.println(res);}scanner3.close();

 

第二个例子是QualifierFilter的

Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,                                                  new BinaryComparator(Bytes.toBytes("col-2")));Scan scan = new Scan();scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {     System.out.println(result);}scanner.close();Get get = new Get(Bytes.toBytes("row-5"));get.setFilter(filter);Result result = table.get(get);

 

第三个例子是ValueFilter

Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,                                                new SubstringComparator(".4"));Scan scan = new Scan();scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {       for (KeyValue kv : result.raw()) {             System.out.println("KV: " + kv + ", Value: " +             Bytes.toString(kv.getValue()));       }}scanner.close();Get get = new Get(Bytes.toBytes("row-5"));get.setFilter(filter);Result result = table.get(get);for (KeyValue kv : result.raw()) {     System.out.println("KV: " + kv + ", Value: " +     Bytes.toString(kv.getValue()));}

 

第四个例子是PageFilter,分页的filter

Filter filter = new PageFilter(15);int totalRows = 0;byte[] lastRow = null;while (true) {    Scan scan = new Scan();    scan.setFilter(filter);    if (lastRow != null) {         byte[] startRow = Bytes.add(lastRow, POSTFIX);        System.out.println("start row: " +Bytes.toStringBinary(startRow));         scan.setStartRow(startRow);    }    ResultScanner scanner = table.getScanner(scan);      int localRows = 0;    Result result;    while ((result = scanner.next()) != null) {        System.out.println(localRows++ + ": " + result);         totalRows++;         lastRow = result.getRow();   }   scanner.close();   if (localRows == 0) break;}System.out.println("total rows: " + totalRows);

 

第五个例子是InclusiveStopFilter,它是什么意思呢,就是扫描到某个rowkey就停止,下面的例子是从row-3开始扫描,一直扫描到row-5结束。

Filter filter = newInclusiveStopFilter(Bytes.toBytes("row-5"));Scan scan = new Scan();scan.setStartRow(Bytes.toBytes("row-3"));scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);    for (Result result : scanner) {    System.out.println(result);}scanner.close();

 

第六个SingleColumnValueFilter,这个是我们最常用的filter,我觉得很好用。

SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("colfam1"),Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL,new SubstringComparator("val-5"));filter.setFilterIfMissing(true);Scan scan = new Scan();scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {  for (KeyValue kv : result.raw()) {     System.out.println("KV: " + kv + ", Value: " +     Bytes.toString(kv.getValue()));  }}scanner.close();Get get = new Get(Bytes.toBytes("row-6"));get.setFilter(filter);Result result = table.get(get);System.out.println("Result of get: ");for (KeyValue kv : result.raw()) {     System.out.println("KV: " + kv + ", Value: " +     Bytes.toString(kv.getValue()));}

 

第七个FilterList,它有两个选项,需要全部通过,还是一个通过就行,MUST_PASS_ALL,MUST_PASS_ONE。

List
filters = new ArrayList
();Filter filter1 = newRowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,new BinaryComparator(Bytes.toBytes("row-03")));filters.add(filter1);302Filter filter2 = newRowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("row-06")));filters.add(filter2);Filter filter3 = newQualifierFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("col-0[03]"));filters.add(filter3);FilterList filterList1 = new FilterList(filters);Scan scan = new Scan();scan.setFilter(filterList1);ResultScanner scanner1 = table.getScanner(scan);for (Result result : scanner1) {for (KeyValue kv : result.raw()) { System.out.println("KV: " + kv + ", Value: " + Bytes.toString(kv.getValue())); }}scanner1.close();FilterList filterList2 = new FilterList(FilterList.Operator.MUST_PASS_ONE, filters);scan.setFilter(filterList2);ResultScanner scanner2 = table.getScanner(scan); for (Result result : scanner2) { for (KeyValue kv : result.raw()) { System.out.println("KV: " + kv + ", Value: " + Bytes.toString(kv.getValue())); }}scanner2.close();

转载于:https://www.cnblogs.com/cenyuhai/p/3231369.html

你可能感兴趣的文章
20170814 新鲜:EChart新增了日历图,要想办法用起来
查看>>
Lighttpd1.4.20源代码分析 笔记 状态机之错误处理和连接关闭
查看>>
具体解释MVP矩阵之ViewMatrix
查看>>
构建之法读书笔记 (1)
查看>>
table合并单元格colspan和rowspan
查看>>
Windows和Linux下查看Apache、MySQL、PHP版本
查看>>
centOs6.9服务器版本安装图解(包含java和mysql)
查看>>
new出来的对象无法调用@Autowired注入的Spring Bean
查看>>
jfrog artifactory jenkins pipeline 集成
查看>>
mysql开启慢查询日志
查看>>
大量用户升级iPhone3.0系统导致苹果服务器故障
查看>>
Gac代码库分析(1)
查看>>
IBM高级工程师,谷歌等国际知名公司工程师撰写Android开发教程合集
查看>>
关于图形学的那些让人迷惑的入门概念
查看>>
java中比较两个map是否相同
查看>>
阅读,学习编程的重要能力
查看>>
webpack 应用编译优化之路
查看>>
【刘文彬】区块链3.0:拥抱EOS
查看>>
javascript模拟鸟群使用cax和threejs渲染引擎
查看>>
设计模式——工厂方法模式
查看>>