HBase-HLog分析

2024-09-03 16:48
文章标签 分析 hbase hlog

本文主要是介绍HBase-HLog分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

HLog的全部实现在包:

org.apache.hadoop.hbase.regionserver.wal 中

 

相关的配置为

参数名默认值含义
hbase.regionserver.hlog.enabledtrue是否启用WAL
hbase.regionserver.hlog.writer.implSequenceFileLogWriterHLog.Writer实现类
hbase.regionserver.hlog.reader.implSequenceFileLogReaderHLog.Reader实现类
hbase.regionserver.hlog.keyclassHLogKeyHLog.Entry的key实现类
hbase.regionserver.wal.enablecompressionfalse是否对日志压缩
io.file.buffer.size4096读取文件的buffer大小
hbase.regionserver.hlog.replication1复制类型
hbase.regionserver.hlog.blocksize32M文件系统块大小
hbase.regionserver.hlog.lowreplication.rolllimit5若低于副本数,尝试几次
hbase.regionserver.hlog.tolerable.lowreplication??未知
hbase.regionserver.maxlogs32最大日志个数
hbase.regionserver.logroll.multiplier0.95到HDFS块95%时回滚

hbase.regionserver.hlog.lowreplication.rolllimit

其中在Hlog.syncer方法中调用checkLowReplication方法用来判断是否hlog在hdfs上的副本数低于配置项,

若低于则requestLogRoll,最终调用logRollRequested方法,但是调用次数不超过默认5次

 

核心类HLog,负责创建读,写接口的实现,完成最终的写入数据,读数据等。

HLog.Reader  HLog读接口,实现类:SequenceFileLogReader

HLog.Writer    HLog写接口,实现类:SequenceFileLogWriter

HLog中包含了若干 HLog.Entry,Entry是一个K/V键值对

键为:HLogKey

值为:WALEdit

工具类:

    HLogSplitter(./hbase hlog命令实现类)

    Compressor(负责将input内容压缩到output中,或者input内容解压到output中)

WALActionsListener  日志的观察者监听类,当日志发生变化了可以触发观察者,比如replication功能就是实现了这个接口。 

 

HLog是一个二进制格式的文件,文件开头二进制信息:

SEQ0org.apache.hadoop.hbase.regionserver.wal.HLogKey0org.apache.hadoop.hbase.regionserver.wal.WALEdit

一个HLog文件如下:


 

 

这里注明了key和value使用的分别是HLogKey和WALEdit两个类

HLog中包含若干Entry,HLog是一个顺序的文件结构,没有索引,所有的Entry都是顺序挨个读取的。

HLog格式如下图:

 


 

HLog$Entry包含一个key,value
key:    HLogKey
value: WALEdit
Entry二进制内容如下:
1.总数据长度(hlogkey+waledit)
2.key长度
3.数据

 

HLogKey
1.变长int
2.编码后的region名字(若启用压缩则写入压缩后的内容)
3.表名(若启用压缩则写入压缩后的内容)
4.long长度的序列号
5.long长度的时间戳
6.1字节UUID标识(是否用默认的UUID标识)
7.UUID前8个字节
8.UUID后8个字节

 

WALEdit
1.int长度版本号
2.int长度KeyValue个数
3.遍历写入KeyValue(若启用压缩则写入压缩后的内容)3-1.KeyValue长度(int)3-2.KeyValue的二进制数据(4字节的key长,4字节的value长,2字节的rowkey长,key,1字节的family长,family,qualify,8字节的timestampe,1字节的keytype,变成long的memstoreMVC
4.socpe个数(若scope为null则写入0)
5.遍历写入每个scope(int长度)

 

 

HLog读取,写入的列子

	/*** 读取HLog中的内容* @throws IOException*/public void read()throws IOException {FileSystem fs = FileSystem.get(cfg);Path path = new Path("/test/hbase/hlog.1234567890");HLog.Reader reader = HLog.getReader(fs, path, cfg);HLog.Entry entry = reader.next();HLogKey key = entry.getKey();WALEdit edit = entry.getEdit();System.out.println( Bytes.toString(key.getEncodedRegionName()) );System.out.println( Bytes.toString(key.getTablename()) );System.out.println( key.getLogSeqNum() );System.out.println( key.getWriteTime() );System.out.println( key.getClusterId().toString() );List<KeyValue> list = edit.getKeyValues();for(KeyValue kv:list) {System.out.println(kv.toString());}while( (entry=reader.next()) !=null ) {System.out.println(entry);}}

 

 

 

	/*** 将数据写入到HLog,用HLog.Writer直接写入数据* @throws IOException*/public void write() throws IOException {FileSystem fs = FileSystem.get(cfg);Path path = new Path("/test/hbase/hlog.1234567890");HLog.Writer writer =  HLog.createWriter(fs, path, cfg);byte[] encodedRegionName = Bytes.toBytes("myregion");byte[] tablename = Bytes.toBytes("test");long logSeqNum = 100;long now = System.currentTimeMillis();UUID clusterId = UUID.randomUUID();HLogKey key1 = new HLogKey(encodedRegionName, tablename, logSeqNum, now, clusterId);WALEdit edit1 = new WALEdit();edit1.add(generator("key111111111111111111111111", "column", "a",System.currentTimeMillis(), new byte[] { '2' }));		HLog.Entry entry1 = new Entry(key1, edit1);HLogKey key2 = new HLogKey(encodedRegionName, tablename, 101L, now, UUID.randomUUID());WALEdit edit2 = new WALEdit();edit2.add(generator("key333333333333333333333333", "column", "b",System.currentTimeMillis(), VALUES));HLog.Entry entry2 = new Entry(key2, edit2);writer.append(entry2);writer.append(entry1);writer.sync();writer.close();}

 

	/*** 使用HLog写入数据* @throws IOException*/public void hlogWriter() throws IOException {FileSystem fs = FileSystem.get(cfg);String regionDir = "/test/hbase/hlog";HLog log = new HLog(fs, new Path(regionDir, "logs"), new Path(regionDir, "oldlogs"), cfg);byte[] tableName = Bytes.toBytes("test");byte[] startKey = Bytes.toBytes("aaaaa1111111111");byte[] endKey = Bytes.toBytes("zzzzz9999999999");HRegionInfo info = new HRegionInfo(tableName, startKey, endKey);WALEdit edit = new WALEdit();edit.add(generator("key333333333333333333333333", "column", "a",System.currentTimeMillis(), new byte[] { '2' }));edit.add(generator("key333333333333333333333333", "column", "b",System.currentTimeMillis(), VALUES));UUID uuid = UUID.randomUUID();long now = System.currentTimeMillis();HColumnDescriptor family = new HColumnDescriptor("column");HTableDescriptor descriptor = new HTableDescriptor();descriptor.addFamily(family);log.append(info, tableName, edit, uuid, now, descriptor);log.sync();log.hflush();log.hsync();log.close();}

  

	/*** 生成KeyValue* @param key* @param column* @param qualifier* @param timestamp* @param value* @return*/public KeyValue generator(String key, String column, String qualifier,long timestamp, byte[] value) {byte[] keyBytes = Bytes.toBytes(key);byte[] familyBytes = Bytes.toBytes(column);byte[] qualifierBytes = Bytes.toBytes(qualifier);Type type = Type.Put;byte[] valueBytes = value;KeyValue kv = new KeyValue(keyBytes, 0, keyBytes.length, familyBytes,0, familyBytes.length, qualifierBytes, 0,qualifierBytes.length, timestamp, type, valueBytes, 0,valueBytes.length);return kv;}

 

 

 

 

这篇关于HBase-HLog分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133494

相关文章

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory