In pig How to concatenate all items in bag ?

安稳与你 提交于 2020-01-07 04:40:14

问题


I have a structure like

 {A, {1,2,3}}
 {B, {4,5,6}}

What I want is

 {A, "1|2|3"}
 {B, "4|5|6"}
  • I looked at CONCAT operator but that will not help me achieve what I wanted.

回答1:


This is most easily achieved with a Python UDF.

myudfs.py

#!/usr/bin/python

@outputSchema('concated: string')
def concat_bag(BAG):
    return '|'.join([ str(i) for i in BAG ])

It can be used like:

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)


来源:https://stackoverflow.com/questions/21104792/in-pig-how-to-concatenate-all-items-in-bag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!