SequenceFile 是Hadoop 的一个基础数据文件格式,后续讲的 MapFile, SetFile, ArrayFile 及 BloomMapFile 都是基于它来实现的。
- MapFile – 一个key-value 对应的查找数据结构,由数据文件/data 和索引文件 /index 组成,数据文件中包含所有需要存储的key-value对,按key的顺序排列。索引文件包含一部分key值,用以指向数据文件的关键位置。
- SetFile – 基于 MapFile 实现的,他只有key,value为不可变的数据。
- ArrayFile – 也是基于 MapFile 实现,他就像我们使用的数组一样,key值为序列化的数字。
- BloomMapFile – 他在 MapFile 的基础上增加了一个 /bloom 文件,包含的是二进制的过滤表,在每一次写操作完成时,会更新这个过滤表
-
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 */ /* * 将一个long类型的i,写入输出流DataOutput中 * 如果 -112 <= i <= 127,只使用一个byte表示i并写入输出流中 * 第一个字节表示i的正负和接下来表示i的字节数 * 如果第一个字节-113 <= v <= -120,那么i是正数,并且接下来i占的字节数是-(v+112)(也就是1到8个字节之间) * 如果第一个字节-121 <= v <= -128,那么i是负数,并且接下来的i占的字节数是-(v+120)(也就是1到8个字节之间) * 写入时先写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)); } }
这种编码方式的有点是照顾了绝大多数能够使用一个byte编码的数字,最大的缺点是,两个byte所能编码的数字少了很多,并且两个byte以上长度的编码效率都下降了。