public class WritableComparator extends Object implements RawComparator
WritableComparable 对象的通用比较函数.
通用比较函数使用对象的自然顺序进行排序,如果用户需要自定义对象顺序,则需要重载
compare(WritableComparable, WritableComparable)方法。
如果对排序性能敏感,可以重载 compare(byte[], int, int, byte[], int, int) 方法。
| Modifier and Type | Method and Description |
|---|---|
int |
compare(byte[] b1,
int s1,
int l1,
byte[] b2,
int s2,
int l2)
本方法是
RawComparator 的低效实现,如果能提供此方法的高效实现,请重载此方法. |
int |
compare(Object a,
Object b)
重载
Comparator.compare(Object, Object) 方法,定义为 final,不允许子类重载. |
int |
compare(WritableComparable a,
WritableComparable b)
比较两个
WritableComparable 对象. |
static int |
compareBytes(byte[] b1,
int s1,
int l1,
byte[] b2,
int s2,
int l2)
逐字节比较两组二进制数据.
|
static void |
define(Class c,
WritableComparator comparator)
此静态方法用于为指定类型注册更高效的 WritableComparator 实现,否则默认使用本实现.
|
static WritableComparator |
get(Class<? extends WritableComparable> c)
此静态方法用于获取为类型 c 注册的 WritableComparator 实现.
|
Class<? extends WritableComparable> |
getKeyClass()
返回待比较的
WritableComparable 实现类 |
static int |
hashBytes(byte[] bytes,
int length)
计算二进制数据的哈希值.
|
WritableComparable |
newKey()
新建一个新的
WritableComparable 对象. |
static double |
readDouble(byte[] bytes,
int start)
从 byte 数组读取double.
|
static float |
readFloat(byte[] bytes,
int start)
从 byte 数组读取 float.
|
static int |
readInt(byte[] bytes,
int start)
从 byte 数组读取 int.
|
static long |
readLong(byte[] bytes,
int start)
从 byte 数组读取 long.
|
static int |
readUnsignedShort(byte[] bytes,
int start)
从 byte 数组读取无符号 short.
|
static int |
readVInt(byte[] bytes,
int start)
从byte数组读取压缩编码过的 int.
|
static long |
readVLong(byte[] bytes,
int start)
从byte数组读取压缩编码过的 long.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic static WritableComparator get(Class<? extends WritableComparable> c)
此方法返回通过 define(Class, WritableComparator) 方法为类型 c 注册的
WritableComparator 实现,如果没有注册,则返回 WritableComparator 这一通用实现。
c - 待比较的 WritableComparable 类型JobConf#getOutputKeyComparator()public static void define(Class c, WritableComparator comparator)
注意:只能注册线程安全的比较器,注册的对象可能在多线程场景中使用
c - 待比较的 WritableComparable 类型comparator - 更高效的 WritableComparator 实现get(Class)public Class<? extends WritableComparable> getKeyClass()
WritableComparable 实现类WritableComparable 实现类public WritableComparable newKey()
WritableComparable 对象.WritableComparable 对象public int compare(byte[] b1,
int s1,
int l1,
byte[] b2,
int s2,
int l2)
RawComparator 的低效实现,如果能提供此方法的高效实现,请重载此方法.
本方法的默认实现是:先通过 Writable.readFields(DataInput) 将二进制表示反序列化为
WritableComparable 对象,然后调用
compare(WritableComparable, WritableComparable) 进行比较。
compare in interface RawComparatorb1 - 对象1二进制内容所在的byte数组s1 - 对象1二进制内容在b1的起始位置l1 - 对象1二进制内容长度b2 - 对象2二进制内容所在的byte数组s2 - 对象2二进制内容在b2的起始位置l2 - 对象2二进制内容长度public int compare(WritableComparable a, WritableComparable b)
WritableComparable 对象.
按自然顺序比较两个 WritableComparable 对象,直接调用
Comparable.compareTo(Object).
a - 左边 WritableComparable 对象b - 右边 WritableComparable 对象public final int compare(Object a, Object b)
Comparator.compare(Object, Object) 方法,定义为 final,不允许子类重载.
compare in interface Comparatorpublic static int compareBytes(byte[] b1,
int s1,
int l1,
byte[] b2,
int s2,
int l2)
b1 - s1 - l1 - b2 - s2 - l2 - RawComparator.compare(byte[], int, int, byte[], int, int)public static int hashBytes(byte[] bytes,
int length)
bytes - byte数组length - 数据长度public static int readUnsignedShort(byte[] bytes,
int start)
bytes - 字节数组start - 起始位置,读取内容bytes[start, start+1]public static int readInt(byte[] bytes,
int start)
bytes - 字节数组start - 起始位置,读取内容bytes[start, start+3]public static float readFloat(byte[] bytes,
int start)
bytes - 字节数组start - 起始位置,读取内容bytes[start, start+3]public static long readLong(byte[] bytes,
int start)
bytes - 字节数组start - 起始位置,读取内容bytes[start, start+7]public static double readDouble(byte[] bytes,
int start)
bytes - 字节数组start - 起始位置,读取内容bytes[start, start+7]public static long readVLong(byte[] bytes,
int start)
throws IOException
bytes - 字节数组start - 起始位置,读取内容长度依 long 值的压缩编码特征而定IOExceptionpublic static int readVInt(byte[] bytes,
int start)
throws IOException
bytes - 字节数组start - 起始位置,读取内容长度依 int 值的压缩编码特征而定IOExceptionCopyright © 2024 Alibaba Cloud Computing. All rights reserved.