Pig - ERROR 1045: AVG as multiple or none of them fit. Please use an explicit cast
Asked Answered
B

1

8

I have a comma seperated .txt file, I want to DUMP the AVG age of all Males.

records = LOAD 'file:/home/gautamshaw/Documents/PigDemo_CommaSep.txt' USING PigStorage(',') AS (firstname:chararray,lastname:chararray,age:int,sex:chararray);
filter_by_male = FILTER records BY sex == 'M';
grouped = GROUP filter_by_male ALL;
average_male_age = FOREACH grouped GENERATE AVG(records.age);

I am getting an error in the FOREACH line:

ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1045: 
<line 6, column 44> Could not infer the matching function for org.apache.pig.builtin.AVG as multiple or none of them fit. Please use an explicit cast.

Please advice.

Brana answered 30/1, 2015 at 1:27 Comment(0)
T
9

You should not project records relation it should be filter_by_male relation.

Can you change your script like this?

average_male_age = FOREACH grouped GENERATE AVG(filter_by_male.age);
Titanothere answered 30/1, 2015 at 1:38 Comment(1)
You grouped your data from filter_by_male relation not from records relation. You can type "describe grouped" to see the result. It will clearly tell the relation name that you need to use for any aggregate operations.Titanothere

© 2022 - 2024 — McMap. All rights reserved.