Skip to content

Instantly share code, notes, and snippets.

@grf
Created September 21, 2011 03:20
Show Gist options
  • Save grf/1231154 to your computer and use it in GitHub Desktop.
Save grf/1231154 to your computer and use it in GitHub Desktop.
p1b
package net.sacred;
import java.io.*;
import org.apache.hadoop.io.*;
// Create a pair of Text objects that we can emit from a map function.
// From example 4.7 in Hadoop, The Definitive Guide. O'Reilly 2010
public class TextPair implements WritableComparable<TextPair> {
private Text first;
private Text second;
public TextPair() {
set(new Text(), new Text());
}
public TextPair(String first, String second) {
set(new Text(first), new Text(second));
}
public TextPair(Text first, Text second) {
set(first, second);
}
public void set(Text first, Text second) {
this.first = first;
this.second = second;
}
public Text getFirst() {
return first;
}
public Text getSecond() {
return second;
}
@Override
public void write(DataOutput out) throws IOException {
first.write(out);
second.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
first.readFields(in);
second.readFields(in);
}
@Override
public int hashCode() {
return first.hashCode() * 163 + second.hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof TextPair) {
TextPair tp = (TextPair) o;
return first.equals(tp.first) && second.equals(tp.second);
}
return false;
}
@Override
public String toString() {
return first + " " + second;
}
@Override
public int compareTo(TextPair tp) {
int cmp = first.compareTo(tp.first);
if (cmp != 0) {
return cmp;
}
return second.compareTo(tp.second);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment