Hbase中多版本(version)數據獲取辦法

前言:本文介紹2種獲取列的多版本數據的方式:shell和spring data hadoop

一、hbase shell中如何獲取

    1、在shell端創建一個Hbase表


[java] view plain copy

  1. create 't1','f1'  

    2、查看錶結構



[java] view plain copy

  1. describe 't1'  

表結構如下:



[java] view plain copy

  1. Table t1 is ENABLED                                                                                                                     

  2. t1                                                                                                                                      

  3. COLUMN FAMILIES DESCRIPTION                                                                                                             

  4. {NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NON  

  5. E', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'  

  6. }                                                                                                                                       

  7. 1 row(s) in 0.1370 seconds  

從上面的表結構中,我們可以看到,VERSIONS爲1,也就是說,默認情況只會存取一個版本的列數據,當再次插入的時候,後面的值會覆蓋前面的值。


    3、修改表結構,讓Hbase表支持存儲3個VERSIONS的版本列數據


[java] view plain copy

  1. alter 't1',{NAME=>'f1',VERSIONS=>3}  

修改後,shell終端顯示如下:



[java] view plain copy

  1. Updating all regions with the new schema...  

  2. 1/1 regions updated.  

  3. Done.  

  4. 0 row(s) in 2.5680 seconds  

再次查看錶結構:



[java] view plain copy

  1. Table t1 is ENABLED                                                                                                                     

  2. t1                                                                                                                                      

  3. COLUMN FAMILIES DESCRIPTION                                                                                                             

  4. {NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '3', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NON  

  5. E', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'  

  6. }                                                                                                                                       

  7. 1 row(s) in 0.0330 seconds  

我們會發現VERSIONS已經修改成了3.


    4、插入3行數據


[java] view plain copy

  1. hbase(main):015:0> put 't1','rowkey1','f1:name','chhliu'  

  2. 0 row(s) in 0.5890 seconds  

  3.   

  4. hbase(main):016:0> put 't1','rowkey1','f1:name','xyh123'  

  5. 0 row(s) in 0.1900 seconds  

  6.   

  7. hbase(main):017:0> put 't1','rowkey1','f1:name','chhliuxyh'  

  8. 0 row(s) in 0.1580 seconds  

  9.   

  10. hbase(main):018:0> get 't1','rowkey1','f1:name'  

  11. COLUMN                             CELL                                                                                                 

  12.  f1:name                           timestamp=1482820567560, value=chhliuxyh                                                             

  13. 1 row(s) in 0.2110 seconds  

從上面可以看出,插入了3行數據到表中,並且3行數據的rowkey一致,然後使用get命令來獲取這一行數據,發現只返回了最新的一行數據。


    5、獲取多行數據方法


[java] view plain copy

  1. hbase(main):002:0> get 't1','rowkey1',{COLUMN=>'f1:name',VERSIONS=>3}  

  2. COLUMN                             CELL                                                                                                 

  3.  f1:name                           timestamp=1482820567560, value=chhliuxyh                                                             

  4.  f1:name                           timestamp=1482820541363, value=xyh123                                                                

  5.  f1:name                           timestamp=1482820503889, value=chhliu                                                                

  6. 3 row(s) in 0.0960 seconds  

從上面的測試結果中,可以看出,一次性獲取了3個版本的數據。


二、spring data hadoop獲取多版本信息

    1、服務封裝如下:


[java] view plain copy

  1. public List<String> get(final String tableName, final byte[] rowName, final String familyName,  

  2.             final String qualifier) {  

  3.         return htemplate.execute(tableName, new TableCallback<List<String>>() {  

  4.   

  5.             @Override  

  6.             public List<String> doInTable(HTableInterface table) throws Throwable {  

  7.                 Get get = new Get(rowName);  

  8.                 get.setMaxVersions(3); // 設置一次性獲取多少個版本的數據  

  9.                 get.addColumn(familyName.getBytes(), qualifier.getBytes());  

  10.                 Result result = table.get(get);  

  11.                 List<Cell> cells = result.listCells();  

  12.                 String res = "";  

  13.                 List<String> list = new ArrayList<String>();  

  14.                 if(null != cells && !cells.isEmpty()){  

  15.                     for(Cell ce:cells){  

  16.                         res = Bytes.toString(ce.getValueArray(),  

  17.                                 ce.getValueOffset(),  

  18.                                 ce.getValueLength());  

  19.                         System.out.println("res:"+res+" timestamp:"+ce.getTimestamp());  

  20.                         list.add(res);  

  21.                     }  

  22.                 }  

  23.                 return list;  

  24.             }  

  25.         });  

  26.     }  

    2、測試

[java] view plain copy

  1. List<String> result = hService.get("t1", rowKey, "f1""name");  

  2.         System.out.println("result:"+result);  

[java] view plain copy

  1. res:chhliuxyh timestamp:1482820567560  

  2. res:xyh123 timestamp:1482820541363  

  3. res:chhliu timestamp:1482820503889  

從上面的測試結果可以看出,同時獲取了3個版本的列信息


PS:spring data hadoop默認提供的接口中,是沒有提供一次性獲取多個版本的列信息的接口的,需要我們自己使用Hbase原生的API進行封裝。具體封裝方法,如上。


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