HBase源代码调试(3)

HBase里面一个很重要的数据结构

hbase.client.Result

其中有一个private KeyValue[] kvs =null

KeyValue wraps a byte array and takes offsets and lengths into passed array at where to start interpreting the content as KeyValue. The KeyValue format inside a byte array is: <keylength> <valuelength> <key> <value> Key is further decomposed as:<rowlength> <row> <columnfamilylength> <columnfamily> <columnqualifier> <timestamp> <keytype>


当我们在客户端new HTable(conf, "testwy"), 会从meta文件搜索相关信息,并返回一个Result对象

当我们把此Result对象打印出来

keyvalues={testwy,,1356526985748.9c2583081c219f5331e7a80c5a63c244./info:regioninfo/1356526985844/Put/vlen=80/ts=0, testwy,,1356526985748.9c2583081c219f5331e7a80c5a63c244./info:server/1356526985935/Put/vlen=15/ts=0, testwy,,1356526985748.9c2583081c219f5331e7a80c5a63c244./info:serverstartcode/1356526985935/Put/vlen=8/ts=0}


 其中有三个keyvalue,结构都是

row/family:qualifier/timestamp/Type/vlen=(length of value)/ts=(memstore time stamp)

HRegionInfo.readFields function can analyse the string with 'info:regioninfo', to get the endkey, offLine, regionName, regionNameStr etc. member value.


		Put put = new Put(Bytes.toBytes("row1"));
		put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));
对应的 KeyValue值

[0, 0, 0, 28, 0, 0, 0, 4, 0, 4, 114, 111, 119, 49, 7, 99, 111, 108, 102, 97, 109, 49, 113, 117, 97, 108, 49, 127, -1, -1, -1, -1, -1, -1, -1, 4, 118, 97, 108, 49]




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,演示如何使用Apache Flink将数据写入HBase: ``` import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.api.java.DataSet; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class FlinkHBaseExample { public static void main(String[] args) throws Exception { // 创建Flink的执行环境 ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); // 加载数据集 DataSet<Tuple2<String, String>> data = env.fromElements( new Tuple2<>("row1", "value1"), new Tuple2<>("row2", "value2"), new Tuple2<>("row3", "value3") ); // 配置HBase连接信息 Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); config.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("mytable")); // 将数据写入HBase data.map(new MapFunction<Tuple2<String, String>, Put>() { @Override public Put map(Tuple2<String, String> value) throws Exception { Put put = new Put(Bytes.toBytes(value.f0)); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes(value.f1)); return put; } }).output(new HBaseOutputFormat(table)); // 执行程序 env.execute(); } } ``` 在上面的代码中,我们首先创建了Flink的执行环境,并加载了一个数据集。接下来,我们使用HBaseConfiguration类来配置HBase连接信息,然后创建一个HBase表对象。最后,我们使用map函数将数据转换为Put对象,并将其输出到HBase中。在output方法中,我们使用了一个自定义的HBaseOutputFormat对象,该对象用于将Put对象写入HBase表中。 需要注意的是,上面的代码中没有包含所有必要的依赖项。在实际使用中,您需要在项目中添加以下依赖项: ``` <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-core</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-java</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java_2.11</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-hbase</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hbase-client</artifactId> <version>${hbase.version}</version> </dependency> ``` 其中,${flink.version}、${hadoop.version}和${hbase.version}是您选择的Flink、Hadoop和HBase版本的占位符。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值