You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-user@hadoop.apache.org by Kunsheng Chen <ke...@yahoo.com> on 2009/06/21 02:16:26 UTC
Looking for correct way to implements WritableComparable in Hadoop-0.17
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));
}
}
------------------------------------------------
Re: Looking for correct way to implements WritableComparable in
Hadoop-0.17
Posted by Tom White <to...@cloudera.com>.
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));
> }
>
> }
>
> ------------------------------------------------
>
>
>