前言:本文介绍2种获取列的多版本数据的方式:shell和spring data hadoop
一、hbase shell中如何获取
1、在shell端创建一个Hbase表
[java] view plain copy
- create 't1','f1'
2、查看表结构
[java] view plain copy
- describe 't1'
表结构如下:
[java] view plain copy
从上面的表结构中,我们可以看到,VERSIONS为1,也就是说,默认情况只会存取一个版本的列数据,当再次插入的时候,后面的值会覆盖前面的值。
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
3、修改表结构,让Hbase表支持存储3个VERSIONS的版本列数据
[java] view plain copy
- alter 't1',{NAME=>'f1',VERSIONS=>3}
修改后,shell终端显示如下:
[java] view plain copy
再次查看表结构:
1. Updating all regions with the new
2. 1/1
3. Done.
4. 0 row(s) in 2.5680
[java] view plain copy
我们会发现VERSIONS已经修改成了3.
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
4、插入3行数据
[java] view plain copy
从上面可以看出,插入了3行数据到表中,并且3行数据的rowkey一致,然后使用get命令来获取这一行数据,发现只返回了最新的一行数据。
1. hbase(main):015:0> put 't1','rowkey1','f1:name','chhliu'
2. 0 row(s) in 0.5890
3.
4. hbase(main):016:0> put 't1','rowkey1','f1:name','xyh123'
5. 0 row(s) in 0.1900
6.
7. hbase(main):017:0> put 't1','rowkey1','f1:name','chhliuxyh'
8. 0 row(s) in 0.1580
9.
10. hbase(main):018:0> get 't1','rowkey1','f1:name'
11. COLUMN CELL
12. 1482820567560, value=chhliuxyh
13. 1 row(s) in 0.2110
5、获取多行数据方法
[java] view plain copy
从上面的测试结果中,可以看出,一次性获取了3个版本的数据。
1. hbase(main):002:0> get 't1','rowkey1',{COLUMN=>'f1:name',VERSIONS=>3}
2. COLUMN CELL
3. 1482820567560, value=chhliuxyh
4. 1482820541363, value=xyh123
5. 1482820503889, value=chhliu
6. 3 row(s) in 0.0960
二、spring data hadoop获取多版本信息
1、服务封装如下:
[java] view plain copy
2、测试
1. public List<String> get(final String tableName, final byte[] rowName, final
2. final
3. return htemplate.execute(tableName, new
4.
5. @Override
6. public List<String> doInTable(HTableInterface table) throws
7. new
8. 3); // 设置一次性获取多少个版本的数据
9. get.addColumn(familyName.getBytes(), qualifier.getBytes());
10. Result result = table.get(get);
11. List<Cell> cells = result.listCells();
12. "";
13. new
14. if(null
15. for(Cell ce:cells){
16. res = Bytes.toString(ce.getValueArray(),
17. ce.getValueOffset(),
18. ce.getValueLength());
19. "res:"+res+" timestamp:"+ce.getTimestamp());
20. list.add(res);
21. }
22. }
23. return
24. }
25. });
26. }
[java] view plain copy
- List<String> result = hService.get("t1", rowKey, "f1", "name");
- "result:"+result);
[java] view plain copy
- res:chhliuxyh timestamp:1482820567560
- res:xyh123 timestamp:1482820541363
- res:chhliu timestamp:1482820503889