2017-10-12 85 views
0

這是一個基本的PIG問題。 我有我的數據是這樣的PIG集團避免袋

10 | Dog 
15 | Cow 
20 | Dog 
15 | Elephant 
15 | Dog 
25 | Elephant 

我想要找的平均重量每隻動物的,並具有輸出,因爲這:

Dog | 12.5 
Elephant | 20 
Cow | 15 

我能夠通過使用GROUP和得到的結果,但結果是一個袋子,像這樣:

{(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('|'); 

回答

0

使用group代替animal_weight.animal。注意,從您的樣本數據,狗應具有重的平均值(10 + 20 + 15)/ 3 =15公斤

results = FOREACH animal_by GENERATE group as animal_name, AVG(animal_weight.weight) as kg; 

輸出

enter image description here

+0

謝謝!我剛剛想出了12.5,應該做的數學:-) – EnnioMorricone