我正在学习如何使用 Hadoop Pig。
如果我有这样的输入文件:
a,b,c,true
s,c,v,false
a,s,b,true
...
最后一个字段是我需要计算的...所以我想知道这个文件中有多少“真”和“假”。
我尝试:
records = LOAD 'test/input.csv' USING PigStorage(',');
boolean = foreach records generate $3;
groups = group boolean all;
现在我卡住了。我想使用:
count = foreach groups generate count('true');"
要获取“true”的数量,但我总是得到错误:
2013-08-07 16:32:36,677 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1070: Could not resolve count using imports: [, org.apache.pig.builtin., org.apache.pig.impl.builtin.] Details at logfile: /etc/pig/pig_1375911119028.log
谁能告诉我问题出在哪里?
最佳答案
两件事。首先,count
实际上应该是 COUNT
.在 pig 中,所有内置函数都应使用全大写字母调用。
其次,COUNT
计算一个包中值的数量,而不是一个值。因此,您应该按 true/false 分组,然后是 COUNT
:
boolean = FOREACH records GENERATE $3 AS trueORfalse ;
groups = GROUP boolean BY trueORfalse ;
counts = FOREACH groups GENERATE group AS trueORfalse, COUNT(boolean) ;
所以现在 counts
的 DUMP
输出将类似于:
(true, 2)
(false, 1)
如果你想要他们自己关系中的真假计数,那么你可以FILTER
counts
的输出。但是,SPLIT
可能会更好boolean
,然后做两个单独的计数:
boolean = FOREACH records GENERATE $3 AS trueORfalse ;
SPLIT boolean INTO alltrue IF trueORfalse == 'true',
allfalse IF trueORfalse == 'false' ;
tcount = FOREACH (GROUP alltrue ALL) GENERATE COUNT(alltrue) ;
fcount = FOREACH (GROUP allfalse ALL) GENERATE COUNT(allfalse) ;
关于Hadoop Pig 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18115071/