syker
syker

Reputation: 11272

Joining a Bag of Tuples into a Single String without any Custom Pig UDFs

How do I concatenate a bag of tuples (ex: {(664),(823),(8),(47),(35),(27),(13),(16),(4),(8),(6)}) into a single string with Pig Latin that does not rely on writing a custom UDF?

Upvotes: 3

Views: 3066

Answers (2)

nadavw
nadavw

Reputation: 51

this is an example for a UDF concatenating strings which are tuples in a bag assuming there is 1 item in each tuple (string delimiter in the output will be an '_'):

#!/usr/bin/python

@outputSchema("schema:chararray")
def convertBagToStr(acctBag):
    return "_".join([str(i[0]) for i in sorted(acctBag)])

in the Pig script :

register '$udf_dir/myUDF.py' using jython as funcs;

a = foreach mygroup generate funcs.convertBagToStr(mybag) as bag_str;

Upvotes: 3

cyang
cyang

Reputation: 5694

There is no way to do this in just Pig Latin. According to Programming Pig, bags are unordered collections of tuples. Thus, trying to concatenate them gives an ambiguous result unless you were able to specify an ordering—which is possible through a custom UDF.

Upvotes: 2

Related Questions