hadoop自定义数据类型

Hadoop的基本数据类型是基于对Java的基本数据类型的封装,如int对应IntWritable,Long对应LongWritable。

和Java中自定义数据类型一样,某些时候我们也会在Hadoop中创建自定义数据类型。

Hadoop中自定义数据类型必须实现WritableComparable接口

举例:

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class LastOrder implements WritableComparable<LastOrder>{

	private int cust_id;
	private String cust_type;
	private String cust_email;
	
	public LastOrder(){
		
	}
	
	@Override
	public void readFields(DataInput in) throws IOException {
		
		this.cust_id = in.readInt();
		this.cust_type = in.readUTF();
		this.cust_email =in.readUTF();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		
		out.writeInt(this.cust_id);
		out.writeUTF(this.cust_type);
		out.writeUTF(this.cust_email);
		
	}


	public int compareTo(LastOrder o) {
		
		return this.cust_id-o.cust_id;
	}

	public int hashCode(){
		return super.hashCode();
	}
	
	public boolean equals(LastOrder o){
		return super.equals(o);
	}
	
	
	public String toString(){
		StringBuffer  sb= new StringBuffer();
		sb.append(cust_id);
		sb.append("\001");
		sb.append(cust_type);
		sb.append("\001");
		sb.append(cust_email);
		
		return sb.toString();
	}
	
	public int getCust_id() {
		return cust_id;
	}

	public void setCust_id(int cust_id) {
		this.cust_id = cust_id;
	}

	public String getCust_email() {
		return cust_email;
	}

}

注意:方法readFields()和write()的字段顺序必须一一对应,不然程序运行时会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值