org.apache.hadoop.io
生活随笔
收集整理的這篇文章主要介紹了
org.apache.hadoop.io
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.下面是主要的類層次圖
2.Writable和WritableComparable的子類們基本大同小異
?
3.RawComparator和WritableComparator
舉例如下,以下以text類型的comparator每個(gè)字符從高到低位比較,對(duì)于數(shù)字類型的字符串也是比較適用的 /** A WritableComparator optimized for Text keys. */ public static class Comparator extends WritableComparator {public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2){int n1 = WritableUtils.decodeVIntSize(b1[s1]);int n2 = WritableUtils.decodeVIntSize(b2[s2]);return compareBytes(b1, s1 + n1, l1 - n1, b2, s2 + n2, l2 - n2);} }4.Text類應(yīng)用廣泛,值得仔細(xì)看下
5.*InputBuffer和*OutputBuffer
6.Hadoop 數(shù)據(jù)類型與文件結(jié)構(gòu) Sequence, Map, Set, Array, BloomMap Files
1.Hadoop’s?SequenceFile
SequenceFile 是 Hadoop 的一個(gè)重要數(shù)據(jù)文件類型,它提供key-value的存儲(chǔ),但與傳統(tǒng)key-value存儲(chǔ)(比如hash表,btree)不同的是,它是appendonly的,于是你不能對(duì)已存在的key進(jìn)行寫操作。每一個(gè)key-value記錄如下圖,不僅保存了key,value值,也保存了他們的長度。
SequenceFile 有三種壓縮態(tài):
文件的壓縮態(tài)標(biāo)識(shí)在文件開頭的header數(shù)據(jù)中。
在header數(shù)據(jù)之后是一個(gè)Metadata數(shù)據(jù),他是簡單的屬性/值對(duì),標(biāo)識(shí)文件的一些其他信息。Metadata 在文件創(chuàng)建時(shí)就寫好了,所以也是不能更改的。
2.MapFile, SetFile, ArrayFile 及 BloomMapFile
SequenceFile 是Hadoop 的一個(gè)基礎(chǔ)數(shù)據(jù)文件格式,后續(xù)講的 MapFile, SetFile, ArrayFile 及 BloomMapFile 都是基于它來實(shí)現(xiàn)的。
- MapFile?– 一個(gè)key-value 對(duì)應(yīng)的查找數(shù)據(jù)結(jié)構(gòu),由數(shù)據(jù)文件/data 和索引文件 /index 組成,數(shù)據(jù)文件中包含所有需要存儲(chǔ)的key-value對(duì),按key的順序排列。索引文件包含一部分key值,用以指向數(shù)據(jù)文件的關(guān)鍵位置。
- SetFile?– 基于 MapFile 實(shí)現(xiàn)的,他只有key,value為不可變的數(shù)據(jù)。
- ArrayFile?– 也是基于 MapFile 實(shí)現(xiàn),他就像我們使用的數(shù)組一樣,key值為序列化的數(shù)字。
- BloomMapFile?– 他在 MapFile 的基礎(chǔ)上增加了一個(gè) /bloom 文件,包含的是二進(jìn)制的過濾表,在每一次寫操作完成時(shí),會(huì)更新這個(gè)過濾表
-
7.值得提一下binary stream with zero-compressed encoding
/** * Serializes a long to a binary stream with zero-compressed encoding. * For -112 <= i <= 127, only one byte is used with the actual value. * For other values of i, the first byte value indicates whether the * long is positive or negative, and the number of bytes that follow. * If the first byte value v is between -113 and -120, the following long * is positive, with number of bytes that follow are -(v+112). * If the first byte value v is between -121 and -128, the following long * is negative, with number of bytes that follow are -(v+120). Bytes are * stored in the high-non-zero-byte-first order. * * @param stream Binary output stream * @param i Long to be serialized * @throws java.io.IOException */ /* * 將一個(gè)long類型的i,寫入輸出流DataOutput中 * 如果 -112 <= i <= 127,只使用一個(gè)byte表示i并寫入輸出流中 * 第一個(gè)字節(jié)表示i的正負(fù)和接下來表示i的字節(jié)數(shù) * 如果第一個(gè)字節(jié)-113 <= v <= -120,那么i是正數(shù),并且接下來i占的字節(jié)數(shù)是-(v+112)(也就是1到8個(gè)字節(jié)之間) * 如果第一個(gè)字節(jié)-121 <= v <= -128,那么i是負(fù)數(shù),并且接下來的i占的字節(jié)數(shù)是-(v+120)(也就是1到8個(gè)字節(jié)之間) * 寫入時(shí)先寫i的高位,再寫低位 * */ public static void writeVLong(DataOutput stream, long i) throws IOException { if (i >= -112 && i <= 127) { stream.writeByte((byte)i); return; } int len = -112; if (i < 0) { i ^= -1L; // take one's complement' len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } stream.writeByte((byte)len); len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; stream.writeByte((byte)((i & mask) >> shiftbits)); } } 這種編碼方式的有點(diǎn)是照顧了絕大多數(shù)能夠使用一個(gè)byte編碼的數(shù)字,最大的缺點(diǎn)是,兩個(gè)byte所能編碼的數(shù)字少了很多,并且兩個(gè)byte以上長度的編碼效率都下降了。?8.參考url
http://hadoop.apache.org/common/docs/r0.20.2/api/index.html http://blog.csdn.net/ludi7125/article/details/7605719 http://www.cloudera.com/blog/2011/01/hadoop-io-sequence-map-set-array-bloommap-files/ http://blog.nosqlfan.com/html/1217.html http://jerrylead.iteye.com/blog/1181716??http://www.itivy.com/arch/archive/2011/12/12/hadoop-writable-interface-introduction.html
總結(jié)
以上是生活随笔為你收集整理的org.apache.hadoop.io的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDFS初探之旅
- 下一篇: hadoop的hdfs文件操作实现上传文