{"id":615,"date":"2011-03-17T11:32:58","date_gmt":"2011-03-17T03:32:58","guid":{"rendered":"http:\/\/www.strongd.net\/?p=615"},"modified":"2011-03-17T11:32:58","modified_gmt":"2011-03-17T03:32:58","slug":"hfile%e5%ad%98%e5%82%a8%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=615","title":{"rendered":"HFile\u5b58\u50a8\u683c\u5f0f"},"content":{"rendered":"<p>HBase\u4e2d\u7684\u6240\u6709\u6570\u636e\u6587\u4ef6\u90fd\u5b58\u50a8\u5728Hadoop HDFS\u6587\u4ef6\u7cfb\u7edf\u4e0a\uff0c\u4e3b\u8981\u5305\u62ec\u4e24\u79cd\u6587\u4ef6\u7c7b\u578b\uff1a<\/p>\n<p>1.\u00a0HFile\uff0c HBase\u4e2dKeyValue\u6570\u636e\u7684\u5b58\u50a8\u683c\u5f0f\uff0cHFile\u662fHadoop\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u6587\u4ef6\uff0c\u5b9e\u9645\u4e0aStoreFile\u5c31\u662f\u5bf9HFile\u505a\u4e86\u8f7b\u91cf\u7ea7\u5305\u88c5\uff0c\u5373StoreFile\u5e95\u5c42\u5c31\u662fHFile<\/p>\n<p>2.\u00a0HLog File\uff0cHBase\u4e2dWAL\uff08Write Ahead Log\uff09 \u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u7269\u7406\u4e0a\u662fHadoop\u7684Sequence File<\/p>\n<p>\u4e0b\u9762\u4e3b\u8981\u901a\u8fc7\u4ee3\u7801\u7406\u89e3\u4e00\u4e0bHFile\u7684\u5b58\u50a8\u683c\u5f0f\u3002<\/p>\n<h3>HFile<\/h3>\n<p>\u4e0b\u56fe\u662fHFile\u7684\u5b58\u50a8\u683c\u5f0f\uff1a<\/p>\n<p><a href=\"http:\/\/www.tbdata.org\/wp-content\/uploads\/2011\/02\/HFile.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.tbdata.org\/wp-content\/uploads\/2011\/02\/HFile.jpg\" alt=\"\" width=\"590\" height=\"593\" \/><\/a><\/p>\n<p>HFile\u75316\u90e8\u5206\u7ec4\u6210\u7684\uff0c\u5176\u4e2d\u6570\u636eKeyValue\u4fdd\u5b58\u5728Block 0 \u2026 N\u4e2d\uff0c\u5176\u4ed6\u90e8\u5206\u7684\u529f\u80fd\u6709\uff1a\u786e\u5b9aBlock Index\u7684\u8d77\u59cb\u4f4d\u7f6e\uff1b\u786e\u5b9a\u67d0\u4e2akey\u6240\u5728\u7684Block\u4f4d\u7f6e\uff08\u5982block index\uff09\uff1b\u5224\u65ad\u4e00\u4e2akey\u662f\u5426\u5728\u8fd9\u4e2aHFile\u4e2d\uff08\u5982Meta Block\u4fdd\u5b58\u4e86Bloom Filter\u4fe1\u606f\uff09\u3002\u5177\u4f53\u4ee3\u7801\u662f\u5728HFile.java\u4e2d\u5b9e\u73b0\u7684\uff0cHFile\u5185\u5bb9\u662f\u6309\u7167\u4ece\u4e0a\u5230\u4e0b\u7684\u987a\u5e8f\u5199\u5165\u7684\uff08Data Block\u3001Meta Block\u3001File Info\u3001Data Block Index\u3001Meta Block Index\u3001Fixed File Trailer\uff09\u3002<\/p>\n<p><strong>KeyValue: <\/strong>HFile\u91cc\u9762\u7684\u6bcf\u4e2aKeyValue\u5bf9\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684byte\u6570\u7ec4\u3002\u4f46\u662f\u8fd9\u4e2abyte\u6570\u7ec4\u91cc\u9762\u5305\u542b\u4e86\u5f88\u591a\u9879\uff0c\u5e76\u4e14\u6709\u56fa\u5b9a\u7684\u7ed3\u6784\u3002\u6211\u4eec\u6765\u770b\u770b\u91cc\u9762\u7684\u5177\u4f53\u7ed3\u6784\uff1a<\/p>\n<p><a href=\"http:\/\/www.tbdata.org\/wp-content\/uploads\/2011\/02\/KeyValue.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.tbdata.org\/wp-content\/uploads\/2011\/02\/KeyValue.jpg\" alt=\"\" width=\"553\" height=\"93\" \/><\/a><\/p>\n<p>\u5f00\u59cb\u662f\u4e24\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u5206\u522b\u8868\u793aKey\u7684\u957f\u5ea6\u548cValue\u7684\u957f\u5ea6\u3002\u7d27\u63a5\u7740\u662fKey\uff0c\u5f00\u59cb\u662f\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aRowKey\u7684\u957f\u5ea6\uff0c\u7d27\u63a5\u7740\u662f RowKey\uff0c\u7136\u540e\u662f\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aFamily\u7684\u957f\u5ea6\uff0c\u7136\u540e\u662fFamily\uff0c\u63a5\u7740\u662fQualifier\uff0c\u7136\u540e\u662f\u4e24\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aTime Stamp\u548cKey Type\uff08Put\/Delete\uff09\u3002Value\u90e8\u5206\u6ca1\u6709\u8fd9\u4e48\u590d\u6742\u7684\u7ed3\u6784\uff0c\u5c31\u662f\u7eaf\u7cb9\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4e86\u3002<\/p>\n<p><strong>Data Block:<\/strong>\u7531DATABLOCKMAGIC\u548c\u82e5\u5e72\u4e2arecord\u7ec4\u6210\uff0c\u5176\u4e2drecord\u5c31\u662f\u4e00\u4e2aKeyValue\uff08key length, value length, key, value\uff09\uff0c\u9ed8\u8ba4\u5927\u5c0f\u662f64k\uff0c\u5c0f\u7684\u6570\u636e\u5757\u6709\u5229\u4e8e\u968f\u673a\u8bfb\u64cd\u4f5c\uff0c\u800c\u5927\u7684\u6570\u636e\u5757\u5219\u6709\u5229\u4e8escan\u64cd\u4f5c\uff0c\u8fd9\u662f\u56e0\u4e3a\u8bfbKeyValue\u7684\u65f6\u5019\uff0cHBase\u4f1a\u5c06\u67e5\u8be2\u5230\u7684data block\u5168\u90e8\u8bfb\u5230Lru Block Cache\u4e2d\u53bb\uff0c\u800c\u4e0d\u662f\u4ec5\u4ec5\u5c06\u8fd9\u4e2arecord\u8bfb\u5230cache\u4e2d\u53bb\u3002<\/p>\n<p>private void append(final byte [] key, final int koffset, final int klength, final byte [] value, final int voffset, final int vlength) throws IOException {<\/p>\n<p>this.out.writeInt(klength);<\/p>\n<p>this.keylength += klength;<\/p>\n<p>this.out.writeInt(vlength);<\/p>\n<p>this.valuelength += vlength;<\/p>\n<p>this.out.write(key, koffset, klength);<\/p>\n<p>this.out.write(value, voffset, vlength);<\/p>\n<p>}<\/p>\n<p><strong>Meta Block\uff1a<\/strong>\u7531METABLOCKMAGIC\u548cBloom Filter\u4fe1\u606f\u7ec4\u6210\u3002<\/p>\n<p>public void close() throws IOException {<\/p>\n<p>if (metaNames.size() &gt; 0) {<\/p>\n<p>for (int i = 0 ; i &lt; metaNames.size() ; ++ i ) {<\/p>\n<p>dos.write(<em>METABLOCKMAGIC<\/em>);<\/p>\n<p>metaData.get(i).write(dos);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>File Info: <\/strong>\u7531MapSize\u548c\u82e5\u5e72\u4e2akey\/value\uff0c\u8fd9\u91cc\u4fdd\u5b58\u7684\u662fHFile\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u5982hfile.LASTKEY, hfile.AVG_KEY_LEN, hfile.AVG_VALUE_LEN, hfile.COMPARATOR\u3002<\/p>\n<p>private long writeFileInfo(FSDataOutputStream o) throws IOException {<\/p>\n<p>if (this.lastKeyBuffer != null) {<\/p>\n<p>\/\/ Make a copy.\u00a0 The copy is stuffed into HMapWritable.\u00a0 Needs a clean<\/p>\n<p>\/\/ byte buffer.\u00a0 Won\u2019t take a tuple.<\/p>\n<p>byte [] b = new byte[this.lastKeyLength];<\/p>\n<p>System.<em>arraycopy<\/em>(this.lastKeyBuffer, this.lastKeyOffset, b, 0, this.lastKeyLength);<\/p>\n<p><em>appendFileInfo<\/em>(this.fileinfo, FileInfo.<em>LASTKEY<\/em>, b, false);<\/p>\n<p>}<\/p>\n<p>int avgKeyLen = this.entryCount == 0? 0: (int)(this.keylength\/this.entryCount);<\/p>\n<p><em>appendFileInfo<\/em>(this.fileinfo, FileInfo.<em>AVG_KEY_LEN<\/em>, Bytes.<em>toBytes<\/em>(avgKeyLen), false);<\/p>\n<p>int avgValueLen = this.entryCount == 0? 0: (int)(this.valuelength\/this.entryCount);<\/p>\n<p><em>appendFileInfo<\/em>(this.fileinfo, FileInfo.<em>AVG_VALUE_LEN<\/em>,<\/p>\n<p>Bytes.<em>toBytes<\/em>(avgValueLen), false);<\/p>\n<p><em>appendFileInfo<\/em>(this.fileinfo, FileInfo.<em>COMPARATOR<\/em>, Bytes.<em>toBytes<\/em>(this.comparator.getClass().getName()), false);<\/p>\n<p>long pos = o.getPos();<\/p>\n<p>this.fileinfo.write(o);<\/p>\n<p>return pos;<\/p>\n<p>}<\/p>\n<p><strong>Data\/Meta Block Index:<\/strong> \u7531INDEXBLOCKMAGIC\u548c\u82e5\u5e72\u4e2arecord\u7ec4\u6210\uff0c\u800c\u6bcf\u4e00\u4e2arecord\u75313\u4e2a\u90e8\u5206\u7ec4\u6210 \u2014 block\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0cblock\u7684\u5927\u5c0f\uff0cblock\u4e2d\u7684\u7b2c\u4e00\u4e2akey\u3002<\/p>\n<p>static long writeIndex(final FSDataOutputStream o, final List&lt;byte []&gt; keys, final List&lt;Long&gt; offsets, final List&lt;Integer&gt; sizes) throws IOException {<\/p>\n<p>long pos = o.getPos();<\/p>\n<p>\/\/ Don\u2019t write an index if nothing in the index.<\/p>\n<p>if (keys.size() &gt; 0) {<\/p>\n<p>o.write(<em>INDEXBLOCKMAGIC<\/em>);<\/p>\n<p>\/\/ Write the index.<\/p>\n<p>for (int i = 0; i &lt; keys.size(); ++i) {<\/p>\n<p>o.writeLong(offsets.get(i).longValue());<\/p>\n<p>o.writeInt(sizes.get(i).intValue());<\/p>\n<p>byte [] key = keys.get(i);<\/p>\n<p>Bytes.<em>writeByteArray<\/em>(o, key);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>return pos;<\/p>\n<p>}<\/p>\n<p><strong>Fixed file trailer:<\/strong> \u5927\u5c0f\u56fa\u5b9a\uff0c\u4e3b\u8981\u662f\u53ef\u4ee5\u6839\u636e\u5b83\u67e5\u627e\u5230File Info, Block Index\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<p>public void close() throws IOException {<\/p>\n<p>trailer.fileinfoOffset = writeFileInfo(this.outputStream);<\/p>\n<p>trailer.dataIndexOffset = BlockIndex.writeIndex(this.outputStream,<\/p>\n<p>this.blockKeys, this.blockOffsets, this.blockDataSizes);<\/p>\n<p>if (metaNames.size() &gt; 0) {<\/p>\n<p>trailer.metaIndexOffset = BlockIndex.writeIndex(this.outputStream,<\/p>\n<p>this.metaNames, metaOffsets, metaDataSizes);<\/p>\n<p>}<\/p>\n<p>trailer.dataIndexCount = blockKeys.size();<\/p>\n<p>trailer.metaIndexCount = metaNames.size();<\/p>\n<p>trailer.totalUncompressedBytes = totalBytes;<\/p>\n<p>trailer.entryCount = entryCount;<\/p>\n<p>trailer.compressionCodec = this.compressAlgo.ordinal();<\/p>\n<p>trailer.serialize(outputStream);<\/p>\n<p>}<\/p>\n<p>\u6ce8\uff1a\u4e0a\u9762\u7684\u4ee3\u7801\u526a\u5207\u81eaHFile.java\u4e2d\u7684\u4ee3\u7801\uff0c\u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u67e5\u770bHbase\u6e90\u4ee3\u7801\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a href=\"http:\/\/www.searchtb.com\/2011\/01\/understanding-hbase.html\" target=\"_blank\">http:\/\/www.searchtb.com\/2011\/01\/understanding-hbase.html<\/a><\/p>\n<p><a href=\"http:\/\/th30z.blogspot.com\/2011\/02\/hbase-io-hfile.html\" target=\"_blank\">http:\/\/th30z.blogspot.com\/2011\/02\/hbase-io-hfile.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HBase\u4e2d\u7684\u6240\u6709\u6570\u636e\u6587\u4ef6\u90fd\u5b58\u50a8\u5728Hadoop HDFS\u6587\u4ef6\u7cfb\u7edf\u4e0a\uff0c\u4e3b\u8981\u5305\u62ec\u4e24\u79cd\u6587\u4ef6\u7c7b\u578b\uff1a 1.\u00a0HFile\uff0c HBase\u4e2dKeyValue\u6570\u636e\u7684\u5b58\u50a8\u683c\u5f0f\uff0cHFile\u662fHadoop\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u6587\u4ef6\uff0c\u5b9e\u9645\u4e0aStoreFile\u5c31\u662f\u5bf9HFile\u505a\u4e86\u8f7b\u91cf\u7ea7\u5305\u88c5\uff0c\u5373StoreFile\u5e95\u5c42\u5c31\u662fHFile 2.\u00a0HLog File\uff0cHBase\u4e2dWAL\uff08Write Ahead Log\uff09 \u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u7269\u7406\u4e0a\u662fHadoop\u7684Sequence File \u4e0b\u9762\u4e3b\u8981\u901a\u8fc7\u4ee3\u7801\u7406\u89e3\u4e00\u4e0bHFile\u7684\u5b58\u50a8\u683c\u5f0f\u3002 HFile \u4e0b\u56fe\u662fHFile\u7684\u5b58\u50a8\u683c\u5f0f\uff1a HFile\u75316\u90e8\u5206\u7ec4\u6210\u7684\uff0c\u5176\u4e2d\u6570\u636eKeyValue\u4fdd\u5b58\u5728Block 0 \u2026 N\u4e2d\uff0c\u5176\u4ed6\u90e8\u5206\u7684\u529f\u80fd\u6709\uff1a\u786e\u5b9aBlock Index\u7684\u8d77\u59cb\u4f4d\u7f6e\uff1b\u786e\u5b9a\u67d0\u4e2akey\u6240\u5728\u7684Block\u4f4d\u7f6e\uff08\u5982block index\uff09\uff1b\u5224\u65ad\u4e00\u4e2akey\u662f\u5426\u5728\u8fd9\u4e2aHFile\u4e2d\uff08\u5982Meta Block\u4fdd\u5b58\u4e86Bloom Filter\u4fe1\u606f\uff09\u3002\u5177\u4f53\u4ee3\u7801\u662f\u5728HFile.java\u4e2d\u5b9e\u73b0\u7684\uff0cHFile\u5185\u5bb9\u662f\u6309\u7167\u4ece\u4e0a\u5230\u4e0b\u7684\u987a\u5e8f\u5199\u5165\u7684\uff08Data Block\u3001Meta Block\u3001File Info\u3001Data Block Index\u3001Meta Block Index\u3001Fixed File Trailer\uff09\u3002 KeyValue: HFile\u91cc\u9762\u7684\u6bcf\u4e2aKeyValue\u5bf9\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684byte\u6570\u7ec4\u3002\u4f46\u662f\u8fd9\u4e2abyte\u6570\u7ec4\u91cc\u9762\u5305\u542b\u4e86\u5f88\u591a\u9879\uff0c\u5e76\u4e14\u6709\u56fa\u5b9a\u7684\u7ed3\u6784\u3002\u6211\u4eec\u6765\u770b\u770b\u91cc\u9762\u7684\u5177\u4f53\u7ed3\u6784\uff1a \u5f00\u59cb\u662f\u4e24\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u5206\u522b\u8868\u793aKey\u7684\u957f\u5ea6\u548cValue\u7684\u957f\u5ea6\u3002\u7d27\u63a5\u7740\u662fKey\uff0c\u5f00\u59cb\u662f\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aRowKey\u7684\u957f\u5ea6\uff0c\u7d27\u63a5\u7740\u662f RowKey\uff0c\u7136\u540e\u662f\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aFamily\u7684\u957f\u5ea6\uff0c\u7136\u540e\u662fFamily\uff0c\u63a5\u7740\u662fQualifier\uff0c\u7136\u540e\u662f\u4e24\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u503c\uff0c\u8868\u793aTime Stamp\u548cKey Type\uff08Put\/Delete\uff09\u3002Value\u90e8\u5206\u6ca1\u6709\u8fd9\u4e48\u590d\u6742\u7684\u7ed3\u6784\uff0c\u5c31\u662f\u7eaf\u7cb9\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4e86\u3002 Data Block:\u7531DATABLOCKMAGIC\u548c\u82e5\u5e72\u4e2arecord\u7ec4\u6210\uff0c\u5176\u4e2drecord\u5c31\u662f\u4e00\u4e2aKeyValue\uff08key length, value length, key, value\uff09\uff0c\u9ed8\u8ba4\u5927\u5c0f\u662f64k\uff0c\u5c0f\u7684\u6570\u636e\u5757\u6709\u5229\u4e8e\u968f\u673a\u8bfb\u64cd\u4f5c\uff0c\u800c\u5927\u7684\u6570\u636e\u5757\u5219\u6709\u5229\u4e8escan\u64cd\u4f5c\uff0c\u8fd9\u662f\u56e0\u4e3a\u8bfbKeyValue\u7684\u65f6\u5019\uff0cHBase\u4f1a\u5c06\u67e5\u8be2\u5230\u7684data block\u5168\u90e8\u8bfb\u5230Lru Block Cache\u4e2d\u53bb\uff0c\u800c\u4e0d\u662f\u4ec5\u4ec5\u5c06\u8fd9\u4e2arecord\u8bfb\u5230cache\u4e2d\u53bb\u3002 private void append(final byte [] key, final int koffset, &hellip; <a href=\"https:\/\/www.strongd.net\/?p=615\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HFile\u5b58\u50a8\u683c\u5f0f<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[202],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-11","tag-202"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=615"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":616,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/616"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}