1
我想在Titanic data上使用一些猪功能。有一次,我缩小它客车类及票价(票价):如何铸AVG功能猪场
下面的代码:
sh echo "1. create FarePclass with two fields"
FarePclass = FOREACH train GENERATE Pclass,Fare ;
DUMP FarePclass;
sh echo "2. create FareByClass grouping by Pclass"
FareByPclass = GROUP FarePclass BY Pclass ;
--FareByPclass = GROUP FarePclass ALL;
--DUMP FareByPclass;
DESCRIBE FareByPclass;
sh echo "3. get average"
AvgFareByPclass = FOREACH FareByPclass GENERATE (float) SUM(FarePclass.Fare);
下面是步骤#1从DUMP语句的一些示例行和输出:
(2,10.5)
(3,7.05)
(3,29.125)
(2,13)
(1,30)
(3,23.45)
(1,30)
(3,7.75)
2. create FareByClass grouping by Pclass
FareByPclass: {group: chararray,FarePclass: {(Pclass: chararray,Fare: chararray)}}
3. get average
2014-08-28 20:56:23,288 ERROR org.apache.pig.tools.grunt.Grunt: ERROR 1045:
<file titanic_dypler_datafu.pig, line 36, column 56> Could not infer the matching function for org.apache.pig.builtin.SUM as multiple or none of them fit. Please use an explicit cast.
我有这个脚本,我试图让最后一行运行。 AvgFareByPclass = FOREACH FareByPclass GENERATE(float)SUM(FarePclass.Fare);
尝试运行时出现此错误: 无法使用架构:bag {:tuple(Fare:chararray)}将包放置为浮动。
您能否建议如何投FarePclass.Fare?我在概念上错过了关于如何去做这件事的东西?
谢谢乔纳森,这解决了我的问题。如果有人想知道,泰坦尼克号门票的平均票价是:头等舱:84.15美元二等舱:21.66美元三等舱:13.67美元。顺便说一句,我最初的目标是计算平均值,但我决定让SUM首先工作,以防万一您注意到名称和函数调用之间的不匹配。再次感谢。 – Jazzmine 2014-08-29 01:53:49