PIG中的字数

时间:2018-10-25 17:31:25

标签: hadoop hive apache-pig

假设我有一个文本文件count.txt,其中包含下面提到的段落

    I am working  in hadoop along with  various courses like Hadoop, Hana, Java etc
    I love working with hadoop
    This is hadoop project 

现在我需要获取在上面的文件中hadoop单词出现了多少次

以下代码是我尝试过的

    c1= load '/...../count.txt' using PigStorage(',') as (Name:chararray);
    c2 = foreach c1  generate FLATTEN(TOKENIZE(LOWER(Name)))as (Name1:chararray);
    dump c2;
    c3 = filter c2 by Name1=='hadoop';
    dump c3;

我得到的输出是

(hadoop)
(hadoop)
(hadoop)
(hadoop)

我需要的是数字4,而不是单词hadoop重复4次。因此我试图执行

`c4 = foreach c3 generate COUNT($0);`

并出错。.确实可以帮助我,可能是我找不到的简单事情。 预先感谢。

1 个答案:

答案 0 :(得分:1)

尝试一下:

只做一组c2:

c3 = filter c2 by Name1=='hadoop'
grouped = GROUP c3 BY Name1;
wordcount = FOREACH grouped GENERATE $0, COUNT($1);
DUMP wordcount

让我知道是否有帮助。