Filter query of hbase

There are many filters for hbase: they can be roughly divided into two categories: comparison filter and special filter. The function of filter is to...

There are many filters for hbase: they can be roughly divided into two categories: comparison filter and special filter. The function of filter is to judge whether the data meets the conditions at the service end, and then only return the data meeting the conditions to the client;
Comparison operator of hbase filter:

LESS ----- < LESS_OR_EQUAL ----- <= EQUAL ----- = NOT_EQUAL ----- <> GREATER_OR_EQUAL ----- >= GREATER ----- > NO_OP #Exclude all

Comparator for HBase filter (specify comparison mechanism):

BinaryComparator compares the specified byte array in byte index order, using Bytes.compareTo(byte []) The binary prefix comparator is the same as the previous one, only to compare whether the data at the left end is the same NullComparator determines whether the given is empty BitComparator bit by bit comparison RegexStringComparator provides a regular comparator that only supports EQUAL and non EQUAL Substring comparator determines whether the provided substring appears in value.

1. Comparison filter

//RowFilter Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("user0000"))); scan.setFilter(filter1);
//Family filter Filter filter1 = new FamilyFilter(CompareOp.LESS, new BinaryComparator(Bytes.toBytes("base_info"))); scan.setFilter(filter1);
//Column filter QualifierFilter Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("name"))); scan.setFilter(filter1);
//Value filter Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("zhangsan") ); scan.setFilter(filter1);
//TimestampsFilter List<Long> tss = new ArrayList<Long>(); tss.add(1495398833002l); Filter filter1 = new TimestampsFilter(tss); scan.setFilter(filter1);

2. Special filter

//Single column value filtersinglecolumnvaluefilter -- returns the whole row that meets the conditions SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5")); filter.setFilterIfMissing(true); //If not set to true, rows that do not contain the specified column will also return scan.setFilter(filter1);
//SingleColumnValueExcludeFilter ----- returns the result of excluding the column SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5")); filter.setFilterIfMissing(true); //If not set to true, rows that do not contain the specified column will also return scan.setFilter(filter1);
//Prefix filterprefixfilter ---- for row keys Filter filter = new PrefixFilter(Bytes.toBytes("row1")); scan.setFilter(filter1);
//Column prefixfilter Filter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2")); scan.setFilter(filter1);

Practical cases:

public class HBase_Filter01 { private static String ZK_KEY = "hbase.zookeeper.quorum"; private static String ZK_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181"; private static Configuration conf; private static Connection connection; private static Admin admin; static { conf = HBaseConfiguration.create(); conf.set(ZK_KEY,ZK_VALUE); try { connection= ConnectionFactory.createConnection(conf); admin=connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Scan scan=new Scan(); ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a")); scan.setFilter(filter); TableName tableName =TableName.valueOf("user_info"); try { Table table = connection.getTable(tableName); ResultScanner scanner = table.getScanner(scan); Iterator<Result> iterator=scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); System.out.println(result.list()); byte[] row = result.getRow(); System.out.println(new String(row)); } } catch (IOException e) { e.printStackTrace(); } } }

4 December 2019, 16:46 | Views: 7767

Add new comment

For adding a comment, please log in
or create account

0 comments