【问题标题】:How to group by key and value using Pig如何使用 Pig 按键和值分组
【发布时间】:2015-10-22 16:40:04
【问题描述】:

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

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;

【问题讨论】:

    标签: hadoop apache-pig bigdata hadoop-streaming


    【解决方案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)
    

    【讨论】:

    • 谢谢!这就是总和;但是我也希望输出按计数排序。我使用了我在问题中使用的相同命令,但这次没有排序:sorted = order WORDS by count desc; 有什么建议吗?
    • @OmarAlejandroChacinOrtega :更新的答案,按 ASC(默认)顺序的 total_count 字段排序,如果您需要降序使用 DESC 关键字。如果您需要按标题排序,请使用标题字段而不是 total_count。参考:pig.apache.org/docs/r0.12.0/basic.html#order-by
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    相关资源
    最近更新 更多