HBase Green Quest-Filter API

Source: Internet
Author: User

HBase provides a number of filter interfaces to filter the data so that the desired data is queried.

Row filter

For the filtering of row information, the parameters can be matched by prefix matching, bitwise AND, OR, XOR and substring matching. You can also control the equal, not_equal options to control the criteria for filtering data.

/** * Row filter Binarycomparator Nullcomparator: is not a null value * Bitcomparator: bit-level comparison with bitwise-, OR, XOR, or operations provided by the Bitwiseop class Rege Xstringcomparator: Regular match * Substringcomparator: substring is not included to match */    Private Static void Testrowfilter() {Try{htable table =Newhtable (config,"TestTable"); Scan Scan =NewScan (); Scan.addcolumn ("Col1". GetBytes (),"Name". GetBytes ());//Row filterFilter filter =NewRowFilter (Compareop.equal,NewBinarycomparator ("Row2". GetBytes ()));            Scan.setfilter (filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result) {Log.info ("line filter >"+ res); }//Regular line filterFilter Filter2 =NewRowFilter (Compareop.equal,NewRegexstringcomparator (". *.2"));            Scan.setfilter (FILTER2); Resultscanner resultregx = Table.getscanner (scan); for(Result res:resultregx) {Log.info ("Regular >"+ res); } Filter filtersubstring =NewRowFilter (Compareop.equal,NewSubstringcomparator ("W2"));            Scan.setfilter (filtersubstring); Resultscanner resultsubstring = Table.getscanner (scan); for(Result res:resultsubstring) {Log.info ("sub-string >"+ res);        } table.close (); }Catch(IOException e)        {Log.error (e); }    }
Column Family Filter

Filtering is based on the data of the column family, similar to the row filter above, and filtered by the criteria that control the filters in the corresponding parameters.

/** * Column Family filter * /    Private Static void Testfamlyfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewFamilyfilter (Compareop.equal,NewBinarycomparator ("Col1". GetBytes ())); Scan Scan =NewScan ("Row2". GetBytes (), filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result)            {Log.info (res); } Filter Filternull =NewFamilyfilter (Compareop.equal,NewRegexstringcomparator (". *.1")); Scan Scannull =NewScan ("Row2". GetBytes (), filternull); Scannull.addfamily ("Col1". GetBytes ()); Resultscanner resultnull = Table.getscanner (scannull);if(Resultnull! =NULL) { for(Result res:resultnull)                {Log.info (res); }            }Else{Log.info ("NULL");        } table.close (); }Catch(IOException e)        {Log.error (e); }    }
Column name filter

Similar to the above several filters, this is based on the column to filter, set the appropriate conditions can be filtered accordingly.

/** * Column name filter * /     Public Static void Testcolumfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewQualifierfilter (Compareop.equal,NewBinarycomparator ("Name". GetBytes ())); Scan Scan =NewScan ("Row2". GetBytes (), filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result)            {Log.info (res); } Get Get =NewGet ("Row2". GetBytes ());            Get.setfilter (filter);            Result resultget = Table.get (get);            Log.info (Resultget);        Table.close (); }Catch(IOException e)        {Log.info (e); }    }
Reference column Filter

The Reference column filter filters on the column family and column qualifiers to return all key-value pairs for the same time-stamped row as the reference column.

/** * Reference column filter */     Public Static void Testdependentcolumnfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewDependentcolumnfilter ("Col1". GetBytes (),"Name". GetBytes (),false); Scan Scan =NewScan ();            Scan.setfilter (filter); Resultscanner ResU = Table.getscanner (scan); for(Result Result:resu)            {Log.info (result); } Get Get =NewGet ("Row2". GetBytes ());            Get.setfilter (filter);            Result result = Table.get (get);            Log.info (result);        Table.close (); }Catch(IOException e)        {Log.error (e); }    }
Single row filter

It is not necessary to filter by the value of a column.

/** * Single row filter * /     Public Static void Testsinglecolumnvaluefilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewSinglecolumnvaluefilter ("Col1". GetBytes (),"Name". GetBytes (), Compareop.equal,"WY". GetBytes ()); Scan Scan =NewScan ();            Scan.setfilter (filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result)            {Log.info (res); } Get Get =NewGet ("Row2". GetBytes ());            Get.setfilter (filter);            Result resultget = Table.get (get);            Log.info (Resultget);        Table.close (); }Catch(IOException e)        {Log.info (e); }    }
Prefix filter

The data that matches the row key according to the prefix, in this case, is the data of the row prefixed with row.

/** * Prefix filter * /     Public Static void Testprefixfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewPrefixfilter ("Row". GetBytes ()); Scan Scan =NewScan ();            Scan.setfilter (filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result) {Log.info ("Res>"+ res); } Get Get =NewGet ("Row2". GetBytes ());            Result resultget = Table.get (get); Log.info ("Get>"+ Resultget);        Table.close (); }Catch(IOException e)        {Log.info (e); }    }
Paging Filter

Set the number of data in a page by Pagefilter, note that when you reset the start line, you make the new row and database different, otherwise, the dead loop cannot be stopped.

/** * Paged filter * /     Public Static void Testpagefilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewPagefilter (Ten);intTotalrows =0;byte[] LastRow =NULL; Scan Scan =NewScan (); while(true) {Scan.setfilter (filter);if(LastRow! =NULL) {//Plus 0 indicates that the new start prevents the content of the row from causing a dead loop                    byte[] StartRow = Bytes.add (lastrow, POSTFIX);                Scan.setstartrow (StartRow); } Resultscanner Resultscan = Table.getscanner (scan);intLocalrows =0; Result result = Resultscan.next (); while(Result! =NULL) {Log.info (result);                    localrows++;                    totalrows++;                    LastRow = Result.getrow ();                result = Resultscan.next (); }if(Localrows = =0) Break;            } log.info (Totalrows);        Table.close (); }Catch(IOException e)        {Log.info (e); }    }
/** * Column pagination filter * /     Public Static void Testcolumnpaginationfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filter =NewColumnpaginationfilter (5,Ten); Scan Scan =NewScan ();            Scan.setfilter (filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result)            {Log.info (res);        } table.close (); }Catch(IOException e)        {Log.info (e); }    }
Skip Filter

Used in conjunction with Valuefilter, if a row of a column does not meet the requirements of the filter is directly filtered out.

/** * Skip filter * /     Public Static void Testskipfilter() {Try{htable table =Newhtable (config,"TestTable"); Filter filt =NewValuefilter (Compareop.not_equal,NewBinarycomparator ("V". GetBytes ())); Scan Scanvalue =NewScan ();            Scanvalue.setfilter (filt); Resultscanner ress = Table.getscanner (Scanvalue); for(Result result:ress) {Log.info ("<"+ result); } Filter Filter =NewSkipfilter (filt); Scan Scan =NewScan ();            Scan.setfilter (filter); Resultscanner result = Table.getscanner (scan); for(Result Res:result) {Log.info (">"+ res);        } table.close (); }Catch(IOException e)        {Log.info (e); }    }
Full match Filter

The data is queried all the time before a condition is encountered, until the query is completed after the data that satisfies the condition is encountered.

    /** * Full match filter * /     Public Static void Testwhilematch() {Try{htable table =Newhtable (config,"TestTable"); Filter filt =NewRowFilter (Compareop.not_equal,NewBinarycomparator ("Row6". GetBytes ())); Scan Scan =NewScan ();            Scan.setfilter (filt); Resultscanner results = Table.getscanner (scan); for(Result res:results) {Log.info (">"+ res); } Filter Filter =NewWhilematchfilter (filt);            Scan.setfilter (filter); Resultscanner Resultscan = Table.getscanner (scan); for(Result Res:resultscan) {Log.info ("<"+ res);        } table.close (); }Catch(IOException e)        {Log.info (e); }    }
Filter combination

The above filter can be placed in a list, and then form a combination of multiple filters to filter.

    /** * Filter combinations * /     Public Static void testfilterlist() {list<filter> filterlist =NewArraylist<filter> (); Filter Filter1 =NewSinglecolumnvaluefilter ("Col1". GetBytes (),"Name". GetBytes (), Compareop.equal,"x". GetBytes ());        Filterlist.add (Filter1); Filter Filter2 =NewRowFilter (Compareop.not_equal,NewBinarycomparator ("Row2". GetBytes ()));        Filterlist.add (FILTER2); FilterList filters =NewFilterList (filterlist); Scan Scan =NewScan (); Scan.setfilter (filters);Try{htable table =Newhtable (config,"TestTable"); Resultscanner result = Table.getscanner (scan); for(Result Res:result)            {Log.info (res);        } table.close (); }Catch(IOException e)        {Log.info (e); }    }

Reprint Annotated Source: http://blog.csdn.net/wangyang1354/article/details/53761559

HBase Green Quest-Filter API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.