Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

group concat equivalent in pig?

Trying to get this done on Pig. (Looking for the group_concat() equivalent of MySQL)

In my table, for example, I have this: (3fields- userid, clickcount,pagenumber)

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

The desired output is:

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

156| 6,7 | 1,5

How can I achieve this on PIG?

like image 923
Vedant Avatar asked Sep 13 '13 07:09

Vedant


1 Answers

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

Now X will be:

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

Now you need to use the builtin UDF BagToTuple:

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

output should now contain what you want. You can merge the output step into the merge step as well:

    output = FOREACH grouped GENERATE group as userid, 
                     BagToTuple(table.clickcount) as clickcounts, 
                     BagToTuple(table.pagenumber) as pagenumbers;
like image 95
Hari Menon Avatar answered Oct 10 '22 09:10

Hari Menon