This article describes the Java&Shell API of HBase RowFilter in detail, and provides some code examples for reference. RowFilter filters data based on row keys. You can use the HBase Rowkey to filter data. For details about the Comparator, see HBase Filter Comparator principle and source code

one Java Api

The head of code

public class RowFilterDemo { private static boolean isok = false; private static String tableName = "test"; private static String[] cfs = new String[]{"f"}; 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"}; public static void main(String[] args) throws IOException { MyBase myBase = new MyBase(); Connection connection = myBase.createConnection(); if (isok) { myBase.deleteTable(connection, tableName); myBase.createTable(connection, tableName, cfs); myBase.putRows(connection, tableName, data); } Table Table = connection.gettable (tablename.valueof (TableName)); Scan scan = new Scan();Copy the code

Slide the middle code to the right to see the output.

1. The BinaryComparator constructs the filter

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-ac]Copy the code

2. BinaryPrefixComparator constructs the filter

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // []
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]Copy the code

3. SubstringComparator constructs the filter

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

4. RegexStringComparator constructs the filter

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

5. NullComparator constructs the filter

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

The tail code

scan.setFilter(rowFilter); ResultScanner scanner = table.getScanner(scan); Iterator<Result> iterator = scanner.iterator(); LinkedList<String> rowkeys = new LinkedList<>(); while (iterator.hasNext()) { Result result = iterator.next(); String rowkey = Bytes.toString(result.getRow()); rowkeys.add(rowkey); } System.out.println(rowkeys); scanner.close(); table.close(); connection.close(); }}Copy the code

two Shell Api

1. The BinaryComparator constructs the filter

A:

hbase(main):006:0> scan 'test',{FILTER=>"RowFilter(=,'binary:row-ab')"} ROW COLUMN+CELL row-ab column=f:c2, Timestamp =1588156704669, value=v2 1 row(s) in 0.0140 secondsCopy the code

Supported comparison operators: =! = > >= < < <=.

Method 2:

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

The comparison operators are LESS, LESS_OR_EQUAL, EQUAL, NOT_EQUAL, GREATER, and GREATER_OR_EQUAL.

Recommended use method 1, more concise and convenient.

2. BinaryPrefixComparator constructs the filter

A:

hbase(main):023:0> scan 'test',{FILTER=>"RowFilter(=,'binaryprefix:row-ab')"} ROW COLUMN+CELL row-ab column=f:c2, Timestamp =1588156704669, value=v2 row-abc column= F :c4, timestamp=1588156704669, value= V4 2 row(s) in 0.0360 secondsCopy the code

Method 2:

import org.apache.hadoop.hbase.filter.CompareFilter import org.apache.hadoop.hbase.filter.BinaryPrefixComparator import org.apache.hadoop.hbase.filter.RowFilter hbase(main):027:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('row-ab')))} ROW COLUMN+CELL row-ab column=f:c2, timestamp=1588156704669, Value =v2 row-abc column= F :c4, timestamp=1588156704669, value= V4 2 row(s) in 0.0110 secondsCopy the code

Ditto for everything else.

3. SubstringComparator constructs the filter

A:

hbase(main):001:0> scan 'test',{FILTER=>"RowFilter(=,'substring:row-ab')"} ROW COLUMN+CELL row-ab column=f:c2, Timestamp =1588156704669, value=v2 row-abc column= F :c4, timestamp=1588156704669, value= V4 2 row(s) in 0.3200 secondsCopy the code

Method 2:

import org.apache.hadoop.hbase.filter.CompareFilter import org.apache.hadoop.hbase.filter.SubstringComparator import org.apache.hadoop.hbase.filter.RowFilter hbase(main):007:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('row-ab'))} ROW COLUMN+CELL row-ab column=f:c2, timestamp=1588156704669, Value =v2 row-abc column= F :c4, timestamp=1588156704669, value= V4 2 row(s) in 0.0230 secondsCopy the code

The difference is that the string is passed in for comparison, and only EQUAL and NOT_EQUAL are supported.

4. RegexStringComparator constructs the filter

import org.apache.hadoop.hbase.filter.CompareFilter import org.apache.hadoop.hbase.filter.RegexStringComparator import org.apache.hadoop.hbase.filter.RowFilter hbase(main):007:0> scan 'test',{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('row-ab'))} ROW COLUMN+CELL row-ab column=f:c2, timestamp=1588156704669, Value =v2 row-abc column= F :c4, timestamp=1588156704669, value= V4 2 row(s) in 0.0230 secondsCopy the code

The comparator passes strings directly for comparison and only supports EQUAL and NOT_EQUAL. If you want to use the first method, you can pass in regexString and try it out. My version is a little bit too low to support it, so I won’t show it again.

Note that the regular match here refers to inclusion, corresponding to the underlying find() method.

Additionally, RowFilter does not support the LongComparator comparator, and bitComparators and NullComparators are rarely used and are no longer introduced.

To view the full source code of the article, visit GitHub:

https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/RowFilterD emo.javaCopy the code

Reprint please indicate the source! Welcome to follow my wechat official account [HBase Work Notes]