使用Pig条件运算符实现还是?

时间:2015-03-31 21:12:08

标签: apache-pig conditional-statements conditional-operator

我们说我有一些表f,包括以下列:

a, b
0, 1
0, 0
0, 0
0, 1
1, 0
1, 1

我想创建一个等于a的新列c湾

我尝试过以下方法:

f = foreach f generate a, b, ((a or b) == 1) ? 1 : 0 as c;

但收到以下错误:

ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: Pig script failed to parse: NoViableAltException(91@[])

1 个答案:

答案 0 :(得分:2)

OR条件构造不正确,你能试试吗?

f = foreach f generate a, b, (((a==1) or (b==1))?1:0) AS c;

示例示例:
输入

0,1
0,0
0,0
0,1
1,0
1,1

<强> PigScript:

A = LOAD 'input' USING PigStorage(',') AS (a:int,b:int);
B = foreach A generate a, b, (((a==1) or (b==1))?1:0) AS c;
DUMP B;

<强>输出:

(0,1,1)
(0,0,0)
(0,0,0)
(0,1,1)
(1,0,1)
(1,1,1)