序列化
1 > 什么是序列化?
将结构化对象转换成字节流以便于进行网络传输或写入持久存储的过程。
2 > 什么是反序列化?
将字节流转换为一系列结构化对象的过程。
3 > 序列化用途?
1,一种作为持久化格式
2,一种作为通信的数据格式
3,作为一种数据拷贝,克隆机制。
Java的序列化和反序列化
1,创建一个对象实现了Serializable
2,序列化:ObjectOutputStream.writeObject(序列化对象)
反序列化:ObjectInputStream.readObject()返回序列化对象
为什么Hadoop的不直接使用的Java序列化?
Hadoop的的序列化机制与Java的的序列化机制不同,它将对象序列化到流中,值得一提的是Java的的序列化机制是不断的创建对象,但在Hadoop的的序列化机制中,用户可以复用对象,这样就减少了的java对象的分配和回收,提高了应用效率。
Hadoop的序列化
Hadoop的的序列化不采用的Java的序列化,而是实现了自己的序列化机制。
Hadoop的通过Writable接口实现的序列化机制,不过没有提供比较功能,所以和Java的的中Comparable接口合并,提供一个接口WritableComparable。
【实例文件】
DataBean.java
public class DataBean implements Writable{
private String tel;
private long upPayLoad;
private long downPayLoad;
private long totalPayLoad;
public DataBean(){}
public DataBean(String tel, long upPayLoad, long downPayLoad) {
super();
this.tel = tel;
this.upPayLoad = upPayLoad;
this.downPayLoad = downPayLoad;
this.totalPayLoad = upPayLoad + downPayLoad;
}
@Override
public String toString() {
return this.upPayLoad + "\t" + this.downPayLoad + "\t" + this.totalPayLoad;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(tel);
out.writeLong(upPayLoad);
out.writeLong(downPayLoad);
out.writeLong(totalPayLoad);
}
@Override
public void readFields(DataInput in) throws IOException {
this.tel = in.readUTF();
this.upPayLoad = in.readLong();
this.downPayLoad = in.readLong();
this.totalPayLoad = in.readLong();
}
//getter setter方法
}
InfoBean.java
public class InfoBean implements WritableComparable<InfoBean> {
private String account;//key邮箱
private double income;//收入
private double expenses;//支出
private double surplus;//结余
//set方法(含参数构造方法)
public void set(String account,double income,double expenses){
this.account = account;
this.income = income;
this.expenses = expenses;
this.surplus = income - expenses;
}
//反序列化
public void write(DataOutput out) throws IOException {
out.writeUTF(account);
out.writeDouble(income);
out.writeDouble(expenses);
out.writeDouble(surplus);
}
//序列化
public void readFields(DataInput in) throws IOException {
this.account = in.readUTF();
this.income = in.readDouble();
this.expenses = in.readDouble();
this.surplus = in.readDouble();
}
@Override
public int compareTo(InfoBean o) {
//先比较收入,当输入相等
if(this.income == o.getIncome()){
//比支出
return this.expenses > o.getExpenses() ? 1 : -1;
}
return this.income > o.getIncome() ? 1 : -1;
}
@Override
public String toString() {
return income + "\t" + expenses + "\t" + surplus;
}
//getter setter方法
}