[ Skip to the content ]

Institute of Formal and Applied Linguistics Wiki


[ Back to the navigation ]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
courses:mapreduce-tutorial:step-27 [2012/01/28 18:59]
straka
courses:mapreduce-tutorial:step-27 [2012/01/28 20:02]
straka
Line 40: Line 40:
 } }
 </code> </code>
-Remark: If the ''BERIntWritable'' class is not declared top-level, it must be declared **//static//**.+Remark: If the ''BERIntWritable'' class is not declared top-level, it must be declared **''static''**.
  
 Such implementation can be used as a type of //values//. If we wanted to use it as a type of //keys//, we need to implement [[http://hadoop.apache.org/common/docs/r1.0.0/api/org/apache/hadoop/io/WritableComparable.html|WritableComparable]] instead of just ''Writable''. It is enough to add ''compareTo'' method to current implementation: Such implementation can be used as a type of //values//. If we wanted to use it as a type of //keys//, we need to implement [[http://hadoop.apache.org/common/docs/r1.0.0/api/org/apache/hadoop/io/WritableComparable.html|WritableComparable]] instead of just ''Writable''. It is enough to add ''compareTo'' method to current implementation:
Line 76: Line 76:
   public void setFirst(A first) { this.first = first; }   public void setFirst(A first) { this.first = first; }
   public void setSecond(B first) { this.second = second; }   public void setSecond(B first) { this.second = second; }
 +  public String toString() { return String.format("%s %s", first.toString(), second.toString()); }
   public PairWritable(A first, B second) { this.first = first; this.second = second; }   public PairWritable(A first, B second) { this.first = first; this.second = second; }
 } }
 </code> </code>
-Remark: +Remark: Remark: If the ''PairWritable'' class is not declared top-level, it must be declared **''static''**. 
 + 
 +We did not define ''compareTo'' method. The reason is that in order to do so, the types ''A'' and ''B'' would have to implement ''WritableComparable'' and the ''PairWritable'' could not be used with types not providing ''compareTo''. The best way of solving this issue is probably to create a new type ''PairWritableComparable<A, B>'' which implements ''WritableComparable'': 
 +<code java> 
 +public static class PairWritableComparable<A extends WritableComparable, B extends WritableComparable > implements WritableComparable { 
 +  private A first; 
 +  private B second; 
 + 
 +  public void readFields(DataInput in) throws IOException { 
 +    first.readFields(in); 
 +    second.readFields(in); 
 +  } 
 + 
 +  public void write(DataOutput out) throws IOException { 
 +    first.write(out); 
 +    second.write(out); 
 +  } 
 + 
 +  public int compareTo(Object other) { 
 +    PairWritableComparable<A, B> otherPair = (PairWritableComparable<A, B>) other; 
 +    int cmpFirst = first.compareTo(otherPair.getFirst()); 
 +    if (cmpFirst < 0) return -1; 
 +    if (cmpFirst > 0) return 1; 
 +    return second.compareTo(otherPair.getSecond()); 
 +  } 
 + 
 +  public A getFirst() { return first; } 
 +  public B getSecond() { return second; } 
 +  public void setFirst(A first) { this.first = first; } 
 +  public void setSecond(B first) { this.second = second; } 
 +  public String toString() { return String.format("%s %s", first.toString(), second.toString()); }   
 +  public PairWritableComparable(A first, B second) { this.first = first; this.second = second; } 
 +
 +</code> 
 +Remark: If the ''PairWritableComparable'' class is not declared top-level, it must be declared **''static''**. 

[ Back to the navigation ] [ Back to the content ]