HBase相關API整理-Get

前言

    之前曾經發表過博文,整理了Hbase2.1.0之後的相關API。這裏對獲取數據的方法進行詳細整理

創建連接

//獲取到當前設置
Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "bigdate01:2181,bigdate02:2181,bigdate03:2181");
//創建Hbase連接
Connection conn = ConnectionFactory.createConnection(conf);

獲取操作表

//設置操作的表
TableName tablename=TableName.valueOf(Bytes.toBytes("testtable"));
Table table = conn.getTable(tablename);

獲取數據

通過行鍵 列族 列獲取到值

Get get=new Get(Bytes.toBytes("row-1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
//這種方法通過指定的列族 列名獲取到對應的值,獲取到的值只有一個版本
if(table.exists(get)) {//判斷需要獲取的數據是否存在
   Result result = table.get(get);
   byte[] value = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));     
   System.out.println("獲取到的值爲:"+Bytes.toString(value));
}

通過行鍵獲取到數據

Get get1=new Get(Bytes.toBytes("row-1"));
get1.addFamily(Bytes.toBytes("colfam1"));
//獲取到當前選中列族的數據
if (table.exists(get1)) {
   Result result = table.get(get1);
   NavigableMap<byte[], byte[]> fam1 = result.getFamilyMap(Bytes.toBytes("colfam1"));
   Set<Entry<byte[], byte[]>> entrySet = fam1.entrySet();
   Iterator<Entry<byte[], byte[]>> iterator = entrySet.iterator();
   System.out.println("獲取到的鍵值對數量:"+result.size());
   while(iterator.hasNext()) {
	Entry<byte[], byte[]> next = iterator.next();
	byte[] key = next.getKey();
	System.out.println("當前獲取到的鍵爲:"+Bytes.toString(key)+",當前獲取的值爲:"+Bytes.toString(fam1.get(key)));
   }
}

get的其他方法

Get get3=new Get(Bytes.toBytes("row-1"));
get3.addFamily(Bytes.toBytes("colfam1"));//限制讀取的列族
//設置讀取的列族和列,這種方法讀取的的值,獲取到值是當前列的最新版本。
get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
get3.setTimeRange(0, Integer.MAX_VALUE);//設置可讀取的時間戳範圍
get3.setTimestamp(12458585);//設置讀取數據的時間戳
	    
get3.setMaxVersions();//設置讀取的最大版本數,這種設置會把最大的版本數爲Integer.maxvalue()
 get3.setMaxVersions(2);//設置讀取的最大版本數爲2
 System.out.println("列族大小:"+get3.numFamilies());//這裏獲取到大小,指的是通過Get對象中設置的列族數量

Result對象的說明

/**創建的get對象,通過table.get()方法執行,返回值Result
*
*/
Get get3=new Get(Bytes.toBytes("row-1"));
get3.addFamily(Bytes.toBytes("colfam1"));//限制讀取的列族
//設置讀取的列族和列,這種方法讀取的的值,獲取到值是當前列的最新版本。
get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
Result result = table.get(get3);
//返回對應列的最新單元格的值
byte[] value = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
//獲取Get是咧設置的行鍵
byte[] row = result.getRow();
System.out.println("獲取到當前對應的行鍵:"+Bytes.toString(row));

判斷獲取的數據是否爲空

/***************************************************************************************
* size()方法和isEmpty()方法,兩者是對服務器端返回的鍵值對進行操作
* 在HBase以前的版本中,返回的鍵值對是以KeyValue對象的形式返回的
* 在2.1.0及以後的版本中,返回的鍵值對是以Cell的對象的形式
		 ***************************************************************************************
*/
int size = result.size();
System.out.println("服務器返回的鍵值對數量爲:"+size);
System.out.println("判斷當前返回的數據是否爲空:"+result.isEmpty());

讀取Cell對象

Cell[] cells = result.rawCells();
//讀取cell對象
if(cells.length>0) {
    for (int i = 0; i < cells.length; i++) {
	System.out.println("當前讀取到的行:"+Bytes.toString(CellUtil.cloneRow(cells[i])));
	System.out.println("當前讀取到的列族:"+Bytes.toString(CellUtil.cloneFamily(cells[i])));
	System.out.println("當前讀取到的列:"+Bytes.toString(CellUtil.cloneQualifier(cells[i])));
	System.out.println("當前讀取到的值:"+Bytes.toString(CellUtil.cloneValue(cells[i])));
			}
}else {
    System.out.println("讀取到的數據大小爲空");
}

Result的其他方法

//返回對應列的最新版本值,和getColumn不同的是,它返回的對象封裝成了Cell對象
Cell cell = result.getColumnLatestCell(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
boolean contains = result.containsColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
System.out.println("判斷結果中是否包含對應的值:"+contains);
		
//將所有的get請求返回的內容都裝入一個Java的Map類實例,這樣用戶可以使用該方法遍歷所有的結果
//          行鍵                 列族                 時間戳 列
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
//和getMap不同的是,只會獲取到當前每個列的最新版本
NavigableMap<byte[], NavigableMap<byte[], byte[]>> noVersionMap = result.getNoVersionMap();
//通過指定的列族,獲取結果中所有版本
NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes("colfam1"));
Set<Entry<byte[], byte[]>> entrySet = familyMap.entrySet();
Iterator<Entry<byte[], byte[]>> it = entrySet.iterator();
while(it.hasNext()) {
	Entry<byte[], byte[]> next = it.next();
	System.out.println("familyMap獲取到的鍵:"+Bytes.toString(next.getKey())+",獲取到的值爲:"+Bytes.toString(next.getValue()));
}

一次請求,多次獲取數據

Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "bigdate01:2181,bigdate02:2181,bigdate03:2181");
Connection conn = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf(Bytes.toBytes("testtable"));
Table table = conn.getTable(tableName);
List<Get> gets=new ArrayList<>();
Get get=new Get(Bytes.toBytes("row-1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("name"));
if(table.exists(get)) {//首先判斷表中是否存在請求數據
	gets.add(get);
}
Get get1=new Get(Bytes.toBytes("row-1"));
get1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("lib"));
if(table.exists(get1)) {//判斷需要獲取的數據是否存在
	gets.add(get1);
}
Result[] results = table.get(gets);
System.out.println("First iteration 。。。。");
for (Result result : results) {
	String row=Bytes.toString(result.getRow());
	System.out.println("Row:"+row+" ");
	for (Cell cell : result.rawCells()) {
System.out.println("Family:"+Bytes.toString(CellUtil.cloneFamily(cell))+",quailer:"+Bytes.toString(CellUtil.cloneQualifier(cell))+",value:"+Bytes.toString(CellUtil.cloneValue(cell)));
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章