Package org.apache.hadoop.io
Class WritableComparator
java.lang.Object
org.apache.hadoop.io.WritableComparator
- All Implemented Interfaces:
Comparator,Configurable,RawComparator
- Direct Known Subclasses:
KeyFieldBasedComparator,RecordComparator
@Public
@Stable
public class WritableComparator
extends Object
implements RawComparator, Configurable
A Comparator for
WritableComparables.
This base implementation uses the natural ordering. To define alternate
orderings, override compare(WritableComparable,WritableComparable).
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.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedWritableComparator(Class<? extends WritableComparable> keyClass) Construct for aWritableComparableimplementation.protectedWritableComparator(Class<? extends WritableComparable> keyClass, boolean createInstances) protectedWritableComparator(Class<? extends WritableComparable> keyClass, Configuration conf, boolean createInstances) -
Method Summary
Modifier and TypeMethodDescriptionintcompare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) Optimization hook.intCompare two Object.intCompare two WritableComparables.static intcompareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) Lexicographic order of binary data.static voiddefine(Class c, WritableComparator comparator) Register an optimized comparator for aWritableComparableimplementation.static WritableComparatorget(Class<? extends WritableComparable> c) For backwards compatibility.static WritableComparatorget(Class<? extends WritableComparable> c, Configuration conf) Get a comparator for aWritableComparableimplementation.getConf()Return the configuration used by this object.Class<? extends WritableComparable>Returns the WritableComparable implementation class.static inthashBytes(byte[] bytes, int length) Compute hash for binary data.static inthashBytes(byte[] bytes, int offset, int length) Compute hash for binary data.newKey()Construct a newWritableComparableinstance.static doublereadDouble(byte[] bytes, int start) Parse a double from a byte array.static floatreadFloat(byte[] bytes, int start) Parse a float from a byte array.static intreadInt(byte[] bytes, int start) Parse an integer from a byte array.static longreadLong(byte[] bytes, int start) Parse a long from a byte array.static intreadUnsignedShort(byte[] bytes, int start) Parse an unsigned short from a byte array.static intreadVInt(byte[] bytes, int start) Reads a zero-compressed encoded integer from a byte array and returns it.static longreadVLong(byte[] bytes, int start) Reads a zero-compressed encoded long from a byte array and returns it.voidsetConf(Configuration conf) Set the configuration to be used by this object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
WritableComparator
protected WritableComparator() -
WritableComparator
Construct for aWritableComparableimplementation.- Parameters:
keyClass- WritableComparable Class.
-
WritableComparator
-
WritableComparator
protected WritableComparator(Class<? extends WritableComparable> keyClass, Configuration conf, boolean createInstances)
-
-
Method Details
-
get
For backwards compatibility.- Parameters:
c- WritableComparable Type.- Returns:
- WritableComparator.
-
get
Get a comparator for aWritableComparableimplementation.- Parameters:
c- class.conf- configuration.- Returns:
- WritableComparator.
-
setConf
Description copied from interface:ConfigurableSet the configuration to be used by this object.- Specified by:
setConfin interfaceConfigurable- Parameters:
conf- configuration to be used
-
getConf
Description copied from interface:ConfigurableReturn the configuration used by this object.- Specified by:
getConfin interfaceConfigurable- Returns:
- Configuration
-
define
Register an optimized comparator for aWritableComparableimplementation. Comparators registered with this method must be thread-safe.- Parameters:
c- class.comparator- WritableComparator.
-
getKeyClass
Returns the WritableComparable implementation class.- Returns:
- WritableComparable.
-
newKey
Construct a newWritableComparableinstance.- Returns:
- WritableComparable.
-
compare
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 (usingWritable.readFields(DataInput), then callscompare(WritableComparable,WritableComparable).- Specified by:
comparein interfaceRawComparator- 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.
-
compare
Compare two WritableComparables. The default implementation uses the natural ordering, callingComparable.compareTo(Object).- Parameters:
a- the first object to be compared.b- the second object to be compared.- Returns:
- compare result.
-
compare
Compare two Object.- Specified by:
comparein interfaceComparator- Parameters:
a- the first object to be compared.b- the second object to be compared.- Returns:
- compare result.
-
compareBytes
public static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) Lexicographic order of binary data.- Parameters:
b1- b1.s1- s1.l1- l1.b2- b2.s2- s2.l2- l2.- Returns:
- compare bytes.
-
hashBytes
public static int hashBytes(byte[] bytes, int offset, int length) Compute hash for binary data.- Parameters:
bytes- bytes.offset- offset.length- length.- Returns:
- hash for binary data.
-
hashBytes
public static int hashBytes(byte[] bytes, int length) Compute hash for binary data.- Parameters:
bytes- bytes.length- length.- Returns:
- hash for binary data.
-
readUnsignedShort
public static int readUnsignedShort(byte[] bytes, int start) Parse an unsigned short from a byte array.- Parameters:
bytes- bytes.start- start.- Returns:
- unsigned short from a byte array
-
readInt
public static int readInt(byte[] bytes, int start) Parse an integer from a byte array.- Parameters:
bytes- bytes.start- start.- Returns:
- integer from a byte array
-
readFloat
public static float readFloat(byte[] bytes, int start) Parse a float from a byte array.- Parameters:
bytes- bytes.start- start.- Returns:
- float from a byte array
-
readLong
public static long readLong(byte[] bytes, int start) Parse a long from a byte array.- Parameters:
bytes- bytes.start- start.- Returns:
- long from a byte array
-
readDouble
public static double readDouble(byte[] bytes, int start) Parse a double from a byte array.- Parameters:
bytes- bytes.start- start.- Returns:
- double from a byte array.
-
readVLong
Reads a zero-compressed encoded long from a byte array and returns it.- Parameters:
bytes- byte array with decode longstart- starting index- Returns:
- deserialized long
- Throws:
IOException- raised on errors performing I/O.
-
readVInt
Reads a zero-compressed encoded integer from a byte array and returns it.- Parameters:
bytes- byte array with the encoded integerstart- start index- Returns:
- deserialized integer
- Throws:
IOException- raised on errors performing I/O.
-