subhrendu
subhrendu

Reputation: 157

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"

Upvotes: 0

Views: 1100

Answers (2)

Mahesh Gupta
Mahesh Gupta

Reputation: 1892

Sample 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

Code:

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 is performed before group relation or bag otherwise it throws: "Invalid scalar projection: grouped : A column needs to be projected from a relation for it to be used as a scalar"


Upvotes: 1

nobody
nobody

Reputation: 11080

You have to count the filteredvalues instead of grouped.

total = foreach grouped generate COUNT(filteredvalues);

Upvotes: 2

Related Questions