Mapper的输入KV类型是由MR的InputFormat定义的,默认是偏移值和一行的数据;而输出KV类型可根据实际业务逻辑自己定义,如果该类型不包括官方提供的序列化数据类型中,则可以自己实现writable接口,必须定义一个空参构造函数,然后重写write、readFields、toString方法,其中write、readFields方法要注意序列化对象成员时的顺序必须一致,而toString方法是指定输出对象时的数据格式
package com.atguigu.mapreduce.writable;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* 1. 定义类实现Writable接口
* 2. 重写序列化和反序列化方法
* 3. 重写空参构造
* 4. toString方法
*/
public class FlowBean implements Writable {
private long upFlow; // 上行流量
private long downFlow; // 下行流量
private long sumFlow; // 总流量
// 空参构造
public FlowBean() {
}
public long getUpFlow() {
return upFlow;
}
public void setUpFlow(long upFlow) {
this.upFlow = upFlow;
}
public long getDownFlow() {
return downFlow;
}
public void setDownFlow(long downFlow) {
this.downFlow = downFlow;
}
public long getSumFlow() {
return sumFlow;
}
public void setSumFlow() {
this.sumFlow = this.upFlow + this.downFlow;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(upFlow);
out.writeLong(downFlow);
out.writeLong(sumFlow);
}
@Override
public void readFields(DataInput in) throws IOException {
this.upFlow = in.readLong();
this.downFlow = in.readLong();
this.sumFlow = in.readLong();
}
@Override
public String toString() {
return upFlow + "\t" + downFlow + "\t" + sumFlow;
}
}
|