设为首页 收藏本站
查看: 593|回复: 0

[经验分享] Hadoop -【IO专题-序列化机制】

[复制链接]

尚未签到

发表于 2016-12-10 08:38:23 | 显示全部楼层 |阅读模式
引自http://blog.sina.com.cn/s/blog_48a45b950100sz4x.html
 
1. 基本概念
       序列化可被定义为将对象的状态存储到存储媒介中的过程。在此过程中,对象的公共字段和私有字段以及类的名称(包括包含该类的程序集)都被转换为字节流,然后写入数据流。在以后反序列化该对象时,创建原始对象的精确复本。
       当在面向对象的环境中实现序列化机制时,您需要在简化使用和保持灵活性之间进行许多权衡。只要您对该过程具有充分的控制,就可以在很大程度上自动化该过程。例如,在简单二进制序列化不充分时可能导致一些情况发生,或者可能有特定原因确定在类中哪些字段需要进行序列化。
       Serialization is the process of turning structured objects into a byte stream for trans-mission over a network or for writing to persistent storage. Deserialization is the process of turning a byte stream back into a series of structured objects.
       Serialization appears in two quite distinct areas of distributed data processing: forinterprocess communication and for persistent storage.
       In Hadoop, interprocess communication between nodes in the system is implemented usingremote procedure calls (RPCs). The RPC protocol uses serialization to render the message into a binary stream to be sent to the remote node, which then deserializes the binary stream into the original message. In general, it is desirable that an RPC serialization format is:
       ·  数据设计紧凑,充分利用网络带宽
       ·  系列化和反序列化的工程能够迅速完成
       ·  协议是高可扩展的
       ·  支持互操作
       Hadoop的系列化是通过Writable接口来实现的,只满足了前两条设计,在org.apache.hadoop.io包下包含了大量的可序列化的组件,它们都实现了Writable接口,Writable接口提供了两个方法,write和readFields,分别用来序列化和反序列化,实现该接口的典型例子如下:
public class MyWritable implements Writable {
    // some data
    private int counter;
    private long timestamp;
    // 序列化方法
    public void write(DataOutput out) throws IOException {
        out.writeInt(counter);
        out.writeLong(timestamp);
    }
    // 反序列化方法
    public void readFields(DataInput in) throws IOException {
        counter = in.readInt();
        timestamp = in.readLong();
    }
    // 反序列化的对外使用接口
    public static MyWritable read(DataInput in) throws IOException {
        MyWritable w = new MyWritable();
        w.readFields(in);
        return w;
    }
}
 
2. 比较器
在Map/Reduce中有多处要进行排序,所以元素的相互比较是比较重要的,Hadoop中通过如下几个接口和类来支持比较操作:
1)  WritableComparable
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
2)  RawComparator
public interface RawComparator<T> extends Comparator<T> {
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2);
}
This interface permits implementors to compare records read from a stream without
deserializing them into objects, thereby avoiding any overhead of object creation.
3)  WritableComparator
WritableComparator is a general-purpose implementation of  RawComparator for WritableComparable classes. It provides two main functions
First, it provides a default implementation of the raw compare() method that deserializes the objects to be compared from the stream and invokes the object compare() method.
Second, it acts as a factory for RawComparator instances (that Writable implementations haveregistered,通过一个HashMap<Class, WritableComparator> comparators来保持所有注册的comparator).For example, to obtain a comparator for IntWritable, we just use:
    RawComparator<IntWritable> comparator = WritableComparator.get(IntWritable.class);
    获得这个comparator之后就可以来比较两个IntWritable的对象:
    IntWritable w1 = new IntWritable(100);
    IntWritable w2 = new IntWritable(101);
    assertThat(comparator.compare(w1,w2), greatherThan(0));
    或者对序列化的对象进行比较:
    byte[] b1 = serialize(w1);
    byte[] b2 = serialize(w2);
    assertThat(comparator.compare(b1,0,b1.length, b2, 0, b2.length), greatherThan(0));
 
3. Writable Classes
Hadoop提供的可序列化的类型主要分成如下几种:
1)  Java基本数据类型 
There are  Writable wrappers  for all  the  Java primitive  types  except short and char (both of which can be stored in an IntWritable). All have a get() and a set() method for retrieving and storing the wrapped value.
Java 基本数据类型
Writable实现
Serialized size(bytes)
boolean
BooleanWritable
1
byte
ByteWritable
1
int
IntWritable
4

VIntWritable
1-5
float
FloatWritable
4
long
LongWritable
8

VLongWritable
1-9
double
DoubleWritable
8
2)  Text
Text is a Writable for UTF-8 sequences, the max length is 2GB, 这个类的系列化和反序列化都是比较显而易见的,实现上大部分代码在做 UTF-8编解码和String(java中的String用的是unicode)的转换等。
3)  BytesWritable
BytesWritable is a wrapper for an array of binary data. Its serialized format is an integerfield (4 bytes) that specifies the number of bytes to follow, followed by the bytes them-selves. For example, the byte array of length two with values 3 and 5 is serialized as a 4-byte integer (00000002) followed by the two bytes from the array (03 and 05):
    BytesWritable b = new BytesWritable(new byte[] { 3, 5 });
    byte[] bytes = serialize(b);
    assertThat(StringUtils.byteToHexString(bytes), is("000000020305"));
    BinaryComparable是针对array of binary data而设计的比较器!
4)  NullWritable
NullWritable is a special type of Writable, as it has a zero-length serialization. No bytes are written to, or read  from, the stream. It  is used as a placeholder; for example, in MapReduce, a key or a value can be declared as a NullWritable when you don’t need to use that position—it effectively stores a constant empty value.
5)  ObjectWritableGenericWritable
ObjectWritable is a general-purpose wrapper for the following: Java primitives, String, enum, Writable, null, or arrays of any of these types. It is used in Hadoop RPC to marshal(包装) and unmarshal method arguments and return types. 其实主要的通途就是对多于1个的域组成对象进行序列化!在对端进行反序列化的时候用到了WritableFactory和 WritableFactories(用来根据类名来生成对象)
6)  Writable Collections
There are four Writable collection types in the org.apache.hadoop.io package:
    ArrayWritable, TwoDArrayWritable, MapWritable, and SortedMapWritable.
ArrayWritable and  TwoDArrayWritable  are  Writable  implementations  for  arrays  and two-dimensional arrays (array of arrays) of Writable instances. All the elements of an ArrayWritable or a TwoDArrayWritable must be  instances of  the same class, which  is specified at construction, as follows:
    ArrayWritable writable = new ArrayWritable(Text.class);
MapWritable and SortedMapWritable are implementations of java.util.Map<Writable,Writable> and java.util.SortedMap<WritableComparable, Writable>, respectively. The type of each key and value field is a part of the serialization format for that field.
DSC0000.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-312121-1-1.html 上篇帖子: hadoop-error:DiskChecker$DiskErrorException: Invalid volume failure config value 下篇帖子: hadoop各种输入方法(InputFormat)汇总
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表