2013-11-20 93 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');' –