Dear Wiki user, You have subscribed to a wiki page or wiki category on "Pig Wiki" for change notification.
The following page has been changed by OlgaN: http://wiki.apache.org/pig/UserDefinedOrdering ------------------------------------------------------------------------------ public int compare(Tuple t1, Tuple t2) { ... } } + }}} + + == Examples == + + === Example 1 === + + This code implements string-based comparator to produce the data in the descending order. + + {{{ + package org.apache.pig.test; + + import org.apache.pig.data.Tuple; + import org.apache.pig.ComparisonFunc; + + public class OrdDesc extends ComparisonFunc { + // this is a simple example - more complex comparison will require + // breakout of the individual values. I suggest you'll have + // to convert "catch(IOException e) to RuntimeException('msg', e)" + public int compare(Tuple t1, Tuple t2) { + return t2.compareTo(t1); + } + }}} + + === Example 2 === + + This code implements numeric comparator to produce the data in the descending order. + Note that you need a recursive compare to account for arbitrary complex tuples. + + {{{ + package org.apache.pig.test; + + import java.io.IOException; + + import org.apache.pig.data.DataAtom; + import org.apache.pig.data.Datum; + import org.apache.pig.data.Tuple; + import org.apache.pig.ComparisonFunc; + + public class OrdDescNumeric extends ComparisonFunc { + public int compare(Tuple t1, Tuple t2) { + try { + for (int i = 0; i < t1.arity(); i++) { + Datum d1 = t1.getField(i); + Datum d2 = t2.getField(i); + int comp; + if (d1 instanceof DataAtom) { + comp = compare((DataAtom)d1, (DataAtom)d2); + } else { + comp = compare((Tuple)d1, (Tuple)d2); + } + if (comp != 0) { + return comp; + } + } + return 0; + } catch (IOException e) { + throw new RuntimeException("Error comparing keys in OrdDEscNumeric", e); + } + } + + private int compare(DataAtom a1, DataAtom a2) throws IOException { + double num1 = a1.numval(); + double num2 = a2.numval(); + if (num2 > num1) { + return 1; + } else if (num2 < num1) { + return -1; + } + return 0; + } + } + }}} == Semantics ==