scalars can only be used with projection in PIG
Asked Answered
D

1

0

scalars can only be used with projection i am getting this error while using foreach.How can i resolved this error ? how can i use LIMIT within foreach ? please suggest some thanks in advance..

Edit (Tichdroma): Copied code from comment

A = LOAD 'part-r-00000';
G = Group A by ($0,$2 );
Y = foreach G generate FLATTEN(group), FLATTEN($1);
sorted = order Y by $0 ASC, $1 DESC;
X = foreach Y {
  lim = LIMIT sorted 3;
  generate lim;
};
Dump x;
Dander answered 2/2, 2012 at 6:44 Comment(2)
want to share the piece of code that produces this error?Harald
yea sure A = LOAD 'part-r-00000' ; G = Group A by ($0,$2 ); Y = foreach G generate FLATTEN(group), FLATTEN($1); sorted = order Y by $0 ASC, $1 DESC; X = foreach Y { lim = LIMIT sorted 3 ; generate lim ; }; Dump x;Dander
B
3

LIMIT is available in Pig 0.9 in the FOREACH nested_op.

If you want the top N element of each group, you might want to try to iterate on each one and individually sort and limit them:

A = LOAD 'part-r-00000';
G = GROUP A by ($0, $2);
X = FOREACH G {
  sorted = ORDER A by $0 ASC, $1 DESC;
  lim = LIMIT sorted 3;
  GENERATE lim;
};
DUMP X;

Notice that TOP can be efficient when you just have a column of comparable values (not in this case).

Boaster answered 2/2, 2012 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.