Pig中GROUP BY中的条件过滤器

时间:2013-08-13 15:56:11

标签: hadoop apache-pig hadoop-streaming

我有以下数据集,如果它们具有相同的键,我需要将多行合并为一行。与此同时,我需要在多个元组中进行选择。

1 N1 1 10
1 N1 2 15
2 N1 1 10
3 N1 1 10
3 N1 2 15
4 N2 1 10
5 N3 1 10
5 N3 2 20

例如

A = LOAD 'data.txt' AS (f1:int, f2:chararray, f3:int, f4:int);
G = GROUP A BY (f1, f2);
DUMP G;

((1,N1),{(1,N1,1,10),(1,N1,2,15)})
((2,N1),{(2,N1,1,10)})
((3,N1),{(3,N1,1,10),(3,N1,2,15)})
((4,N2),{(4,N2,1,10)})
((5,N3),{(5,N3,1,10),(5,N3,2,20)})

现在,我想选择收集的包中是否有多个元组,我想只过滤那些有f3 == 2的元组。这是我想要的最终数据:

((1,N1),{(1,N1,2,15)})   -- f3==2, f3==1 is removed from this set
((2,N1),{(2,N1,1,10)})
((3,N1),{(3,N1,2,15)})   -- f3==2, f3==1 is removed from this bag
((4,N2),{(4,N2,1,10)})
((5,N3),{(5,N3,2,10)})

知道如何实现这个目标吗?

1 个答案:

答案 0 :(得分:3)

我按照上面评论中指定的方式做了。我就是这样做的。

A = LOAD 'group.txt' USING PigStorage(',') AS (f1:int, f2:chararray, f3:int, f4:int);
G = GROUP A BY (f1, f2);

CNT = FOREACH G GENERATE group, COUNT($1) AS cnt, $1;

SPLIT CNT INTO
    CNT1 IF (cnt > 1),
    CNT2 IF (cnt == 1);


M1 = FOREACH CNT1 {
        row = FILTER $2 BY (f3 == 2);
        GENERATE FLATTEN(row);
     };

M2 = FOREACH CNT2 GENERATE FLATTEN($2);

O = UNION M1, M2;

DUMP O;

(2,N1,1,10)
(4,N2,1,10)
(1,N1,2,15)
(3,N1,2,15)
(5,N3,2,20)