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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:26:40+00:00 2026-06-12T00:26:40+00:00

I have a huge text file of form data is saved in directory data/data1.txt,

  • 0

I have a huge text file of form

data is saved in directory data/data1.txt, data2.txt and so on

merchant_id, user_id, amount
1234, 9123, 299.2
1233, 9199, 203.2
 1234, 0124, 230
 and so on..

What I want to do is for each merchant, find the average amount..

so basically in the end i want to save the output in file.
something like

 merchant_id, average_amount
  1234, avg_amt_1234 a
  and so on.

How do I calculate the standard deviation as well?

Sorry for asking such a basic question. 🙁
Any help 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-12T00:26:41+00:00Added an answer on June 12, 2026 at 12:26 am

    Apache PIG is well adapted for such tasks. See example:

    inpt = load '~/pig_data/pig_fun/input/group.txt' as (amnt:double, id:chararray,c2:chararray);
    grp = group inpt by id;
    mean = foreach grp {
        sum = SUM(inpt.amnt);
        count = COUNT(inpt);
        generate group as id, sum/count as mean, sum as sum, count as count;
    };
    

    Pay special attention to the data type of the amnt column as it will influence which implementation of the SUM function PIG is going to invoke.

    PIG can also do something that SQL can not, it can put the mean against each input row without using any inner joins. That is useful if you are calculating z-scores using standard deviation.

     mean = foreach grp {
        sum = SUM(inpt.amnt);
        count = COUNT(inpt);
        generate FLATTEN(inpt), sum/count as mean, sum as sum, count as count;
    };
    

    FLATTEN(inpt) does the trick, now you have access to the original amount that had contributed to the groups average, sum and count.

    UPDATE 1:

    Calculating variance and standard deviation:

    inpt = load '~/pig_data/pig_fun/input/group.txt' as (amnt:double, id:chararray, c2:chararray);
    grp = group inpt by id;
    mean = foreach grp {
            sum = SUM(inpt.amnt);
            count = COUNT(inpt);
            generate flatten(inpt), sum/count as avg, count as count;
    };
    tmp = foreach mean {
        dif = (amnt - avg) * (amnt - avg) ;
         generate *, dif as dif;
    };
    grp = group tmp by id;
    standard_tmp = foreach grp generate flatten(tmp), SUM(tmp.dif) as sqr_sum; 
    standard = foreach standard_tmp generate *, sqr_sum / count as variance, SQRT(sqr_sum / count) as standard;
    

    It will use 2 jobs. I have not figured out how to do it in one, hmm, need to spend more time on it.

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

Sidebar

Related Questions

I have to write huge data in text[csv] file. I used BufferedWriter to write
I have a huge text file and I wanted to write a program which
I have a huge text file with lines like: -568.563626 159 33 -1109.660591 -1231.295129
I have a huge text file with lots of lines like: asdasdasdaasdasd_DATA_3424223423423423 gsgsdgsgs_DATA_6846343636 .....
I have a huge text file with lots of lines like: a 23232 23232
I have a huge text file that I would like to split into multiple
I have a huge text file which I want to explode into an array.
I have an app that reads a huge text file (about more than 100MB)
I have huge text file of such format: aaa bbb 1 aaa ccc 2
I have a huge text file, size > 4GB and I want to replace

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.