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

  • Home
  • SEARCH
  • 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 9233451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:33:24+00:00 2026-06-18T06:33:24+00:00

Is it possible to (efficiently) select a random tuple from a bag in pig?

  • 0

Is it possible to (efficiently) select a random tuple from a bag in pig?
I can just take the first result of a bag (as it is unordered), but in my case I need a proper random selection.
One (not efficient) solution is counting the number of tuples in the bag, take a random number within that range, loop through the bag, and stop whenever the number of iterations matches my random number. Does anyone know of faster/better ways to do this?

  • 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-18T06:33:25+00:00Added an answer on June 18, 2026 at 6:33 am

    You could use RANDOM(), ORDER and LIMIT in a nested FOREACH statement to select one element with the smallest random number:

    inpt = load 'group.txt' as (id:int, c1:bytearray, c2:bytearray);
    groups = group inpt by id;
    randoms = foreach groups {
        rnds = foreach inpt generate *, RANDOM() as rnd; -- assign random number to each row in the bag
        ordered_rnds = order rnds by rnd;
        one_tuple = limit ordered_rnds 1; -- select tuple with the smallest random number
        generate group as id, one_tuple;
    };
    

    dump randoms;

    INPUT:

    1   a   r
    1   a   t
    1   b   r
    1   b   4
    1   e   4
    1   h   4
    1   k   t
    2   k   k
    2   j   j
    3   a   r
    3   e   l
    3   j   l
    4   a   r
    4   b   t
    4   b   g
    4   h   b
    4   j   d
    5   h   k
    

    OUTPUT:

    (1,{(1,b,r,0.05172709255901231)})
    (2,{(2,k,k,0.14351660053632986)})
    (3,{(3,e,l,0.0854104195792681)})
    (4,{(4,h,b,8.906013598960483E-4)})
    (5,{(5,h,k,0.6219490873384448)})
    

    If you run “dump randoms;” multiple times, you should get different results for each run.

    Writing a UDF might give you better performance as you do not need to do secondary sort on random within the bag.

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

Sidebar

Related Questions

Possible Duplicate: MySQL select 10 random rows from 600K rows fast There are many
Possible Duplicate: generating random enums I'm looking for the best way to select a
I want to select a handful of random rows from the results of a
Possible Duplicate: How do I select random rows in MySQL? Okay, so I have
Possible Duplicate: How to efficiently write to file from SQL datareader in c#? I
Im trying to shift my array left as efficiently as possible. Im using pointers
im trying to orient my code to use the cache as efficiently as possible
I'm trying to get my head around using CoffeeScript comprehensions as efficiently as possible.
I need to multiply several 1000s digits long integers as efficiently as possible in
Possible Duplicate: Best practices for efficiently storing md5 hashes in mysql We use Hex

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.