HBase Client api-filter List

Source: Internet
Author: User
Tags prepare

The first two articles say how to filter data using a single filter, but in many cases we need to do a combination of filtering, such as logic and logic or queries, at which point we can use filterlist to implement.

FilterList also implements the filter interface, so we can achieve some effects through multiple filter combinations.

Looking at the example below, we created two filters, the first one is the filter username=user_0, the second is the filter password=password_0, and then we combine the two filters into a FilterList object. and make a combination operator for Must_pass_all, meaning to filter the records that satisfy both of these conditions. You can then scan the records like you would with a normal filter.

Filter filter1 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("username"),
        Compareoperator.equal, New Binarycomparator (Bytes.tobytes ("User_0"));
Filter filter2 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("password"),
        Compareoperator.equal, New Binarycomparator (Bytes.tobytes ("Password_0"));

filterlist filterlist = new FilterList (FilterList.Operator.MUST_PASS_ALL, Filter1, filter2);

Table table = connection.gettable (tablename.valueof (table_name));
Scan Scan = new Scan ();
Scan.setfilter (filterlist);

Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
while (It.hasnext ()) {result result
    = It.next ();
    Printrow (result);
}
Resultscanner.close ();
Table.close ();

The following is a complete code example

Package my.hbasestudy;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.hbase.CompareOperator;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.TableName;
Import org.apache.hadoop.hbase.client.*;
Import org.apache.hadoop.hbase.filter.*;

Import org.apache.hadoop.hbase.util.Bytes;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.Iterator;

Import java.util.List;

    public class Testfilterlist {private static final String table_name = "user";
    private static final String column_family_base = "BASE";

    private static final String column_family_address = "Address";
    private static final String Column_username = "USERNAME";
    private static final String Column_password = "PASSWORD";
    private static final String Column_home = "Home";

    private static final String Column_office = "OFFICE";

    Private Connection Connection; public static void Main (string[] args) throws Exception {Configuration config = hbaseconfiguration.create ();

        Connection Connection = connectionfactory.createconnection (config);
        Testfilterlist t = new testfilterlist (connection);

        T.test ();
    Connection.close ();
    Public testfilterlist (Connection Connection) {this.connection = Connection;
        private void Test () throws IOException {createtable ();

        Prepare ();
        Filtermustpassall ();

        Filtermustpassone ();
    Deletetable (); private void Filtermustpassall () throws IOException {System.out.println ("----------must pass all--------
        --"); Filter filter1 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("username"), Compareoper Ator.
        EQUAL, New Binarycomparator (Bytes.tobytes ("User_0")); Filter filter2 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("password"), Compareoper Ator. EQUAL, New Binarycomparator (bytes.toBytes ("Password_0"));

        filterlist filterlist = new FilterList (FilterList.Operator.MUST_PASS_ALL, Filter1, Filter2);
        Table table = connection.gettable (tablename.valueof (table_name));
        Scan Scan = new Scan ();

        Scan.setfilter (filterlist);
        Resultscanner Resultscanner = Table.getscanner (scan);
        Iterator<result> it = Resultscanner.iterator ();
            while (It.hasnext ()) {result result = It.next ();
        Printrow (result);
        } resultscanner.close ();
    Table.close (); private void Filtermustpassone () throws IOException {System.out.println ("----------must pass one--------
        --"); Filter filter1 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("username"), Compareoper Ator.
        EQUAL, New Binarycomparator (Bytes.tobytes ("User_0")); Filter filter2 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("username"), CoMpareoperator.equal, New Binarycomparator (Bytes.tobytes ("user_10")); Filter Filter3 = new Singlecolumnvaluefilter (bytes.tobytes ("base"), Bytes.tobytes ("username"), Compareoper Ator.

        EQUAL, New Binarycomparator (Bytes.tobytes ("user_99"));

        filterlist filterlist = new FilterList (FilterList.Operator.MUST_PASS_ONE, Filter1, Filter2, Filter3);
        Table table = connection.gettable (tablename.valueof (table_name));
        Scan Scan = new Scan ();

        Scan.setfilter (filterlist);
        Resultscanner Resultscanner = Table.getscanner (scan);
        Iterator<result> it = Resultscanner.iterator ();
            while (It.hasnext ()) {result result = It.next ();
        Printrow (result);
        } resultscanner.close ();
    Table.close ();

        private void CreateTable () throws IOException {Admin admin = connection.getadmin (); try {tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder TabLENAME.VALUEOF (table_name)). addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder Bytes.toBytes (CO lumn_family_base)). Build ()). Addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (CO
            lumn_family_address)). Build ().
        Admin.createtable (TABLEDESC);
        finally {admin.close ();

        } private void Deletetable () throws IOException {Admin admin = connection.getadmin ();
            try {admin.disabletable (tablename.valueof (table_name));
        Admin.deletetable (TABLENAME.VALUEOF (table_name));
        finally {admin.close (); }} private void Prepare () throws ioexception {Table table = connection.gettable (tablename.valueof (table

        _name));
        list<row> actions = new arraylist<row> ();
  for (int i = 0; i < i++) {put on = new put (Bytes.tobytes ("Row_" + i));          Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_username), Bytes.tobytes ("user_" + i)); Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_password), Bytes.tobytes ("Password_" + i)
            );
            Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_home), Bytes.tobytes ("home_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_office), Bytes.tobytes ("Office_" + i));
        Actions.Add (Put);

        } object[] results = new object[actions.size ()];
        try {table.batch (actions, results);
        catch (Interruptedexception e) {e.printstacktrace ();
    } table.close (); private void Printrow (result result) {if (Bytes.tostring (Result.getrow ())!= null) {Stringbui
            Lder sb = new StringBuilder ();
            Sb.append (Bytes.tostring (Result.getrow ()));
    Sb.append ("[");        Sb.append ("base:username=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("username"))); Sb.append (", base:password=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("password")
            )));
            Sb.append (", address:home=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Home"))); Sb.append (", address:office=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Office"))
            );
            Sb.append ("]");
        System.out.println (Sb.tostring ());
 }
    }
}

Finally, for more complex filtering rules, you can implement your own filters.

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.