Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
courses:mapreduce-tutorial:step-29 [2012/01/28 20:17] straka vytvořeno |
courses:mapreduce-tutorial:step-29 [2012/02/05 18:49] straka |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== MapReduce Tutorial : Running multiple | + | ====== MapReduce Tutorial : Custom sorting and grouping comparators. ====== |
+ | |||
+ | ====== Fast sorting comparator ====== | ||
+ | |||
+ | The keys are sorted before processed by a reducer, using a | ||
+ | [[http:// | ||
+ | |||
+ | <code java> | ||
+ | public static class IntPair implements WritableComparable< | ||
+ | private int first = 0; | ||
+ | private int second = 0; | ||
+ | |||
+ | public void set(int left, int right) { first = left; second = right; } | ||
+ | public int getFirst() { return first; } | ||
+ | public int getSecond() { return second; } | ||
+ | |||
+ | public void readFields(DataInput in) throws IOException { | ||
+ | first = in.readInt(); | ||
+ | second = in.readInt(); | ||
+ | } | ||
+ | public void write(DataOutput out) throws IOException { | ||
+ | out.writeInt(first); | ||
+ | out.writeInt(second); | ||
+ | } | ||
+ | |||
+ | public int compareTo(IntPair o) { | ||
+ | if (first != o.first) return first < o.first ? -1 : 1; | ||
+ | else return second < o.second ? -1 : second == o.second ? 0 : 1; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If we would like in a Hadoop | ||
+ | |||
+ | |||
+ | |||
+ | ====== Grouping comparator ====== | ||
+ | |||
+ | In a reduce, it is guaranteed that keys are processed in ascending order. Sometimes it would be useful if the //values associated with one key// could also be processed in ascending order. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | <table style=" | ||
+ | < | ||
+ | <td style=" | ||
+ | <td style=" | ||
+ | <td style=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||