您可以在猪中使用STORE的三元运算符吗?

时间:2014-09-16 15:02:51

标签: mapreduce apache-pig ternary-operator

在apache pig中,如果我想有条件地存储一些数据,我会尝试这样做:

data1 = ....;
data2 = ....;
STORE (condition ? data1 : data2) INTO '$output' USING PigStorage(",");
--assuming pig is smart enough not to run the query for data1 or data2 depending on the condition

然后我收到语法错误:

SEVERE: exception during parsing: Error during parsing. <file test.pig, line 38, column 6>  Syntax error, unexpected symbol at or near '('
Failed to parse: <file test.pig, line 38, column 6>  Syntax error, unexpected symbol at or near '('

我在猪中使用三元运算符是不正确的,如果这是不可能的,还有另一种方法可以在猪中实现条件存储,最好不要编写UDF。

1 个答案:

答案 0 :(得分:0)

您不能在STORE语句中使用三元操作,因为您正试图在问题中进行操作。 您可以将条件列添加到data1和data2,然后获取UNION,然后根据条件值过滤UNION数据。

data1 = ....
data1a = CROSS data1, condition;

data2 = ....
data2a = CROSS data2, condition;

data12 = UNION data1a, data2a;
final = FILTER data12 BY condition == true;

STORE final INTO '$output' USING PigStorage(",");

希望这有帮助。