When querying the data using the HBase API, we often need to set some filter conditions to query the data, we need to use the HBase API of various filter to achieve this function.
Use filters in the HBase API to create a filter instance and then use Scan.setfilter () or get.setfilter () to use filter, as follows:
Table table = connection.gettable (tablename.valueof (table_name));
Scan Scan = new Scan ();
Filter filter = new RowFilter (compareoperator.equal, New Binarycomparator (Bytes.tobytes ("row_2"));
Scan.setfilter (filter);
Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
while (It.hasnext ()) {result result
= It.next ();
Printrow (result);
}
Resultscanner.close ();
Table.close ();
A large number of filter implementations are provided in the HBase API, such as some common filter:rowfilter: Filtering the specified Row Records familyfilter: Filtering the specified column family and returning null Qualifierfilter for other column families: Filtering the specified columns , the other columns return NULL Valuefilter: Filter The specified value, and the other columns return null Singlecolumnvaluefilter: A Single-column value filter Singlecolumnvalueexcludefilter: A Single-column value excludes the filter, Excluded columns return NULL Pagefilter: Paging filter columnpaginationfilter: Column Paging filter ...
The HBase API provides some common comparison operators that can be used to compare values in a filter, such as: Compareoperator.less compareoperator.less_or_equal Compareoperator.equal compareoperator.not_equal Compareoperator.greator compareoperator.greator_or_equal Compareoperator.no_op
The HBase API also provides a number of common comparators that can be used to compare values in a filter, such as: Binarycomparator regexstringcomparator nullcomparator Substringcomparator ...
Here is a complete story of how to look at using various Filter
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.exceptions.DeserializationException;
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 Testfilter {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);
Testfilter t = new Testfilter (connection);
T.test ();
Connection.close ();
Public Testfilter (Connection Connection) {this.connection = Connection;
private void Test () throws IOException, deserializationexception {createtable ();
Prepare ();
System.out.println ("----------Row Filter----------");
Filter (New RowFilter (Compareoperator.equal, New Binarycomparator (Bytes.tobytes ("Row_2")));
Filter (New RowFilter (Compareoperator.less_or_equal, New Binarycomparator (Bytes.tobytes ("Row_5")));
Filter (New RowFilter (Compareoperator.equal, New Regexstringcomparator ("row_*"));
System.out.println ("----------Family Filter----------"); Filter (New Familyfilter (compareoperator.equal, New BinarycoMparator (Bytes.tobytes ("base")));
Filter (New Familyfilter (Compareoperator.equal, New Regexstringcomparator ("address*"));
System.out.println ("----------Column Filter----------");
Filter (New Qualifierfilter (Compareoperator.equal, New Binarycomparator (Bytes.tobytes ("username")));
Filter (New Qualifierfilter (Compareoperator.equal, New Regexstringcomparator ("home*"));
System.out.println ("----------Value Filter----------");
Filter (New Valuefilter (Compareoperator.equal, New Binarycomparator (Bytes.tobytes ("User_0")));
Filter (New Valuefilter (Compareoperator.equal, New Regexstringcomparator ("password_*"));
System.out.println ("----------single Column Value Filter----------"); Filter (New Singlecolumnvaluefilter (Bytes.tobytes ("base"), Bytes.tobytes ("username"), compareoperator.equal
, New Binarycomparator (Bytes.tobytes ("User_0"))); System.out.println ("----------single Column Value Exclude Filter----------"); Filter (New Singlecolumnvalueexcludefilter (Bytes.tobytes ("base"), Bytes.tobytes ("username"), Compareoperato
R.equal, New Binarycomparator (Bytes.tobytes ("User_0")));
Deletetable ();
private void CreateTable () throws IOException {Admin admin = connection.getadmin ();
try {tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder (tablename.valueof (table_name))
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_base)). Build ())
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_address)). Build ())
. 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 filter (Filter filter) throws IOException {Table table = connection.gettable (tablename.valueof
(table_name));
Scan Scan = new Scan ();
Scan.setfilter (filter);
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 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 ());
}
}
}