Hadoop
源码阅读之WritableComparator
类
1. 类简介
-
All Implemented Interfaces: Comparator, Configurable, RawComparator
- 包名:
org.apache.hadoop.io.WritableComparator
2. 类释义
A Comparator for WritableComparables.
WritableComparables
的比较器
This base implemenation uses the natural ordering. To define alternate orderings, override compare(WritableComparable).
- 使用
natural ordering
的基本实现。 如果要实现自定义排序,那么请覆写compare()
【来自WritbaleComparable
类的方法】
One may optimize compare-intensive operations by overriding compare(byte[],int,int,byte[],int,int). Static utility methods are provided to assist in optimized implementations of this method.
- 可以通过重写
compare(byte[],int,int,byte[],int,int)
来优化比较密集型操作。提供静态实用程序方法来帮助优化此方法的实现。
3. 类方法
compare
方法compare
方法有好几种不同的实现,以下逐一讲解:
public int compare(WritableComparable a,
WritableComparable b)
Compare two WritableComparables.
The default implementation uses the natural ordering, calling Comparable.compareTo(Object).
public int compare(byte[] b1,
int s1,
int l1,
byte[] b2,
int s2,
int l2)
Optimization hook. Override this to make SequenceFile.Sorter's scream.
The default implementation reads the data into two WritableComparables (using Writable.readFields(DataInput), then calls compare(WritableComparable,WritableComparable).
Specified by:
compare in interface RawComparator
Parameters:
b1 - The first byte array.
s1 - The position index in b1. The object under comparison's starting index.
l1 - The length of the object in b1.
b2 - The second byte array.
s2 - The position index in b2. The object under comparison's starting index.
l2 - The length of the object under comparison in b2.
Returns:
An integer result of the comparison.
public int compare(Object a,
Object b)
Specified by:
compare in interface Comparator