Hadoop序列化-------總結

1.序列化:java

   1.1序列化(serialization)是指將內存中的對象轉化爲字節流
ide

   1.2反序列化(Deserialization)是序列化的逆過程,將字節流轉化爲內存中對象(結構化對象)。
oop

   1.3java中序列化是實現Serializable接口(java.io.serializablethis

2.Hadoop中序列化:spa

   2.1.實現Writable接口(該接口繼承了serializable接口)
對象

   2.2hadoop中序列化的做用:
排序

       1.高效的使用存儲空間
繼承

       2.快速:讀寫數據的額外開銷小
接口

       3.進程之間的通訊
進程

       4.永久存儲

   2.3hadoop節點之間的通訊

       

   節點1(消息序列化爲二進制流)---------》節點2(二進制流反序列化爲消息)

   

   2.4Writable接口

       1.是根據DataInput和DataOutput實現的簡單,有效的序列化對象

       2.MapReduce中的key,value(自定義的數據類型)必須實現Writable接口

       3.MapReduce中的key必須實現WritableComparable接口(MR默認且只能對key進行排序)

       4.常見的Writable實現類

           Writable實現          java基本類型

           Text                    String

           BooleanWritable         boolean

           ByteWritable            byte

           ....                    .....

   2.5自定義的數據類型

       

class KpiWritable implements Writable{

   Long f1;

   Long f2;

/**

* 上下字段的順序必須一致:序列化和反序列化中字段的順序

* @param in

* @throws IOException

*/

@Override

//readFiles是把輸入流字節反序列化

public void readFields(DataInput in) throws IOException {

   this.f1= in.readLong();

   this.f2= in.readLong();

}

@Override

//write是把每一個對象序列化到輸出流

public void write(DataOutput out) throws IOException {

   out.writeLong(f1);

   out.writeLong(f2);

}


}

相關文章
相關標籤/搜索