You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pig.apache.org by jamal sasha <ja...@gmail.com> on 2012/11/06 20:19:26 UTC

computing avg in pig

> I have data in format

>
>
>     1,1.2
>
>     2,1.3
>
>     and so on..
>
>
>
> So basically this is id, val combination where id is unique...
>
>
>
> I want to calculate the average of all the values..
>
>
>
>     So here.. avg(1.2,1.3)
>
>
>
> I was going thru the documentation but most of the aggregation function
involves grouping by some id.. and then using AVG... but since the id is
unique.. how do I group them???
>
> So basically the outcome of this endeavor would be one float..
>
> Any suggestions will be greatly appreciated.
>
> Thanks

Re: computing avg in pig

Posted by Alan Gates <ga...@hortonworks.com>.
A = load 'input_file';
B = group A all;
C = foreach B generate AVG(A.$1);

This groups all of your records into one bag and then takes the average of the second column.

Alan.

On Nov 6, 2012, at 11:19 AM, jamal sasha wrote:

>> I have data in format
> 
>> 
>> 
>>    1,1.2
>> 
>>    2,1.3
>> 
>>    and so on..
>> 
>> 
>> 
>> So basically this is id, val combination where id is unique...
>> 
>> 
>> 
>> I want to calculate the average of all the values..
>> 
>> 
>> 
>>    So here.. avg(1.2,1.3)
>> 
>> 
>> 
>> I was going thru the documentation but most of the aggregation function
> involves grouping by some id.. and then using AVG... but since the id is
> unique.. how do I group them???
>> 
>> So basically the outcome of this endeavor would be one float..
>> 
>> Any suggestions will be greatly appreciated.
>> 
>> Thanks