1

我正在使用三元运算符SUM()有条件地在操作中包含值。这是我的做法。

GROUPED = GROUP ALL_MERGED BY (fld1, fld2, fld3);

REPORT_DATA = FOREACH GROUPED
              {     GENERATE group,
                    SUM(GROUPED.fld4 == 'S' ? GROUPED.fld5 : 0) AS sum1,
                    SUM(GROUPED.fld4 == 'S' ? GROUPED.fld5 : (GROUPED.fld5 * -1)) AS sum2;
               }

架构ALL_MERGED

{ALL_MERGED: {fld1:chararray, fld2:chararray, fld3:chararray, fld4:chararray: fld5:int}}

当我执行此操作时,它给了我以下错误:

ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: Error during parsing. Invalid alias: SUM in {group: (fld1:chararray, fld2:chararray, fld3:chararray), ALL_MERGED: {fld1:chararray, fld2:chararray, fld3:chararray, fld4:chararray: fld5:int}}

我在这里做错了什么?

4

1 回答 1

2

SUM是一个以包为输入的 UDF。您正在做的事情有很多问题,我怀疑它会帮助您查看关于 Pig 的良好参考。我推荐Programming Pig,可在线免费获得。首先,GROUPED有两个字段:一个被调用的元组group和一个被调用的包ALL_MERGED,这是错误消息试图告诉你的。(我说“尝试”是因为 Pig 错误消息通常很神秘。)

此外,您不能像您希望的那样将表达式传递给 UDF。相反,您必须先访问GENERATE这些字段,然后再传递它们。尝试这个:

ALL_MERGED_2 =
    FOREACH ALL_MERGED
    GENERATE
        fld1 .. fld5,
        ((fld4 == 'S') ? fld5 : 0) AS sum_me1,
        ((fld4 == 'S') ? fld5 : fld5*-1) AS sum_me2;

GROUPED = GROUP ALL_MERGED_2 BY (fld1, fld2, fld3);
DATA =
    FOREACH GROUPED
    GENERATE
        group,
        SUM(ALL_MERGED_2.sum_me1) AS sum1,
        SUM(ALL_MERGED_2.sum_me2) AS sum2;
于 2013-01-11T15:35:53.680 回答