如何使用Pig按键和值进行分组

时间:2015-10-22 16:40:04

标签: hadoop apache-pig bigdata hadoop-streaming

我正在使用猪,这是我要分析的文本的一部分:

SciTePress: 32    
Springer: 10    
Springer: 13    
Springer: 14    
Springer: 1571

我想要实现的是以方兴未艾的方式对文本进行排序。例如,我希望输出看起来像这样:

Springer: 1608  //( i.e. the sum of 10+13+14+1571)
SciTePress: 32

有没有办法用猪来实现这个目标?

这是我现在得到的输出:

Springer: 1571
SciTePress: 32  
Springer: 14  
Springer: 13    
Springer: 10  

这些是我使用过的命令:

    WORDS = LOAD '../filename' using PigStorage(':') AS (title: chararray, count:int);
    grpd = GROUP WORDS BY count;
    sorted = order WORDS by count desc;
    top5 = limit sorted 5;
    dump top5;

1 个答案:

答案 0 :(得分:1)

我们必须根据标题对数据进行分组,对于每个组,我们可以调用SUM函数来获得总和。

输入:

SciTePress: 32    
Springer: 10    
Springer: 13    
Springer: 14    
Springer: 1571

猪脚本:

words = LOAD '/Users/muralirao/learning/pig/a.csv'  USING PigStorage(':') AS (title: chararray, title_count:int);
grp_by_title = GROUP  words BY title;
req_data = FOREACH grp_by_title GENERATE group AS title, SUM(words.title_count) AS total_count;
req_data_ordered = ORDER req_data BY total_count;

输出:DUMP req_data_ordered

(SciTePress,32)
(Springer,1608)