PIG 集团 by avoid Bag
PIG Group by avoid Bag
这是一个基本的 PIG 问题。
我的数据是这样的
10 | Dog
15 | Cow
20 | Dog
15 | Elephant
15 | Dog
25 | Elephant
我想找出每只动物的平均体重并得到如下输出:
Dog | 12.5
Elephant | 20
Cow | 15
我可以使用 GROUP by 并得到结果,但结果是一个包,像这样:
{(Dog), (Dog) } | 12.5
{(Elephant), (Elephant)} | 20
{(Cow)} | 15
我怎样才能只提取单个动物?
我是这样使用GROUP的。
--animal_weight is derived through other means
animal_by = GROUP animal_weight by (animal);
results = FOREACH animal_by GENERATE animal_weight.animal as animal_name, AVG(animal_weight.weight) as kg;
STORE results INTO '$output_4' USING PigStorage('|');
使用group
而不是animal_weight.animal
。请注意,根据您的示例数据,狗的平均体重应为 (10+20+15)/3 = 15 公斤
results = FOREACH animal_by GENERATE group as animal_name, AVG(animal_weight.weight) as kg;
输出
这是一个基本的 PIG 问题。 我的数据是这样的
10 | Dog
15 | Cow
20 | Dog
15 | Elephant
15 | Dog
25 | Elephant
我想找出每只动物的平均体重并得到如下输出:
Dog | 12.5
Elephant | 20
Cow | 15
我可以使用 GROUP by 并得到结果,但结果是一个包,像这样:
{(Dog), (Dog) } | 12.5
{(Elephant), (Elephant)} | 20
{(Cow)} | 15
我怎样才能只提取单个动物?
我是这样使用GROUP的。
--animal_weight is derived through other means
animal_by = GROUP animal_weight by (animal);
results = FOREACH animal_by GENERATE animal_weight.animal as animal_name, AVG(animal_weight.weight) as kg;
STORE results INTO '$output_4' USING PigStorage('|');
使用group
而不是animal_weight.animal
。请注意,根据您的示例数据,狗的平均体重应为 (10+20+15)/3 = 15 公斤
results = FOREACH animal_by GENERATE group as animal_name, AVG(animal_weight.weight) as kg;
输出