Apache pig 使用java UDF将pig中的包元组转换为多元组

Apache pig 使用java UDF将pig中的包元组转换为多元组,apache-pig,Apache Pig,我的数据如下: {(2000),(1800),(2700)} {(2014),(1500),(1900)} etc. 我创建了一个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();) categ

我的数据如下:

{(2000),(1800),(2700)}
{(2014),(1500),(1900)} etc.
我创建了一个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();
我的UDF将输出为:

2000
2014 etc

请说明是否有其他解决方案。请提供您的意见

其实很简单,看看这个:

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(",");
    }   
}
public类YourClass扩展了EvalFunc{
@凌驾
公共字符串exec(元组输入)引发IOException{
数据包=(数据包)输入。获取(0);
元组categoryCode=null;
//保持计算机中每个单元格的计数
Tuple auxiliary=TupleFactory.getInstance().newTuple(3);
int i=0;
for(迭代器代码=bag.Iterator();code.hasNext();){
categoryCode=code.next();
//如果从一开始就不知道,可以使用append
//元组的大小
set(i,categoryCode.get(0.toString());
i+=1;
}
返回辅助.ToDelimiteString(“,”);
}   
}

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

非常感谢您…它真的很有帮助另一个帮助…您能告诉mw如何将这些数据分隔成多个列吗?我的意思是我需要200018002700在3个不同的列中。然后,你应该在你的UDF中返回一个元组,而不是一个字符串,因为它只返回一个只有一个字段(你的字符串)的元组,否则,你也可以使用
REGEX\u EXTRACT\u ALL
将其转换成3个大小的元组,最后,如果你不需要进一步使用这个关系,您可以使用PigStorage(“,”,“-noschema”)将A存储为
存储到“输出”中我尝试过使用REGEX\u EXTRACT\u ALL(我的元组,(*)\,(*))。我收到一个错误,上面写着:“错误1000:解析过程中出错。第24行第324列的词法错误。遇到:”,“(44),在“\”(*)\”之后。请帮忙
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(",");
    }   
}