sravani malla
sravani malla

Reputation: 47

Converting tuple of bags to Multiple tuples in pig using a java UDF

I have my data as:

{(2000),(1800),(2700)}
{(2014),(1500),(1900)} etc.

I have created a java UDF:

DataBag bag = (DataBag) top3.get(0);
Tuple categoryCode = null;
if(bag.size() == 0)
    return null;
for(Iterator<Tuple> code=bag.iterator(); code.hasNext();)
    categoryCode=code.next();
return categoryCode.get(0).toString();

I want my output to be like:

2000,1800,2700
2014,1500,1900 etc

My UDF gives me the output as:

2000
2014 etc

Please help whether there is some other solution for this. Please help with your inputs.

Upvotes: 0

Views: 1487

Answers (1)

Diego Jimeno
Diego Jimeno

Reputation: 312

It's actually pretty easy, look at that:

public class YourClass extends EvalFunc<String>{

    @Override
    public String exec(Tuple input) throws IOException {

        DataBag bag = (DataBag)input.get(0);

        Tuple categoryCode = null;

        //Keep the count of every cell in the 
        Tuple auxiliary = TupleFactory.getInstance().newTuple(3);

        int i = 0;
        for(Iterator<Tuple> code=bag.iterator(); code.hasNext();) {
            categoryCode=code.next();
            //You can use append if don't know from the very beginning
            //the size of tuple
            auxiliary.set(i, categoryCode.get(0).toString());
            i+=1;
        }

        return auxiliary.toDelimitedString(",");
    }   
}

You be better of using an auxiliary tuple to do things easier and then just use the instance method toDelimitedString(), pretty straightforward.

Upvotes: 1

Related Questions