hadoop8-序列化

序列化

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章