WritableComparable接口
Writable接口大家可能都知道,它是一個實現了序列化協議的序列化對象。在Hadoop中定義一個結構化對象都要實現Writable接口,使得該結構化對象可以序列化為字節流,字節流也可以反序列化為結構化對象。那WritableComparable接口是可序列化並且可比較的接口。MapReduce中所有的key值類型都必須實現這個接口,既然是可序列化的那就必須得實現readFiels()和write()這兩個序列化和反序列化函數,既然也是可比較的那就必須得實現compareTo()函數,該函數即是比較和排序規則的實現。這樣MR中的key值就既能可序列化又是可比較的。下面幾符圖是API中對WritableComparable接口的解釋及其方法,還有一個實現了該接口的對象的列子:
public interface WritableComparable<T>
extends
Writable,
Comparable<T>
A Writable which is alsoComparable.
WritableComparables can be compared to each other, typically via Comparators. Any type which is to be used as a key in the Hadoop Map-Reduce framework should implement this interface.
Example:
public class MyWritableComparable implements WritableComparable {
// Some data
private int counter;
private long timestamp;
public void write(DataOutput out) throws IOException {
out.writeInt(counter);
out.writeLong(timestamp);
}
public void readFields(DataInput in) throws IOException {
counter = in.readInt();
timestamp = in.readLong();
}
public int compareTo(MyWritableComparable w) {
int thisValue = this.value;
int thatValue = ((IntWritable)o).value;
return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
}
}
