Apache PIG:仅当参数为>时才应用LIMIT。 0

时间:2013-01-22 18:38:08

标签: hadoop apache-pig

如何在foreach

内实现PIG中的以下功能
REL = foreach RELS {
 if ( cnt == 0 )
 limited_result = NULL/Empty;
 else
 limited_result = LIMIT results cnt ;

 generate limited_result.some_field;
}

我无法使用LIMIT,因为它验证了'cnt'大于0; 我尝试使用SPLIT,但foreach显然不支持。{/ p>

1 个答案:

答案 0 :(得分:2)

FILTER之前FOREACH怎么样?

REL = foreach (filter RELS by cnt > 0) {
 limited_result = LIMIT results cnt ;
 generate limited_result.some_field;
}

如果您仍需要cnt为0的记录,则可以先SPLIT,然后在cnt为0时生成一个空包:

split RELS into ZERO if cnt == 0, NONZERO if cnt > 0;

NZ_LIM = foreach NONZERO {
 result = LIMIT results cnt ;
 generate limited_result.some_field;
}
Z_LIM = foreach ZERO generate {} as some_field;

REL = union NZ_LIM, Z_LIM;