5

试图在 Pig 上完成这项工作。(寻找 MySQL 的 group_concat() 等价物)

例如,在我的表中,我有这个:(3fields- userid,clickcount,pagenumber)

155 | 2 | 12
155 | 3 | 133
155 | 1 | 144
156 | 6 | 1
156 | 7 | 5

所需的输出是:

155| 2,3,1 | 12,133,144

156| 6,7 | 1,5

我怎样才能在 PIG 上实现这一点?

4

1 回答 1

9
grouped = GROUP table BY userid;
   X = FOREACH grouped GENERATE group as userid, 
                                table.clickcount as clicksbag, 
                                table.pagenumber as pagenumberbag;

现在X将是:

{(155,{(2),(3),(1)},{(12),(133),(144)},
 (156,{(6),(7)},{(1),(5)}}

现在您需要使用内置的 UDF BagToTuple

output = FOREACH X GENERATE userid, 
                            BagToTuple(clickbag) as clickcounts, 
                            BagToTuple(pagenumberbag) as pagenumbers;

output现在应该包含你想要的。您也可以将输出步骤合并到合并步骤中:

    output = FOREACH grouped GENERATE group as userid, 
                     BagToTuple(table.clickcount) as clickcounts, 
                     BagToTuple(table.pagenumber) as pagenumbers;
于 2013-09-13T08:59:14.543 回答