The previous blog said some of the HBase filter, today look at the HBase of the paging filter.
Paging filtering is done through Pagefilter in HBase, and when you create this parameter you need to set a pagesize parameter that controls the number of rows returned per page, and you need to specify the start line for this query each time you query.
One thing to note here is that the HBase row keys are sorted in dictionary order, so the result returned is in this order.
Next, take a look at the code snippet for pagination filtering
Filter filter = new Pagefilter (a);
Table table = connection.gettable (tablename.valueof (table_name));
byte[] lastrow = null;
while (true) {
Scan Scan = new Scan ();
Scan.setfilter (filter);
if (lastrow!= null) {
Scan.withstartrow (lastrow, false);
}
Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
int count = 0;
while (It.hasnext ()) {result result
= It.next ();
Printrow (result);
LastRow = Result.getrow ();
Count + +;
}
Resultscanner.close ();
if (count = = 0) {break
;
}
}
Table.close ();
First you need to create a Pagefilter object and set 10 rows per page. Then you set the starting line by Scan.withstartrow (), and for the first query, you don't have to set it. The second parameter is used to identify whether the specified start line needs to be included. Execute the query, for each query set a count, when the count is 0 o'clock, indicating that the query did not return results, the query traversal completed, this time out of the loop.
Here is the complete code to run
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 Testpagefilter {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);
Testpagefilter t = new Testpagefilter (connection);
T.test ();
Connection.close ();
Public Testpagefilter (Connection Connection) {this.connection = Connection;
private void Test () throws IOException, deserializationexception {createtable ();
Prepare ();
Filter filter = new Pagefilter (10);
Table table = connection.gettable (tablename.valueof (table_name));
byte[] lastrow = null;
while (true) {Scan Scan = new Scan ();
Scan.setfilter (filter);
if (lastrow!= null) {Scan.withstartrow (lastrow, false);
} Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator (); int count = 0;
while (It.hasnext ()) {result result = It.next ();
Printrow (result);
LastRow = Result.getrow ();
Count + +;
} resultscanner.close ();
if (count = = 0) {break;
} table.close ();
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 voidDeletetable () 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 ("Offic
E ")));
Sb.append ("]");
System.out.println (Sb.tostring ());
}
}
}