Hadoop序列化学习笔记(一)

来源:互联网 发布:淘宝猪猪的店关闭了吗 编辑:程序博客网 时间:2024/05/17 00:03
什么是序列化?
     序列化(serialization),是指将结构化对象转化为字节流,以便在网络上传输或写入磁盘进行永久存储。
     反序列化(deserialization),是指将字节流重新转换为结构化对象。

Hadoop使用哪种序列化框架?
     Hadoop使用自己的序列化格式Writable,除开Writable,Hadoop也支持Avro序列化框架。
     Writable格式紧凑,速度快。,但很难使用除开Java之外的语言对它进行扩展。
     Avro支持跨语言使用,意思是,可以用Python写入文件,而用C语言来读取文件。

Hadoop提供的Writable类型



Writable接口
package org.apache.hadoop.io;import java.io.DataOutput;import java.io.DataInput;import java.io.IOException;public interface Writable {/** * * Serialize the fields of this object to <code>out</code>. * @param out * <code>DataOuput</code> to serialize this object into. *  * @throws IOException */void write(DataOutput out) throws IOException;/** * * Deserialize the fields of this object from <code>in</code>. * * * <p> * For efficiency, implementations should attempt to re-use storage in the * * existing object where possible. * </p> * * * @param in <code>DataInput</code> to deseriablize this object from. *  * @throws IOException */void readFields(DataInput in) throws IOException;}

如何定制writable
     虽然Hadoop内建了多种Writable供用户选择,但当我们需要使用更加复杂的对象的时候,内建的Writable有可能不能满足我们的需求,这就需要我们定制自己的Writable了。
      一个定制的Writable类首先必须实现Writable或者WritableComparable接口,然后为定制的Writable类编写
write(DataOutput out)和readFields(DataInput in)方法,来控制定制的Writable类如何转化为字节流(write方法)和如何从字节流转回为Writable对象。

1 0
原创粉丝点击