Hello everyone,

I am writing my own Comparator inherits from WritableComparable.

I got the folliowing code from "Hadoop definitive guide", which is not working 
at all, it reminds me "WritableComparable does not take parameter". The book 
might be using Hadoop-0.21 

I also tried the old method for 0.18 version as below:

http://hadoop.apache.org/core/docs/r0.18.3/api/org/apache/hadoop/io/WritableComparable.html

but it will reminds me "hasn't implement compareTo method", which actually I 
did.

I am wondering if I have to reinstall the hadoop again (I prefer not) or there 
was any old way to do it.


Any idea is well appreciated!

-Kun

--------------------------------------------------

import java.io.*;

import org.apache.hadoop.io.*;

public class IntPair implements WritableComparable<IntPair> {

  private int first;
  private int second;
  private Text third;
 
  public IntPair(int first, int second, Text third) {
    set(first, second, third);
  }
  
  public void set(int first, int second, Text third) {
    this.first = first;
    this.second = second;
    this.third = third;
  }
  
  public int getFirst() {
    return first;
  }

  public int getSecond() {
    return second;
  }

  public Text getThird() {

    return third;
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeInt(first);
    out.writeInt(second);
    third.write(out);
  }

  @Override
  public void readFields(DataInput in) throws IOException {
    first = in.readInt();
    second = in.readInt();
    // Redundant
    third.readFields(in);

  }
  
  @Override
  public int hashCode() {
    return first * 163 + second + third.hashCode();
  }
  
  @Override
  public boolean equals(Object o) {
    if (o instanceof IntPair) {
      IntPair ip = (IntPair) o;
      return first == ip.first && second == ip.second && third.equals(ip.third);
    }
    return false;
  }
  @Override
  public String toString() {
    return first + "\t" + second + "\t" + third;
  }
  
  @Override
  public int compareTo(IntPair ip) {
    int cmp = compare(first, ip.first);
    if (cmp != 0) {
      return cmp;
    }
    return compare(second, ip.second);
  }
  
  /**
   * Convenience method for comparing two ints.
   */
  public static int compare(int a, int b) {
    return (a < b ? -1 : (a == b ? 0 : 1));
  }
  
}

------------------------------------------------


      

Reply via email to