Search code examples
hadoopapache-pig

Generate count value in Pig Latin


I am trying to find out the number of users whose age is between 19 and 60. Below is the sample queries

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);

I am getting the following error "Invalid scalar projection: grouped : A column needs to be projected from a relation for it to be used as a scalar"


Solution

  • You have to count the filteredvalues instead of grouped.

    total = foreach grouped generate COUNT(filteredvalues);