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 6926947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:59:04+00:00 2026-05-27T10:59:04+00:00

There are account ids , each with a timestamp grouped by username . foreach

  • 0

There are account ids, each with a timestamp grouped by username. foreach of these username groups I want all pairs of (oldest account, other account).

I have a java reducer that does that, can I rewrite it as a simple pig script?

Schema:

{group:(username),A: {(id , create_dt)}

Input:

(batman,{(id1,100), (id2,200), (id3,50)})
(lulu  ,{(id7,100), (id9,50)})

Desired output:

(batman,{(id3,id1), (id3,id2)})
(lulu  ,{(id9,id7)})
  • 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-05-27T10:59:04+00:00Added an answer on May 27, 2026 at 10:59 am

    Not that anyone seems to care, but here goes. You have to create a UDF:

    desired =   foreach my_input generate group as n, FIND_PAIRS(A) as pairs_bag;
    

    And the UDF:

    public class FindPairs extends EvalFunc<DataBag> {
    @Override
        public DataBag exec(Tuple input) throws IOException {
            Long pivotCreatedDate = Long.MAX_VALUE;
            Long pivot = null;
    
            DataBag accountsBag = (DataBag) input.get(0);
            for (Tuple account : accountsBag){
                Long accountId = Long.parseLong(account.get(0).toString());
                Long creationDate = Long.parseLong(account.get(4).toString());
                if (creationDate < pivotCreatedDate ) {
                    // pivot is the one with the minimal creation_dt
                    pivot = accountId;
                    pivotCreatedDate = creationDate;
                }
            }
    
            DataBag allPairs = BagFactory.getInstance().newDefaultBag();
            if (pivot != null){
                for (Tuple account : accountsBag){
                    Long accountId = Long.parseLong(account.get(0).toString());
                    Long creationDate = Long.parseLong(account.get(4).toString());
                    if (!accountId.equals(pivot)) {
                        // we don't want any self-pairs
                        Tuple output = TupleFactory.getInstance().newTuple(2);
                        if (pivot < accountId){
                                output.set(0, pivot.toString());
                                output.set(1, accountId.toString());
                        }
                        else {
                      output.set(0, accountId.toString());
                        output.set(1, pivot.toString());
                        }
                    allPairs.add(output);
                }
            }               
            return allPairs;
    }
    

    and if you wanna play real nicely, add this:

    /**
     * Letting pig know that we emit a bag with tuples, each representing a pair of accounts
     */
    @Override
    public Schema outputSchema(Schema input) {
        try{
            Schema pairSchema = new Schema();
            pairSchema.add(new FieldSchema(null, DataType.BYTEARRAY));
            pairSchema.add(new FieldSchema(null, DataType.BYTEARRAY));
            return new Schema(
                    new FieldSchema(null,
                    new Schema(pairSchema), DataType.BAG));         
        }catch (Exception e){
                return null;
        }
    }   
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having this For each loop foreach (Account acct in acctTest) { if
I am trying to select the IDs based on period and type of account.Each
Is there a way to monitor a gmail account using imaplib without polling gmail
Is there a way to login my hotmail account and send mails with a
Is there a .NET API available to get data from your XBox Live account?
How can I get the Gmail contact of my account in ASP.NET. Is there
I'm trying to install python to a 1and1.com shared linux hosting account. There is
There are millions of user accounts, and I want to distribute their data into
I have a MySQL database in which each user has an account, and each
Is there a specific range of Event IDs in Windows reserved for application developers?

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.