Now that we have seen how to Create a table and Put data into a table, we will explore how to read data out of a table. The main skeleton code is the the same as before, but this time we will construct a Get
object and pass it in to the get
method of the Table
object. The get
method is overloaded, so it will accept a single get
request or a list of get
requests.
First lets look at what the skeleton code looks like, everything except our get requests:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GetTable { private static byte[] PERSONAL_CF = Bytes.toBytes("personal"); private static byte[] PROFESSIONAL_CF = Bytes.toBytes("professional"); private static byte[] FIRST_NAME_COLUMN = Bytes.toBytes("first_name"); private static byte[] LAST_NAME_COLUMN = Bytes.toBytes("last_name"); private static byte[] OCCUPATION_COLUMN = Bytes.toBytes("occupation"); public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(conf); Table table = null; try { table = connection.getTable(TableName.valueOf("employee")); /* Our Get code will go here */ } } finally { connection.close(); if (table != null) { table.close(); } } } } |
First we will construct a basic Get
object, add three columns to it using the addColumn
method, pass that to the get
method of the Table
object and print out the results. Remember everything in HBase is a byte array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Get get = new Get(Bytes.toBytes("1")); get.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); get.addColumn(PERSONAL_CF, LAST_NAME_COLUMN); get.addColumn(PROFESSIONAL_CF, OCCUPATION_COLUMN); Result result = table.get(get); byte[] firstNameValue = result.getValue(PERSONAL_CF, FIRST_NAME_COLUMN); System.out.println("First Name: " + Bytes.toString(firstNameValue)); byte[] lastNameValue = result.getValue(PERSONAL_CF, LAST_NAME_COLUMN); System.out.println("Last Name: " + Bytes.toString(lastNameValue)); byte[] occupationValue = result.getValue(PROFESSIONAL_CF, OCCUPATION_COLUMN); System.out.println("Occupation: " + Bytes.toString(occupationValue)); System.out.println(); |
Now we will create two Get objects, add them to a list and then pass them in the same way. The result given back to us is a list which we will walk through and print the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
System.out.println("GetTable multiple results in one go:"); List<Get> getList = new ArrayList<Get>(); Get get1 = new Get(Bytes.toBytes("2")); get1.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); Get get2 = new Get(Bytes.toBytes("3")); get1.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); getList.add(get1); getList.add(get2); Result[] results = table.get(getList); for (Result res : results) { firstNameValue = res.getValue(PERSONAL_CF, FIRST_NAME_COLUMN); System.out.println("First Name: " + Bytes.toString(firstNameValue)); } |
Complete program is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GetTable { private static byte[] PERSONAL_CF = Bytes.toBytes("personal"); private static byte[] PROFESSIONAL_CF = Bytes.toBytes("professional"); private static byte[] FIRST_NAME_COLUMN = Bytes.toBytes("first_name"); private static byte[] LAST_NAME_COLUMN = Bytes.toBytes("last_name"); private static byte[] OCCUPATION_COLUMN = Bytes.toBytes("occupation"); public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(conf); Table table = null; try { table = connection.getTable(TableName.valueOf("employee")); Get get = new Get(Bytes.toBytes("1")); get.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); get.addColumn(PERSONAL_CF, LAST_NAME_COLUMN); get.addColumn(PROFESSIONAL_CF, OCCUPATION_COLUMN); Result result = table.get(get); byte[] firstNameValue = result.getValue(PERSONAL_CF, FIRST_NAME_COLUMN); System.out.println("First Name: " + Bytes.toString(firstNameValue)); byte[] lastNameValue = result.getValue(PERSONAL_CF, LAST_NAME_COLUMN); System.out.println("Last Name: " + Bytes.toString(lastNameValue)); byte[] occupationValue = result.getValue(PROFESSIONAL_CF, OCCUPATION_COLUMN); System.out.println("Occupation: " + Bytes.toString(occupationValue)); System.out.println(); System.out.println("GetTable multiple results in one go:"); List<Get> getList = new ArrayList<Get>(); Get get1 = new Get(Bytes.toBytes("2")); get1.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); Get get2 = new Get(Bytes.toBytes("3")); get1.addColumn(PERSONAL_CF, FIRST_NAME_COLUMN); getList.add(get1); getList.add(get2); Result[] results = table.get(getList); for (Result res : results) { firstNameValue = res.getValue(PERSONAL_CF, FIRST_NAME_COLUMN); System.out.println("First Name: " + Bytes.toString(firstNameValue)); } } finally { connection.close(); if (table != null) { table.close(); } } } } |
The results of our program are as follows:
1 2 3 4 5 6 7 |
First Name: John Last Name: Smith Occupation: engineer GetTable multiple results in one go: First Name: Sally First Name: Alex |
Next part we will look at using the Scan command to accomplish similar to what the SELECT command does in SQL. Next Basic HBase Java Classes and Methods – Part 6: Scan a Table