hadoop8-序列化

来源:互联网 发布:武汉淘宝产品拍摄公司 编辑:程序博客网 时间:2024/06/05 01:05

序列化

1)序列化和反序列化的概念

序列化是将结构化对象转换成为字节流以便于进行网络传输或写入持久存储的过错

反序列化是将字节流转换成为一系列结构化对象的过程

序列化的用途

1)作为一种数据持久化格式

2)作为一种通信的数据格式

3)作为一种数据拷贝或者克隆机制

序列化的特征:紧凑/快速/可扩展/互操作

java的序列化和反序列化

1)创建一个对象实现Serializable

2)序列化:ObjectOutStream.writeObject(序列化对象)

反序列化:ObjectInputStream.readObject()返回序列化对象

import java.io.Serializable;

class TestSerial implements Serializable{

            public byte version = 100;

            public byte count = 0;

}

FileInputStream fis = new FileInputStream("temp.out");

ObjectInputStream oin = new ObjectInputStream(fis);

TestSerial ts = (TestSerial) oin.readObject();


FileOutputStream fis = new FileOutputStream("temp.out");

ObjectOutputStream oos = new ObjectOutputStream(fis);

TestSerial ts =  new TestSerial();

oos.writeObject(ts);


hadoop序列化

hadoop的序列化没有采用java的序列化,而是实现了自己的序列化方式,因为java序列化比较繁琐,生成的文件多


hadoop通过Writable接口实现的序列化机制,不过没有提供比较功能,所以和java中的Comparable接口合并,提供一个

接口WiritableComparable


Writable接口提供两个方法,write和readFiles

public interface Writable{

     void write(DataOutput out) throws IOException;

     void readFiles(DataInput in) throws IOException;

}


public interface WritableComparable<T> extends Writable,Comparable<T>
{

}


Hadoop

0 0