1. 基础重写
这边直接假设我们要自定义的Writable类型叫做MyWritable
继承于Writable的class
public class MyWritable implements Writable {
// Some data
// 结构体中储存两个变量 分别是IntWritable 和 MapWritable
private IntWritable distance = new IntWritable();
private MapWritable myMap = new MapWritable();
// 构造函数
public void MyWritable() throws IOException {
this.distance = new IntWritable(0);
this.myMap = new MapWritable();
}
public void setDistance(IntWritable distance){
this.distance = distance;
}
public void setAdjList(MapWritable myMap){
this.myMap = myMap;
}
// 在自定义Writable的时候,一定要重写write readFields 和 read
public void readFields(DataInput in) throws IOException {
distance.readFields(in);
myMap.readFields(in);
}
public void write(DataOutput out) throws IOException {
distance.write(out);