2011-12-12 45 views
1

我有一种情况,其中映射器作为键发射自定义类型的对象。 它有两个字段intWritable ID和一个数据数组IntArrayWritable。 执行如下。 `使用自定义对象作为映射器发出的键

import java.io.*; 

import org.apache.hadoop.io.*; 

public class PairDocIdPerm implements WritableComparable<PairDocIdPerm> { 

    public PairDocIdPerm(){ 
     this.permId = new IntWritable(-1); 
     this.SignaturePerm = new IntArrayWritable(); 
    } 


public IntWritable getPermId() { 
     return permId; 
    } 



    public void setPermId(IntWritable permId) { 
     this.permId = permId; 
    } 



    public IntArrayWritable getSignaturePerm() { 
     return SignaturePerm; 
    } 



    public void setSignaturePerm(IntArrayWritable signaturePerm) { 
     SignaturePerm = signaturePerm; 
    } 

    private IntWritable permId; 
    private IntArrayWritable SignaturePerm; 

    public PairDocIdPerm(IntWritable permId,IntArrayWritable SignaturePerm) { 
    this.permId = permId; 
    this.SignaturePerm = SignaturePerm; 
    } 



    @Override 
    public void write(DataOutput out) throws IOException { 
    permId.write(out); 
    SignaturePerm.write(out); 
    } 

    @Override 
    public void readFields(DataInput in) throws IOException { 
    permId.readFields(in); 
    SignaturePerm.readFields(in); 
    } 

    @Override 
    public int hashCode() { // same permId must go to same reducer. there fore just permId 
    return permId.get();//.hashCode(); 
    } 

    @Override 
    public boolean equals(Object o) { 
    if (o instanceof PairDocIdPerm) { 
    PairDocIdPerm tp = (PairDocIdPerm) o; 
    return permId.equals(tp.permId) && SignaturePerm.equals(tp.SignaturePerm); 
    } 
    return false; 
    } 

    @Override 
    public String toString() { 
    return permId + "\t" +SignaturePerm.toString(); 
    } 

    @Override 
    public int compareTo(PairDocIdPerm tp) { 
    int cmp = permId.compareTo(tp.permId); 
    Writable[] ar, other; 
    ar = this.SignaturePerm.get(); 
    other = tp.SignaturePerm.get(); 

    if (cmp == 0) { 
    for(int i=0;i<ar.length;i++){ 
     if(((IntWritable)ar[i]).get() == ((IntWritable)other[i]).get()){cmp= 0;continue;} 
     else if(((IntWritable)ar[i]).get() < ((IntWritable)other[i]).get()){ return -1;} 
     else if(((IntWritable)ar[i]).get() > ((IntWritable)other[i]).get()){return 1;} 
    } 
    } 

    return cmp; 
    //return 1; 
    } 

    }` 

我需要用相同的ID的钥匙,去同减速器与它们的排序顺序为compareTo方法编码。 但是,当我使用这个,我的工作执行状态总是map100%减少0%。减少永远不会结束。这个实现有什么错误吗? 一般来说,如果减速器状态始终为0%,可能会出现问题。

+0

日志说什么?你的工作是如何配置的? –

回答

1

我认为这可能是在读法可能空指针异常:

@Override 
    public void readFields(DataInput in) throws IOException { 
    permId.readFields(in); 
    SignaturePerm.readFields(in); 
    } 

permId在这种情况下空。 所以,你必须做的是这样的:

IntWritable permId = new IntWritable(); 

无论是在现场初始化或读之前。

但是,您的代码是可怕的阅读。

+0

这不是一个空指针异常。结果发现问题出在我的逻辑中的其他地方,而不是在这个完美工作的类代码中。 – chet

相关问题