[Solved-1 Solution] How to concatenate all items in Pig ?



What is concat()

  • The CONCAT() function of Pig Latin is used to concatenate two or more expressions of the same type.

Syntax

grunt> CONCAT (expression, expression, [...expression])

Problem:

How to concatenate all items in Pig ?

Solution 1:

  • By using python UDF we can easily achieve this

Here is a code

#!/usr/bin/python

@outputSchema('concated: string')
def concat_bag(BAG):
    return '|'.join([ str(i) for i in BAG ])
  • It can be used as the following:
Register 'myudfs.py' using jython as myfuncs;

-- Schema of A is: A:{ T:(letter: chararray, B_of_nums: {num: int}) }

B = FOREACH A GENERATE TOTUPLE(T.letter, myfuncs.concat_bag(T.B_of_nums)) ;

-- The output should be:
-- (A, 1|2|3)
-- (B, 1|2|3)

Related Searches to How to concatenate all items in pig ?