class MySuperKey implements WritableComparable<MySuperKey>{
Long mykey;
public MySuperKey(){
}
public MySuperKey(long mykey){
this.mykey=mykey;
}
@Override
public void readFields(DataInput in) throws IOException {
this.mykey=in.readLong();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.mykey);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.mykey.hashCode();
}
@Override
public boolean equals(Object obj) {
if(! (obj instanceof LongWritable)){
return false;
}
LongWritable lvv=(LongWritable)obj;
return (this.mykey==lvv.get());
}
@Override
public int compareTo(MySuperKey o) {
return ((int)(o.mykey-this.mykey));
}
}
由于在map函数中执行后 需要对相同的key值进行分组,但对于自己创建的对象,无法判断是否是相同的,hadoop基础类型是可以的,此时,需要实现RawComparator接口,并覆盖compare方法,并在job执行的时候,加上job.setGroupingComparatorClass(MyGroupingComparator.class);
下面是自定义的分组对象
class MyGroupingComparator implements RawComparator<MySuperKey>{
@Override
public int compare(MySuperKey o1, MySuperKey o2) {
return (int)(o1.mykey-o2.mykey);
}
@Override
public int compare(byte[] arg0, int arg1, int arg2, byte[] arg3, int arg4,
int arg5) {
return WritableComparator.compareBytes(arg0, arg1, 8, arg3, arg4, 8);
}
}