-
Notifications
You must be signed in to change notification settings - Fork 2
/
AvroKeyComparator.java
45 lines (39 loc) · 1.41 KB
/
AvroKeyComparator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// (c) Copyright 2011 Odiago, Inc.
package org.apache.avro.mapreduce;
import org.apache.avro.Schema;
import org.apache.avro.io.BinaryData;
import org.apache.avro.mapred.AvroKey;
import org.apache.avro.specific.SpecificData;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.RawComparator;
/**
* The {@link org.apache.hadoop.io.RawComparator} used by jobs configured with
* {@link org.apache.avro.mapreduce.AvroJob}.
*
* <p>Compares AvroKeys output from the map phase for sorting.</p>
*/
public class AvroKeyComparator<T> extends Configured implements RawComparator<AvroKey<T>> {
/** The schema of the Avro data in the key to compare. */
private Schema mSchema;
/** {@inheritDoc} */
@Override
public void setConf(Configuration conf) {
super.setConf(conf);
if (null != conf) {
// The MapReduce framework will be using this comparator to sort AvroKey objects
// output from the map phase, so use the schema defined for the map output key.
mSchema = AvroJob.getMapOutputKeySchema(conf);
}
}
/** {@inheritDoc} */
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return BinaryData.compare(b1, s1, b2, s2, mSchema);
}
/** {@inheritDoc} */
@Override
public int compare(AvroKey<T> x, AvroKey<T> y) {
return SpecificData.get().compare(x.datum(), y.datum(), mSchema);
}
}