package org.apache.hadoop.io;
import java.io.DataOutput;
import java.io.DataInput;
import java.io.IOException;
public interface Writable {
void write(DataOutput out) throws IOException;//Serialize the fields of this object to out.
void readFields(DataInput in) throws IOException;// Deserialize the fields of this object from in.
}
writable接口是hadoop中最基本的一个接口,Map-Reduce框架中key-value类型都是通过这个接口实现,通过代码我们可以发现,该接口基于DataInput和DataOutput实现了一个简单,有效的序列化协议。
其中
DataOutput
接口用于将任意 Java 基本类型转换为一系列字节,并将这些字节写入二进制流。同时还提供了一个将String
转换成UTF-8 修改版格式并写入所得到的系列字节的工具。对于此接口中写入字节的所有方法,如果由于某种原因无法写入某个字节,则抛出IOException
。
DataInput
接口用于从二进制流中读取字节,并根据所有 Java 基本类型数据进行重构。同时还提供根据UTF-8 修改版格式的数据重构String
的工具。 对于此接口中的所有数据读取例程来说,如果在读取所需字节数之前已经到达文件末尾 (end of file),则将抛出EOFException
(IOException
的一种)。如果因为到达文件末尾以外的其他原因无法读取字节,则将抛出IOException
而不是EOFException
。尤其是,在输入流已关闭的情况下,将抛出 IOExceptio
(如有错误请指正)