3

在猪的例子中:

A = LOAD 'student.txt' AS (name:chararray, term:chararray, gpa:float);

DUMP A;
(John,fl,3.9F)
(John,wt,3.7F)
(John,sp,4.0F)
(John,sm,3.8F)
(Mary,fl,3.8F)
(Mary,wt,3.9F)
(Mary,sp,4.0F)
(Mary,sm,4.0F)

B = GROUP A BY name;

DUMP B;
(John,{(John,fl,3.9F),(John,wt,3.7F),(John,sp,4.0F),(John,sm,3.8F)})
(Mary,{(Mary,fl,3.8F),(Mary,wt,3.9F),(Mary,sp,4.0F),(Mary,sm,4.0F)})

C = FOREACH B GENERATE A.name, AVG(A.gpa);

DUMP C;
({(John),(John),(John),(John)},3.850000023841858)
({(Mary),(Mary),(Mary),(Mary)},3.925000011920929)

最后一个输出 A.name 是一个包。我怎样才能把东西从包里拿出来:

(John, 3.850000023841858)
(Mary, 3.925000011920929)
4

1 回答 1

4

GROUP创建一个名为 的魔法物品group,这是您分组的内容。这正是为此目的而制作的。

B = GROUP A BY name;

C = FOREACH B GENERATE group AS name, AVG(A.gpa);

看看DESCRIBE B;,你会看到它group在里面。它是一个单一的值,BY ...代表GROUP.

于 2012-05-02T03:10:31.713 回答