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()的字段顺序必须一一对应,不然程序运行时会报错。