[hadoop源码阅读][4]-org.apache.hadoop.io

来源:互联网 发布:中学生当街打母 知乎 编辑:程序博客网 时间:2024/04/28 19:48

转自:http://www.cnblogs.com/xuxm2007/archive/2012/06/15/2550986.html

 

1.下面是主要的类层次图

2.Writable和WritableComparable的子类们基本大同小异

 

 

3.RawComparator和WritableComparator

举例如下,以下以text类型的comparator每个字符从高到低位比较,对于数字类型的字符串也是比较适用的
/** 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类应用广泛,值得仔细看下

5.*InputBuffer和*OutputBuffer

6.Hadoop 数据类型与文件结构 Sequence, Map, Set, Array, BloomMap Files


1.Hadoop’sSequenceFile


SequenceFile 是 Hadoop 的一个重要数据文件类型,它提供key-value的存储,但与传统key-value存储(比如hash表,btree)不同的是,它是appendonly的,于是你不能对已存在的key进行写操作。每一个key-value记录如下图,不仅保存了key,value值,也保存了他们的长度。


SequenceFile 有三种压缩态:

  1. Uncompressed – 未进行压缩的状态
  2. Record Compressed- 对每一条记录的value值进行了压缩(文件头中包含上使用哪种压缩算法的信息)
  3. Block-Compressed – 当数据量达到一定大小后,将停止写入进行整体压缩,整体压缩的方法是把所有的keylength,key,vlength,value 分别合在一起进行整体压缩

文件的压缩态标识在文件开头的header数据中。

在header数据之后是一个Metadata数据,他是简单的属性/值对,标识文件的一些其他信息。Metadata 在文件创建时就写好了,所以也是不能更改的。


2.MapFile, SetFile, ArrayFile 及 BloomMapFile

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以上长度的编码效率都下降了。 
   

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 
原创粉丝点击