MapReduce之自定义数据类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zkzbhh/article/details/79003465

Int ——>IntWritable
Long——->LongWritable
String——>Text
Double—->DoubleWritable
Float——->FloatWritable
Null——–>MullWritable


开发步骤
 1、创建属性和以及getter、setter和构造方法

//创建一个PariWritable的类
//属性
private String stuId;
private String stuName;
//getter,setter方法
public String getStuId() {
    return stuId;
}
public void setStuId(String stuId) {
    this.stuId = stuId;
}
public String getStuName() {
    return stuName;
}
public void setStuName(String stuName) {
    stuName = stuName;
}
//无参的构造方法
public PariWritable() {

}
//有参数的构造方法
public PariWritable(String stuId, String stuName){
    this.stuId = stuId;
    stuName = stuName;
}

 2、重写equals()、hashCode()、toString()方法。

//equals()方法
public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof PariWritable)) 
    return false;
  PariWritable that = (PariWritable) o;
  if (!getStuId().equals(that.getStuId())) 
        return false;
  return getStuName().equals(that.getStuName());
}

//hashCode()方法
public int hashCode() {
    int result = getStuId().hashCode();
    result = 31 * result + getStuName().hashCode();
    return result;
}

//toString()方法
public String toString() {
    return stuId + '\t' +stuName;
}

3、实现WritableComparable接口(org.apache.hadoop.io.WritableComparable)。

public class PariWritable implements   WritableComparable<PariWritable> { }

4、重写write()、readFields()、compareTo()方法

//重写write()方法
public void write(DataOutput dataputput) throws IOException{
    dataOutput.writeUTF(this.cid);
    dataOutput.writeUTF(this.ctel);
    dataOutput.writeUTF(this.netType);
    dataOutput.writeUTF(this.caddress);
    dataOutput.writeLong(this.ctimeLong);
    dataOutput.writeLong(this.upCount);
    dataOutput.writeLong(this.downCount);
}

//重写readFields()方法
public void readFields(DataInput dataInput) throws IOException{
    this.cid=dataInput.readUTF();
    this.ctel=dataInput.readUTF();
    this.netType=dataInput.readUTF();
    this.caddress=dataInput.readUTF();
    this.ctimeLong=dataInput.readLong();
    this.upCount=dataInput.readLong();
    this.downCount=dataInput.readLong();
}

//重写compareTo()方法
public int compareTo(CustomerWritable o){
    return this.cid.compareTo(o.getCid());
}

猜你喜欢

转载自blog.csdn.net/zkzbhh/article/details/79003465
今日推荐