Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:15:22+00:00 2026-06-01T09:15:22+00:00

I’m trying to write a Java UDF that will rank tuples in a bag

  • 0

I’m trying to write a Java UDF that will rank tuples in a bag using a java UDF.
The tuples have a value column that is the criteria for the ranking and a rank column which is initially set to 0.
The tuples are sorted based on the value column.
All the tuples are placed in a bag and that bag is placed inside a new tuple which is passed to the UDF.

The UDF is modifying the rank column however – once the method exits the values have all become 0 again. I’m not sure how to get the values to “Stick”.

Any help would greatly appreciated.

Here is my java class

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.pig.FilterFunc;
import org.apache.pig.EvalFunc;
import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.DataBag;
import org.apache.pig.impl.logicalLayer.FrontendException;
import java.util.Iterator;
import org.apache.pig.PigWarning;

/**
 *
 * @author Winter
 */
public class Ranker  extends EvalFunc<String>{
    @Override
    public String exec(Tuple tuple) throws IOException {
        if (tuple == null || tuple.size() == 0) {
            return null;
        }


        List<Object> list = tuple.getAll();
        DataBag db = (DataBag) list.get(0);
        Integer num = (Integer)list.get(1);

        Iterator<Tuple>itr = db.iterator();
        boolean containsNonNull = false;
        int i = 1;
        double previous=0;
        while (itr.hasNext()) {

            Tuple t= itr.next();
            double d = (Double)t.get(num.intValue());
            int rankCol = t.size()-1;
            Integer rankVal = (Integer)t.get(rankCol);
            if(i == 0){    
                System.out.println("i==0");
                previous = d;
                t.set(rankCol, i);
            } else {
                if(d == previous)
                    t.set(rankCol, i);
                else{
                    System.out.print("d!==previous|" + d + "|"+ previous+"|"+rankVal);
                    t.set(rankCol, ++i);
                    rankVal = (Integer)t.get(rankCol);
                     System.out.println("|now rank val" + rankVal);
                    previous = d;
                }
            }
        }


        return "Y";
    }
}

Here is how I am calling everything in Pig –

REGISTER /myJar.jar;
A = LOAD '/Users/Winter/milk-tea-coffee.tsv'  as (year:chararray, milk:double);
B = foreach A generate year, milk, 0 as rank;
C = order B by milk asc; 
D = group C by rank order C by milk;
E = foreach D generate D.C.year,D.C.milk,D.C.rank,  piglet3.evalFunctions.Ranker(D.C,1);
dump E;

I can tell its working inside the UDF because of the print statements inside the UDF –
d!==previous|21.2|0.0|0|now rank val2
d!==previous|21.6|21.2|0|now rank val3
d!==previous|21.9|21.6|0|now rank val4
d!==previous|22.0|21.9|0|now rank val5
d!==previous|22.5|22.0|0|now rank val6
d!==previous|22.9|22.5|0|now rank val7
d!==previous|23.0|22.9|0|now rank val8
d!==previous|23.4|23.0|0|now rank val9
d!==previous|23.8|23.4|0|now rank val10
d!==previous|23.9|23.8|0|now rank val11

but when I dump out E or D or C the rank column only contains 0s.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:15:23+00:00Added an answer on June 1, 2026 at 9:15 am

    The exec function must return the output you want from the UDF. You are currently modifying the Tuple that is being passed to the exec function, then returning the String “Y” — all that Pig see’s as output from your UDF is “Y”. In this case, you should return the Tuple instead of “Y”.

    I think the following code is close to your intent, but I’m not quite clear on what you are trying to do:

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.pig.FilterFunc;
    import org.apache.pig.EvalFunc;
    import org.apache.pig.backend.executionengine.ExecException;
    import org.apache.pig.data.DataType;
    import org.apache.pig.data.Tuple;
    import org.apache.pig.data.DataBag;
    import org.apache.pig.impl.logicalLayer.FrontendException;
    import java.util.Iterator;
    import org.apache.pig.PigWarning;
    
    /**
     *
     * @author Winter
     */
    public class Ranker  extends EvalFunc<Tuple>{
        @Override
        public Tuple exec(Tuple tuple) throws IOException {
            if (tuple == null || tuple.size() == 0) {
                return null;
            }
    
    
            List<Object> list = tuple.getAll();
            DataBag db = (DataBag) list.get(0);
            Integer num = (Integer)list.get(1);
    
            Iterator<Tuple>itr = db.iterator();
            boolean containsNonNull = false;
            int i = 1;
            double previous=0;
            while (itr.hasNext()) {
    
                Tuple t= itr.next();
                double d = (Double)t.get(num.intValue());
                int rankCol = t.size()-1;
                Integer rankVal = (Integer)t.get(rankCol);
                if(i == 0){    
                    System.out.println("i==0");
                    previous = d;
                    t.set(rankCol, i);
                } else {
                    if(d == previous)
                        t.set(rankCol, i);
                    else{
                        System.out.print("d!==previous|" + d + "|"+ previous+"|"+rankVal);
                        t.set(rankCol, ++i);
                        rankVal = (Integer)t.get(rankCol);
                         System.out.println("|now rank val" + rankVal);
                        previous = d;
                    }
                }
            }
    
    
            return tuple;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.