如何计算猪中不同别名数据的减法?

时间:2013-07-17 07:09:13

标签: apache-pig

dump count_a;
(10)

dump count_b;
(20)

dump count_c;
(30)

现在我要计算:count_c - count_b - count_a。如何在PIG脚本中实现这一目标?

1 个答案:

答案 0 :(得分:0)

您需要将三个别名连接在一起,为此您需要一个公共字段才能加入。假设它们都是单个记录别名,您可以创建一个字段n来加入:

prep_a = FOREACH count_a GENERATE 1 AS n, a; 
prep_b = FOREACH count_b GENERATE 1 AS n, b; 
prep_c = FOREACH count_c GENERATE 1 AS n, c; 

然后你可以加入& b& c全部由公共字段n:

ab = JOIN prep_a by n FULL, prep_b by n;
abc = JOIN ab by prep_a::n FULL, prep_c by n;

然后最终计算出最终结果:

result = FOREACH abc GENERATE (c - b - a) AS result;