用pig拉丁语生成计数值

wlwcrazw  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(333)

我正在努力找出年龄在19岁到60岁之间的用户数量。下面是示例查询

loadtable = load '/user/userdetails.txt' using PigStorage(',') AS (name:chararray,age:int);

filteredvalues = filter loadtable  by (age > 19 AND  age < 60);

grouped = GROUP filteredvalues ALL;

count = foreach grouped generate COUNT(grouped);

我得到了以下错误“无效的标量投影:分组:一个列需要从关系中投影才能用作标量”

yqyhoc1h

yqyhoc1h1#

你必须计算filteredvalue而不是分组。

total = foreach grouped generate COUNT(filteredvalues);
jucafojl

jucafojl2#

示例userdetails.txt:

Robin,85

BOB,55

Maya,23

Sara,45

David,23

Maggy,22

Robert,75

Syam,23

Mary,25

Saran,17

Stacy,19

Kelly,22

代码:

grunt> loadtable = load '/user/userdetails.txt' using PigStorage(',') AS (name:chararray,age:int);

grunt> filteredvalues = filter loadtable  by (age > 19 AND  age < 60);

grunt> grouped = GROUP filteredvalues ALL;

grunt> count = foreach grouped generate COUNT(filteredvalues);

grunt> dump count;

always count在group relation或bag之前执行,否则抛出:“invalid scalar projection:grouped:需要从关系中投影列才能用作标量”

相关问题