Basic HBase Java Classes and Methods – Part 5: Getting Data from a Table
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.
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.
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:
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:
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
Recent Posts
See AllOne of the biggest bottlenecks in Deep Learning is loading data. having fast drives and access to the data is important, especially if...
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName;...
Comments