Return-Path: Delivered-To: apmail-incubator-pig-commits-archive@locus.apache.org Received: (qmail 77456 invoked from network); 30 Nov 2007 02:00:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 30 Nov 2007 02:00:40 -0000 Received: (qmail 77921 invoked by uid 500); 30 Nov 2007 02:00:28 -0000 Delivered-To: apmail-incubator-pig-commits-archive@incubator.apache.org Received: (qmail 77910 invoked by uid 500); 30 Nov 2007 02:00:27 -0000 Mailing-List: contact pig-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: pig-dev@incubator.apache.org Delivered-To: mailing list pig-commits@incubator.apache.org Received: (qmail 77901 invoked by uid 99); 30 Nov 2007 02:00:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Nov 2007 18:00:27 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.130] (HELO eos.apache.org) (140.211.11.130) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Nov 2007 02:00:16 +0000 Received: from eos.apache.org (localhost [127.0.0.1]) by eos.apache.org (Postfix) with ESMTP id 63CFDD2D6 for ; Fri, 30 Nov 2007 02:00:19 +0000 (GMT) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: pig-commits@incubator.apache.org Date: Fri, 30 Nov 2007 02:00:19 -0000 Message-ID: <20071130020019.4251.52853@eos.apache.org> Subject: [Pig Wiki] Update of "UserDefinedOrdering" by OlgaN X-Virus-Checked: Checked by ClamAV on apache.org 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 ==