import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellScanner;
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;
/**
* Created by similarface on 16/8/22.
*/
public class RetrievesRequestedRowNecessary {
public static void main(String args[]) throws IOException {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的連接
Table table = connection.getTable(TableName.valueOf("testtable"));
//獲取行鍵 企圖獲取一個不存在的行
Get get1 = new Get(Bytes.toBytes("95599"));
//添加查詢的列族和列限定符號
get1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
//獲取結果級
Result result1 = table.get(get1);
//結果集是否為空 ==> Get 1 isEmpty: true
System.out.println("Get 1 isEmpty: " + result1.isEmpty());
//根據result獲取游標
CellScanner scanner1 = result1.cellScanner();
//循環遍歷結果集的數據 當然是沒有任何數據
while (scanner1.advance()) {
System.out.println("Get 1 Cell: " + scanner1.current());
}
//95599行鍵的值 數據庫不存在這行數據
Get get2 = new Get(Bytes.toBytes("95599"));
//指定列族和列限定符
get2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
//95599這行是在數據庫中沒有的 setClosestRowBefore 會試圖去取前面的行如果有必要的話
get2.setClosestRowBefore(true);
//獲取數據集 ==> Get 2 isEmpty: false
Result result2 = table.get(get2);
System.out.println("Get 2 isEmpty: " + result2.isEmpty());
//繼續獲取游標
CellScanner scanner2 = result2.cellScanner();
//遍歷游標獲取數據 ==> Get 2 Cell: 10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
while (scanner2.advance()) {
System.out.println("Get 2 Cell: " + scanner2.current());
}
//這行數據庫是存在的
Get get3 = new Get(Bytes.toBytes("10010"));
get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"));
//依然+了setClosestRowBefore
get3.setClosestRowBefore(true);
// ==> Get 3 isEmpty: false
Result result3 = table.get(get3);
System.out.println("Get 3 isEmpty: " + result3.isEmpty());
CellScanner scanner3 = result3.cellScanner();
//如果存在行還是取出的該行的數據
while (scanner3.advance()) {
//叫了setClosestRowBefore 的作用在這兒是請求列族中的所有列分隔的字段都要取出來
// Get 3 Cell: 10010/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
// Get 3 Cell: 10010/colfam1:qual2/1471836722159/Put/vlen=18/seqid=0
System.out.println("Get 3 Cell: " + scanner3.current());
}
Get get4 = new Get(Bytes.toBytes("10086"));
get4.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
//Get 4 isEmpty: false
Result result4 = table.get(get4);
System.out.println("Get 4 isEmpty: " + result4.isEmpty());
CellScanner scanner4 = result4.cellScanner();
while (scanner4.advance()) {
//Get 4 Cell: 10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
System.out.println("Get 4 Cell: " + scanner4.current());
}
}
}
/**
Get 1 isEmpty: false
Get 1 Cell: 10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
Get 2 isEmpty: false
Get 2 Cell: 10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
Get 3 isEmpty: false
Get 3 Cell: 10010/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
Get 3 Cell: 10010/colfam1:qual2/1471836722159/Put/vlen=18/seqid=0
Get 4 isEmpty: false
Get 4 Cell: 10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0
**/