package hadoop.mapreduce.serializable;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/*
* 自定义Hadoop序列化
* */
public class MySerializable implements Writable {
private String name;
private long up;
private long down;
public MySerializable() {
//反射时会使用
}
public MySerializable(String name, long up, long down) {
this.name = name;
this.up = up;
this.down = down;
}
public String getName() {
return name;
}
public long getUp() {
return up;
}
public long getDown() {
return down;
}
@Override
public String toString() {
return "MySerializable{" +
"up=" + up +
", down=" + down +
'}';
}
//序列化到流中
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(name);
dataOutput.writeLong(up);
dataOutput.writeLong(down);
}
//反序列化
@Override
public void readFields(DataInput dataInput) throws IOException {
//通过反射生成对象
name=dataInput.readUTF();
up= dataInput.readLong();
down= dataInput.readLong();
}
}