前言:本文介绍2种获取列的多版本数据的方式:shell和spring data hadoop

一、hbase shell中如何获取

    1、在shell端创建一个Hbase表

create 't1','f1'

   2、查看表结构

describe 't1'

表结构如下:

Table t1 is ENABLED                                                                                                                   
t1                                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                                           
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NON
E', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'
}                                                                                                                                     
1 row(s) in 0.1370 seconds

从上面的表结构中,我们可以看到,VERSIONS为1,也就是说,默认情况只会存取一个版本的列数据,当再次插入的时候,后面的值会覆盖前面的值。

  3、修改表结构,让Hbase表支持存储3个VERSIONS的版本列数据

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

修改后,shell终端显示如下:

Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.5680 seconds

再次查看表结构:

Table t1 is ENABLED                                                                                                                   
t1                                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                                           
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '3', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NON
E', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'
}                                                                                                                                     
1 row(s) in 0.0330 seconds

我们会发现VERSIONS已经修改成了3.

  4、插入3行数据

hbase(main):015:0> put 't1','rowkey1','f1:name','chhliu'
0 row(s) in 0.5890 seconds
hbase(main):016:0> put 't1','rowkey1','f1:name','xyh123'
0 row(s) in 0.1900 seconds
hbase(main):017:0> put 't1','rowkey1','f1:name','chhliuxyh'
0 row(s) in 0.1580 seconds
hbase(main):018:0> get 't1','rowkey1','f1:name'
COLUMN                             CELL                                                                                               
 f1:name                           timestamp=1482820567560, value=chhliuxyh                                                           
1 row(s) in 0.2110 seconds

从上面可以看出,插入了3行数据到表中,并且3行数据的rowkey一致,然后使用get命令来获取这一行数据,发现只返回了最新的一行数据。

    5、获取多行数据方法

hbase(main):002:0> get 't1','rowkey1',{COLUMN=>'f1:name',VERSIONS=>3}
COLUMN                             CELL                                                                                               
 f1:name                           timestamp=1482820567560, value=chhliuxyh                                                           
 f1:name                           timestamp=1482820541363, value=xyh123                                                              
 f1:name                           timestamp=1482820503889, value=chhliu                                                              
3 row(s) in 0.0960 seconds

从上面的测试结果中,可以看出,一次性获取了3个版本的数据。

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

    1、服务封装如下:

public List<String> get(final String tableName, final byte[] rowName, final String familyName,
			final String qualifier) {
		return htemplate.execute(tableName, new TableCallback<List<String>>() {
 
			@Override
			public List<String> doInTable(HTableInterface table) throws Throwable {
				Get get = new Get(rowName);
				get.setMaxVersions(3); // 设置一次性获取多少个版本的数据
				get.addColumn(familyName.getBytes(), qualifier.getBytes());
				Result result = table.get(get);
				List<Cell> cells = result.listCells();
				String res = "";
				List<String> list = new ArrayList<String>();
				if(null != cells && !cells.isEmpty()){
					for(Cell ce:cells){
						res = Bytes.toString(ce.getValueArray(),
								ce.getValueOffset(),
								ce.getValueLength());
						System.out.println("res:"+res+" timestamp:"+ce.getTimestamp());
						list.add(res);
					}
				}
				return list;
			}
		});
	}

    2、测试

List<String> result = hService.get("t1", rowKey, "f1", "name");
		System.out.println("result:"+result);
res:chhliuxyh timestamp:1482820567560
res:xyh123 timestamp:1482820541363
res:chhliu timestamp:1482820503889

从上面的测试结果可以看出,同时获取了3个版本的列信息

PS:spring data hadoop默认提供的接口中,是没有提供一次性获取多个版本的列信息的接口的,需要我们自己使用Hbase原生的API进行封装。具体封装方法,如上。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐