2

我有一个非常简单的 2 列数据,带有一个 double 和一个 chararray:

user1 234.43
user1 432.23
user2 4321.213
etc.

我想按用户分组,然后计算他们双打的平均值。如何?我需要“GROUP * ALL”吗?我正在尝试遵循示例 2 http://wiki.apache.org/pig/PigOverview,但它对我不起作用。

selfReportsAndDiscrepancies = FOREACH discrepancies1 GENERATE discrepancy,selfReportedText;
perDiscrepancy = GROUP selfReportsAndDiscrepancies BY selfReportedText;

allDiscrep = group perDiscrepancy all;

means = FOREACH allDiscrep GENERATE AVG(perDiscrepancy.discrepancy);

DUMP means;
DESCRIBE means;

给我:

2013-04-02 17:54:06,611 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1128: Cannot find field discrepancy in group:chararray,selfReportsAndDiscrepancies:bag{:tuple(discrepancy:double,selfReportedText:chararray)}
4

1 回答 1

2

我希望我理解正确,你想要组平均值的平均值:

VISITS = LOAD 'data' USING PigStorage(' ')  AS (user:chararray, number:double);
USER_VISITS = GROUP VISITS BY user;
USER_AVG = FOREACH USER_VISITS GENERATE group AS user, AVG(VISITS.number) AS average;
ALL_AVG = GROUP USER_AVG ALL;
OVERALL_AVG = FOREACH ALL_AVG GENERATE AVG(USER_AVG.average);
DUMP OVERALL_AVG;

结果是:

(2327.2715)
于 2013-04-03T07:33:51.913 回答