各种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。
Listfilters = 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();