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进行封装。具体封装方法,如上。


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