Pull things from a bag in a pig

In the example with pigs:

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)

The last conclusion of A.name is the bag. How can I get things from a bag:

(John, 3.850000023841858)
(Mary, 3.925000011920929)
+3
source share
1 answer

GROUPCreates a magic item called the GROUPone you grouped. This is done just for this purpose.

B = GROUP A BY name;

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

Check out DESCRIBE B;, you will see what is located there GROUP. This is the only meaning that represents what was in the BY ...part GROUP.

+4
source

All Articles