You have to provide a row key when creating an instannce of Get,using one of these constructors。
Get(byte[] row)
Get (byte[] row, RowLock rowlock)
A get() operation is bound to one specific row,but can retrieve any number of columns and/or cells contained therein.
package client; // cc GetExample Example application retrieving data from HBase import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import util.HBaseHelper; import java.io.IOException; public class GetExample { public static void main(String[] args) throws IOException { // vv GetExample Configuration conf = HBaseConfiguration.create(); // co GetExample-1-CreateConf Create the configuration. // ^^ GetExample HBaseHelper helper = HBaseHelper.getHelper(conf); if (!helper.existsTable("testtable")) { helper.createTable("testtable", "colfam1"); } // vv GetExample HTable table = new HTable(conf, "testtable"); // co GetExample-2-NewTable Instantiate a new table reference. Get get = new Get(Bytes.toBytes("row1")); // co GetExample-3-NewGet Create get with specific row. get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co GetExample-4-AddCol Add a column to the get. Result result = table.get(get); // co GetExample-5-DoGet Retrieve row with selected columns from HBase. byte[] val = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co GetExample-6-GetValue Get a specific value for the given column. System.out.println("Value: " + Bytes.toString(val)); // co GetExample-7-Print Print out the value while converting it back. // ^^ GetExample } }
As mentioned earlier HBase provide us with a helper class named Bytes that has many static method to convert Java tpes into byte[] arrays.It also can do the same in reverse.
The Result Class
When you retriee data using the get() calls it wraps all matching cells into an instance of the Result class.It provides you with the means to access everything that was returned from the server for the given row and matching the specified query, such as column family, column qualifier, timestamp, and so on.
Dump the Contents
All Java objects have a toString() method,which,when overriden by a class,can be used to convert the data of an instance into a text representation。This is not for serialization perposes, but most often used for debugging。
The Result class has such an implementation of toString(),dumping the result of a read call as a string.The output looks like this:
keyvalue={row-2/colfam1:col-5/1300802024293/Put/vlen=7,row-2/colfam2:col-33/1300802024325/Put/vlen=8)
It simply prints all contained KeyValue instance,i.e.,calling KeyValue.toString()respectively. If the Result instance is empty,the output will be:keyvalue=NONE
List of Gets
package client; // cc GetListExample Example of retrieving data from HBase using lists of Get instances import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import util.HBaseHelper; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GetListExample { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); if (!helper.existsTable("testtable")) { helper.createTable("testtable", "colfam1"); } HTable table = new HTable(conf, "testtable"); // vv GetListExample byte[] cf1 = Bytes.toBytes("colfam1"); byte[] qf1 = Bytes.toBytes("qual1"); byte[] qf2 = Bytes.toBytes("qual2"); // co GetListExample-1-Prepare Prepare commonly used byte arrays. byte[] row1 = Bytes.toBytes("row1"); byte[] row2 = Bytes.toBytes("row2"); List<Get> gets = new ArrayList<Get>(); // co GetListExample-2-CreateList Create a list that holds the Get instances. Get get1 = new Get(row1); get1.addColumn(cf1, qf1); gets.add(get1); Get get2 = new Get(row2); get2.addColumn(cf1, qf1); // co GetListExample-3-AddGets Add the Get instances to the list. gets.add(get2); Get get3 = new Get(row2); get3.addColumn(cf1, qf2); gets.add(get3); Result[] results = table.get(gets); // co GetListExample-4-DoGet Retrieve rows with selected columns from HBase. System.out.println("First iteration..."); for (Result result : results) { String row = Bytes.toString(result.getRow()); System.out.print("Row: " + row + " "); byte[] val = null; if (result.containsColumn(cf1, qf1)) { // co GetListExample-5-GetValue1 Iterate over results and check what values are available. val = result.getValue(cf1, qf1); System.out.println("Value: " + Bytes.toString(val)); } if (result.containsColumn(cf1, qf2)) { val = result.getValue(cf1, qf2); System.out.println("Value: " + Bytes.toString(val)); } } System.out.println("Second iteration..."); for (Result result : results) { for (KeyValue kv : result.raw()) { System.out.println("Row: " + Bytes.toString(kv.getRow()) + // co GetListExample-6-GetValue2 Iterate over results again, printing out all values. " Value: " + Bytes.toString(kv.getValue())); } } // ^^ GetListExample } }
boolean exists(Get get) throws IOException
You can set up a Get instance,just like you do when using the get() calls of HTable.Instead of having to retrieve the data from the remote servers, using an RPC call,to verify that it actually exists you can employ this call because it only returns a boolean flag indicating that same fact.
Using exists() involves the same lookup semantics on the region servers,including loading file blocks to check if a row or column actually exists.You only avoid shipping the data over the network-but that is very useful if you are checking very large columns, or do so very frequently.
Result getRowOrBefore(byte[] row, byte[] family) throws IOException
You need to specify the row you are looking for, and a column family.The latter is required because in HBase,being a column-oriented database, there is no row if there are no columns. Specifying a family name tells the servers to check if the row searched for has any values in a column contained in the given family.
package client; // cc GetRowOrBeforeExample Example using a special retrieval method. import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import util.HBaseHelper; import java.io.IOException; public class GetRowOrBeforeExample { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); if (!helper.existsTable("testtable")) { helper.createTable("testtable", "colfam1"); } HTable table = new HTable(conf, "testtable"); // vv GetRowOrBeforeExample Result result1 = table.getRowOrBefore(Bytes.toBytes("row1"), // co GetRowOrBeforeExample-1-GetRow1 Attempt to find an existing row. Bytes.toBytes("colfam1")); System.out.println("Found: " + Bytes.toString(result1.getRow())); // co GetRowOrBeforeExample-2-SOUT1 Print what was found. Result result2 = table.getRowOrBefore(Bytes.toBytes("row99"), // co GetRowOrBeforeExample-3-GetRow2 Attempt to find a non-existent row. Bytes.toBytes("colfam1")); System.out.println("Found: " + Bytes.toString(result2.getRow())); // co GetRowOrBeforeExample-4-SOUT2 Returns the row that was sorted at the end of the table. for (KeyValue kv : result2.raw()) { System.out.println(" Col: " + Bytes.toString(kv.getFamily()) + // co GetRowOrBeforeExample-5-Dump Print the returned values. "/" + Bytes.toString(kv.getQualifier()) + ", Value: " + Bytes.toString(kv.getValue())); } Result result3 = table.getRowOrBefore(Bytes.toBytes("abc"), // co GetRowOrBeforeExample-6-GetRow3 Attempt to find a row before the test rows. Bytes.toBytes("colfam1")); System.out.println("Found: " + result3); // co GetRowOrBeforeExample-7-SOUT3 Should return "null" since there is no match. // ^^ GetRowOrBeforeExample } }