使用javaudf将包的元组转换为pig中的多元组

x6yk4ghg  于 2021-06-25  发布在  Pig
关注(0)|答案(1)|浏览(312)

我的数据如下:

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

我创建了一个java自定义项:

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();

我希望我的输出像:

2000,1800,2700
2014,1500,1900 etc

我的自定义项将输出为:

2000
2014 etc

请告诉我是否有其他解决办法。请帮忙输入。

vof42yt1

vof42yt11#

其实很简单,看看这个:

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(",");
    }   
}

最好使用辅助元组来简化操作,然后只使用instance方法 toDelimitedString() ,非常简单。

相关问题