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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:51:50+00:00 2026-06-07T12:51:50+00:00

I have a question on Pig when performing what seems like two levels of

  • 0

I have a question on Pig when performing what seems like two levels of groupings. As an example, let’s say I had some example input data like:

email_id:chararray    from:chararray        to:bag{recipients:tuple(recipient:chararray)}
e1                    user1@example.com     {(friend1@example.com),(friend2@example.com),(friend3@myusers.com)}
e2                    user1@example.com     {(friend1@example.com),(friend4@example.com)}
e3                    user1@example.com     {(friend5@example.com)}
e4                    user2@example.com     {(friend2@example.com),(friend4@example.com)}

So each line is an email from a user “from” to user(s) “to”.

And I ultimately want a list of all senders and all the people they’ve sent emails to, including the # of emails sent for each person, sorted from highest to lowest, for example:

user1@example.com     {(friend1@example.com, 2), (friend2@example.com, 1), (friend3@example.com, 1), (friend4@example.com, 1), (friend5@example.com, 1)}
user2@example.com     {(friend2@example.com, 1), (friend4@example.com, 1)}

Ideas on the best way to tackle this in Pig would be appreciated!

  • 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-07T12:51:53+00:00Added an answer on June 7, 2026 at 12:51 pm

    Here is one version of the script:

    inpt = load '/pig_data/pig_fun/input/from_senders.txt' as (email_id:chararray, from:chararray, to:bag{recipients:tuple(recipient:chararray)});
    
    pivot = foreach inpt generate from, FLATTEN(to);
    pivot = foreach pivot generate from, to::recipient as recipient;
    dump pivot;
    /*
    (user1@example.com,friend1@example.com)
    (user1@example.com,friend2@example.com)
    (user1@example.com,friend3@myusers.com)
    (user1@example.com,friend1@example.com)
    (user1@example.com,friend4@example.com)
    (user1@example.com,friend5@example.com)
    (user2@example.com,friend2@example.com)
    (user2@example.com,friend4@example.com)
    */
    
    grp = group pivot by (from, recipient);
    with_count = foreach grp generate FLATTEN(group), COUNT(pivot) as count;
    dump with_count;
    /*
    (user1@example.com,friend1@example.com,2)
    (user1@example.com,friend2@example.com,1)
    (user1@example.com,friend3@myusers.com,1)
    (user1@example.com,friend4@example.com,1)
    (user1@example.com,friend5@example.com,1)
    (user2@example.com,friend2@example.com,1)
    (user2@example.com,friend4@example.com,1)
    */
    
    to_bag = group with_count by from;
    result = foreach to_bag {
        order_by_count = order with_count by count desc;
        generate group as from, order_by_count.(recipient, count);
    };
    dump result;
    /*
    (user1@example.com,{(friend1@example.com,2),(friend2@example.com,1),(friend3@myusers.com,1),(friend4@example.com,1),(friend5@example.com,1)})
    (user2@example.com,{(friend2@example.com,1),(friend4@example.com,1)})
    */
    

    Hope it helps.

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

Sidebar

Related Questions

I have question concerning a function I created. I would like to show the
I have question to Java or C# programmers. I want to render some pages
I have question regarding polymorphism about its assignment statement, for Example this is The
i have question about two different Bundle object in below methods : onSaveInstanceState(Bundle outState);
I have question about 301 Redirect please: example: http://stackapp.com/post/35642 to: http://google.com/posts/index.php?q=http://stackapp.com/post/35642 Note: I want
I have question about global variable in Objective-C. I have two .m files :
I have question on which I can't find any answear. Example: In _root exists
I am new to Hadoop/PIG. I have a basic question. Do we have a
I have question about parsing in Html helper : I have sth like: @foreach
I have question. I have some app on facebook and getting this error Fatal

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.