Hi Kun, The book's code is for 0.20.0. In Hadoop 0.17.x WritableComparable was not generic, so you need a declaration like:
public class IntPair implements WritableComparable { } And the compareTo() method should look like this: public int compareTo(Object o) { IntPair ip = (IntPair) o; int cmp = compare(first, ip.first); if (cmp != 0) { return cmp; } return compare(second, ip.second); } Finally, if you are using Java 5 you should remove the @Override annotations. Cheers, Tom On Sun, Jun 21, 2009 at 1:16 AM, Kunsheng Chen <ke...@yahoo.com> wrote: > > 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)); > } > > } > > ------------------------------------------------ > > >