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格式如下图:

 


 


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

 


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

 


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

 

 

HLog读取,写入的列子

  1. /** 
  2.  * 读取HLog中的内容 
  3.  * @throws IOException 
  4.  */  
  5. public void read()throws IOException {  
  6.     FileSystem fs = FileSystem.get(cfg);  
  7.     Path path = new Path("/test/hbase/hlog.1234567890");  
  8.     HLog.Reader reader = HLog.getReader(fs, path, cfg);  
  9.     HLog.Entry entry = reader.next();  
  10.     HLogKey key = entry.getKey();  
  11.     WALEdit edit = entry.getEdit();  
  12.       
  13.     System.out.println( Bytes.toString(key.getEncodedRegionName()) );  
  14.     System.out.println( Bytes.toString(key.getTablename()) );  
  15.     System.out.println( key.getLogSeqNum() );  
  16.     System.out.println( key.getWriteTime() );  
  17.     System.out.println( key.getClusterId().toString() );  
  18.       
  19.     List<KeyValue> list = edit.getKeyValues();  
  20.     for(KeyValue kv:list) {  
  21.         System.out.println(kv.toString());  
  22.     }  
  23.     while( (entry=reader.next()) !=null ) {  
  24.         System.out.println(entry);  
  25.     }  
  26.       
  27. }  

 

 

 


  1. /** 
  2.  * 将数据写入到HLog,用HLog.Writer直接写入数据 
  3.  * @throws IOException 
  4.  */  
  5. public void write() throws IOException {  
  6.     FileSystem fs = FileSystem.get(cfg);  
  7.     Path path = new Path("/test/hbase/hlog.1234567890");  
  8.     HLog.Writer writer =  HLog.createWriter(fs, path, cfg);  
  9.     byte[] encodedRegionName = Bytes.toBytes("myregion");  
  10.     byte[] tablename = Bytes.toBytes("test");  
  11.     long logSeqNum = 100;  
  12.     long now = System.currentTimeMillis();  
  13.     UUID clusterId = UUID.randomUUID();  
  14.       
  15.     HLogKey key1 = new HLogKey(encodedRegionName, tablename, logSeqNum, now, clusterId);  
  16.     WALEdit edit1 = new WALEdit();  
  17.     edit1.add(generator("key111111111111111111111111""column""a",System.currentTimeMillis(), new byte[] { '2' }));        
  18.     HLog.Entry entry1 = new Entry(key1, edit1);  
  19.       
  20.     HLogKey key2 = new HLogKey(encodedRegionName, tablename, 101L, now, UUID.randomUUID());  
  21.     WALEdit edit2 = new WALEdit();  
  22.     edit2.add(generator("key333333333333333333333333""column""b",System.currentTimeMillis(), VALUES));  
  23.     HLog.Entry entry2 = new Entry(key2, edit2);  
  24.     writer.append(entry2);  
  25.     writer.append(entry1);  
  26.     writer.sync();  
  27.     writer.close();  
  28. }  

 


  1. /** 
  2.  * 使用HLog写入数据 
  3.  * @throws IOException 
  4.  */  
  5. public void hlogWriter() throws IOException {  
  6.     FileSystem fs = FileSystem.get(cfg);  
  7.     String regionDir = "/test/hbase/hlog";  
  8.     HLog log = new HLog(fs, new Path(regionDir, "logs"), new Path(regionDir, "oldlogs"), cfg);  
  9.     byte[] tableName = Bytes.toBytes("test");  
  10.     byte[] startKey = Bytes.toBytes("aaaaa1111111111");  
  11.     byte[] endKey = Bytes.toBytes("zzzzz9999999999");  
  12.     HRegionInfo info = new HRegionInfo(tableName, startKey, endKey);  
  13.   
  14.     WALEdit edit = new WALEdit();  
  15.     edit.add(generator("key333333333333333333333333""column""a",System.currentTimeMillis(), new byte[] { '2' }));  
  16.     edit.add(generator("key333333333333333333333333""column""b",System.currentTimeMillis(), VALUES));  
  17.   
  18.     UUID uuid = UUID.randomUUID();  
  19.     long now = System.currentTimeMillis();  
  20.     HColumnDescriptor family = new HColumnDescriptor("column");  
  21.     HTableDescriptor descriptor = new HTableDescriptor();  
  22.     descriptor.addFamily(family);  
  23.     log.append(info, tableName, edit, uuid, now, descriptor);  
  24.     log.sync();  
  25.     log.hflush();  
  26.     log.hsync();  
  27.     log.close();  
  28. }  

  


  1. /** 
  2.  * 生成KeyValue 
  3.  * @param key 
  4.  * @param column 
  5.  * @param qualifier 
  6.  * @param timestamp 
  7.  * @param value 
  8.  * @return 
  9.  */  
  10. public KeyValue generator(String key, String column, String qualifier,  
  11.         long timestamp, byte[] value) {  
  12.     byte[] keyBytes = Bytes.toBytes(key);  
  13.     byte[] familyBytes = Bytes.toBytes(column);  
  14.     byte[] qualifierBytes = Bytes.toBytes(qualifier);  
  15.     Type type = Type.Put;  
  16.     byte[] valueBytes = value;  
  17.     KeyValue kv = new KeyValue(keyBytes, 0, keyBytes.length, familyBytes,  
  18.             0, familyBytes.length, qualifierBytes, 0,  
  19.             qualifierBytes.length, timestamp, type, valueBytes, 0,  
  20.             valueBytes.length);  
  21.     return kv;  
  22. }  

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值