3

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

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)})

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

4

1 回答 1

4

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

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)
于 2013-08-13T16:19:34.667 回答