13

我是 PIG 的新手,想计算我的一列数据的平均值,看起来像

0
10.1
20.1
30
40
50
60
70
80.1

我写了这个猪脚本

dividends = load 'myfile.txt' as (A);
dump dividends
grouped   = group dividends by A;
avg       = foreach grouped generate AVG(grouped.A);
dump avg

它将数据解析为

(0)
(10.1)
(20.1)
(30)
(40)
(50)
(60)
(70)
(80.1)

但给出这个错误的平均值

2013-03-04 15:10:58,289 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: Pig script failed to parse: 
<file try.pig, line 4, column 41> Invalid scalar projection: grouped
Details at logfile: /Users/PreetiGupta/Documents/CMPS290S/project/pig_1362438645642.log

任何的想法

4

3 回答 3

22

AVG内置函数将一个包作为输入。在您的group声明中,您目前正在按 的值对元素进行分组A,但您真正想要做的是将所有元素分组到一个包中。

Pig'sGROUP ALL是您想要使用的:

dividends = load 'myfile.txt' as (A);
dump dividends
grouped   = group dividends all;
avg       = foreach grouped generate AVG(dividends.A);
dump avg
于 2013-03-05T01:25:47.290 回答
5

下面将用于计算平均值:

dividends = load 'myfile.txt' as (A);
grouped   = GROUP dividends all;
avg       = foreach grouped generate AVG(dividends);
dump avg
于 2013-03-06T07:41:34.870 回答
1

您必须使用原始数据变量名称,而不是使用组变量。在行中FOREACH,我使用AVG(dividends.A)而不是AVG(grouped.A). 这是解决方案脚本:

dividends = load 'myfile.txt' as (A);
dump dividends

grouped   = group dividends by A;
avg = foreach grouped generate AVG(dividends.A);
dump avg
于 2021-06-06T04:10:02.373 回答