通过pig脚本计算平均值

piztneat  于 2021-06-21  发布在  Pig
关注(0)|答案(2)|浏览(423)
/*calculating average for itemssold(int) grouped by city */

a = LOAD 'sales.txt' USING PigStorage(','); /*loading sales data and it has 50 fields that are comma separated*/ 
b = FOREACH a GENERATE $3 as city:chararray, $4 as itemssold:int;/*defining schema for needed fields*/
c = GROUP b BY city; /*grouping by city*/
d = FOREACH c GENERATE group,AVG(b.itemssold); /*calculating average*/
dump d; /*writing output*/

在这里,我试图计算按城市分组的项目的平均值。
错误:计算平均值时出错。
有人能帮我纠正这个错误吗?
注意:由于sales.txt有50个用逗号分隔的字段,我不想在将sales.txt加载到关系本身时为所有字段定义模式。

gxwragnw

gxwragnw1#

a = LOAD 'sales.txt' USING PigStorage(',');
b = FOREACH a GENERATE (chararray) $3 as city, (int) $4 as itemssold;
c = GROUP b BY city; 
d = FOREACH c GENERATE group,AVG(b.itemssold); 
dump d;

上面的代码起作用了。在关系bgenerate中,我试图定义模式而不是强制转换,因此pig感到困惑。现在纠正了这一点&它奏效了。谢谢大家的建议。

ogq8wdun

ogq8wdun2#

可能您的数据包含某些缺少的值,请先尝试按以下方式对其进行筛选:

no_nulls = FILTER b BY itemssold is not null;

相关问题