Basic HBase Java Classes and Methods – Part 6: Scan a Table
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ScanTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = null;
/* ResultScanner instantiation */
try {
/* Table Scan Code */
} finally {
connection.close();
if (table != null) {
table.close();
}
/* Close scanResult */
}
}
}
So there are three areas we will cover that are new:
Define a ResultScanner
Code to Scan the table
Close scan resourcesDefine a ResultScanner First just before our try clause, we define a ResultScanner. We do this outside of the try clause for the same reason we do the table outside the try clause, because we need to check and close the resources in the finally clause and if we were to define this inside the try clause it would then be out of scope.
Scan scan = new Scan();
Scan the table
We get a Table object from our Connection object, just as we have done before. This time we run a getScanner method on the Table object using a Scan object that we instantiate. The results come back as a list which we iterate though. Each iteration we use the CellUtil utility class which allows us to access each part of the ResultScanner separately. You can read more about CellUtil in the JavaDoc.
CellUtil.cloneRow() is used to access the key
CellUtil.cloneFamily() is used to access the column family name
CellUtil.cloneQualifier() is used to access the column name
CellUtil.cloneValue() is used to access the column value
table = connection.getTable(TableName.valueOf("employee"));
Scan scan = new Scan();
scanResult = table.getScanner(scan);
for (Result res : scanResult) {
for (Cell cell : res.listCells()) {
String row = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String column = new String(CellUtil.cloneQualifier(cell));
String value = new String(CellUtil.cloneValue(cell));
System.out.println(row + " " + family + " " + column + " " + value);
}
} Close scan resources
Similar to how we close the connection and the table, we also need to close our ResultScanner.
if (scanResult != null) {
scanResult.close();
}
The final code is as follows:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ScanTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = null;
ResultScanner scanResult = null;
try {
table = connection.getTable(TableName.valueOf("employee"));
Scan scan = new Scan();
scanResult = table.getScanner(scan);
for (Result res : scanResult) {
for (Cell cell : res.listCells()) {
String row = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String column = new String(CellUtil.cloneQualifier(cell));
String value = new String(CellUtil.cloneValue(cell));
System.out.println(row + " " + family + " " + column + " " + value);
}
}
} finally {
connection.close();
if (table != null) {
table.close();
}
if (scanResult != null) {
scanResult.close();
}
}
}
}
This produces the following output:
1 personal age 50
1 personal first_name John
1 personal gender male
1 personal last_name Smith
1 personal marital_status married
1 professional education masters
1 professional occupation engineer
2 personal age 32
2 personal first_name Sally
2 personal gender female
2 personal last_name Jones
2 personal marital_status divorced
2 professional education MD
2 professional occupation doctor
3 personal first_name Alex
3 personal gender male
3 personal last_name Wright
3 professional education high school
3 professional occupation cab driver
In our next part, we will look at how to delete data from an HBase table. Next Basic HBase Java Classes and Methods – Part 7: Delete from a Table
Recent Posts
See AllRecently I was working on a problem with Time Series. Time Series can quickly add up to a lot of data, as you are using previous...
One 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