2013-11-20 51 views
0

我有我的数据在猪转换的包包多的元组的元组为:使用Java UDF

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

我希望我的输出要像:

2000,1800,2700 
2014,1500,1900 etc 

我UDF给我的输出:

2000 
2014 etc 

请帮助是否有其他解决方案。请帮助你的投入。

回答

1

这其实很容易,看看那:

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

您可以使用辅助的元组做的事情变得简单,然后只用实例方法toDelimitedString(),很简单的更好。

+0

非常感谢你......它真的帮助 –

+0

另一种帮助......你能告诉mw如何将这些数据分成多列吗?我的意思是我需要2000,1800,2700在3个不同的列。 –

+0

然后,你应该在你的UDF中返回一个元组,而不是一个String,因为它只返回一个只有一个字段(你的字符串)的元组,否则,你也可以使用'REGEX_EXTRACT_ALL'将其转换成一个3元组元组,最后如果你不需要这个关系可以继续使用,你可以存储为'STORE A INTO'输出'USING PigStorage(',',' - noschema');' –