【Hadoop】MapReduce之自定义数据类型

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());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值