Hbase中多版本(version)数据获取办法

前言:本文介绍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进行封装。具体封装方法,如上。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Java应用程序中读取HBase表中的数据并将其存储到本地文件中,你需要以下步骤: 1. 在你的Java应用程序中添加HBase的依赖,你可以在pom.xml中添加如下依赖: ``` <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.4.12</version> </dependency> ``` 2. 创建HBase Configuration对象并设置HBase连接的相关参数,例如zookeeper地址和端口。 ``` Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "zookeeper1,zookeeper2,zookeeper3"); config.set("hbase.zookeeper.property.clientPort", "2181"); ``` 3. 创建HBase连接,使用HBase Configuration对象作为参数。 ``` Connection connection = ConnectionFactory.createConnection(config); ``` 4. 获取HBase表的实例,使用HBase连接和表名作为参数。 ``` Table table = connection.getTable(TableName.valueOf(tableName)); ``` 5. 使用HBase Scan对象指定要读取的列,然后使用HBase Table对象的getScanner方法执行Scan操作,获取结果集。 ``` Scan scan = new Scan(); scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); ResultScanner scanner = table.getScanner(scan); ``` 6. 遍历结果集,并将数据写入本地文件。你可以使用java.io.FileOutputStream和java.io.OutputStreamWriter将数据写入文件。 ``` FileOutputStream fos = new FileOutputStream(new File(filePath)); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); for (Result result : scanner) { String rowKey = Bytes.toString(result.getRow()); String value = Bytes.toString(result.getValue(Bytes.toBytes(columnFamily), ### 回答2: 以下是一个示例代码,用于使用Java应用程序读取HBase中的表数据并将其存储到本地文件中: ```java import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseToTextFile { public static void main(String[] args) throws IOException { // 创建HBase配置对象 Configuration conf = HBaseConfiguration.create(); // 设置HBase配置参数,例如HBase的zk地址 conf.set("hbase.zookeeper.quorum", "localhost"); conf.set("hbase.zookeeper.property.clientPort", "2181"); // 创建HBase连接对象 try (Connection connection = ConnectionFactory.createConnection(conf)) { // 获取表对象 TableName tableName = TableName.valueOf("your_table_name"); Table table = connection.getTable(tableName); // 创建扫描对象 Scan scan = new Scan(); // 执行扫描操作,获取结果集 ResultScanner scanner = table.getScanner(scan); // 创建文件写入对象 try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")))) { // 遍历结果集 for (Result result : scanner) { // 获取单元格列表 List<Cell> cells = result.listCells(); // 遍历单元格列表,将数据写入到文件中 for (Cell cell : cells) { byte[] rowArray = CellUtil.cloneRow(cell); byte[] familyArray = CellUtil.cloneFamily(cell); byte[] qualifierArray = CellUtil.cloneQualifier(cell); byte[] valueArray = CellUtil.cloneValue(cell); // 将字节数组转换为字符串 String row = Bytes.toString(rowArray); String family = Bytes.toString(familyArray); String qualifier = Bytes.toString(qualifierArray); String value = Bytes.toString(valueArray); // 写入文件 writer.println(row + "," + family + "," + qualifier + "," + value); } } // 输出完成信息 System.out.println("数据已经写入到本地文件。"); } } } } ``` 请注意,上述代码需要使用HBase的相关jar包,以及Hadoop的相关配置。你需要根据自己的环境进行相应的配置和引入相应的依赖。 ### 回答3: 可以使用Java应用程序通过HBase的Java API来读取表的数据并存储,然后使用IO流将数据写入本地文件。 以下是一个简单的示例代码: ```java import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.*; import java.io.*; public class HBaseDataExporter { public static void main(String[] args) throws IOException { // 设置HBase配置 Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); // HBase的ZooKeeper地址 config.set("hbase.zookeeper.property.clientPort", "2181"); // ZooKeeper端口 // 创建HBase连接 Connection connection = ConnectionFactory.createConnection(config); // 获取表 TableName tableName = TableName.valueOf("yourTableName"); // 表名 Table table = connection.getTable(tableName); // 创建本地文件输出流 FileWriter fileWriter = new FileWriter("data.txt"); // 扫描表中的数据并写入文件 ResultScanner scanner = table.getScanner(new Scan()); for (Result result : scanner) { // 获取行键 String rowKey = Bytes.toString(result.getRow()); // 获取所有列族和列的数据 for (Cell cell : result.listCells()) { // 获取列族 String family = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列名 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取值 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 将数据写入文件 fileWriter.write("RowKey: " + rowKey + ", Family: " + family + ", Qualifier: " + qualifier + ", Value: " + value + "\n"); } } // 关闭资源 scanner.close(); table.close(); connection.close(); fileWriter.close(); } } ``` 这个示例程序通过HBase的Java API连接到HBase,然后扫描指定表中的数据。每次获取到一行数据后,遍历所有的列族和列,并将数据写入本地文件"data.txt"中。 请注意,你需要将代码中的"yourTableName"替换为实际的表名,并且根据实际情况修改HBase的配置信息。 希望这个示例能帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值