2016-03-28 12 views
0

複数のPig UDFを定義したい。それぞれがデータの異なる部分を抽出します。私の場合、データは、多数のネストされたJSONオブジェクトを含む複雑な構造を持つJSONドキュメントです。豚:1つのクラスの複数のUDF

問題は今のところ私が必要とするすべての機能に対して異なるEvalクラスを作成したことです。これらのクラスはそれぞれexec()を実装しています。 すべての関数を同じUDFクラスに入れてブタから呼び出す方法はありますか?私のUDFの1の

例:

public class PigGetTimestamps extends EvalFunc<Tuple>{ 
    public Tuple exec(org.apache.pig.data.Tuple input) throws IOException {   

    if (input == null || input.size() == 0){ 
     return null; 
    } 

    try { 

     String inputString = DataType.toString(input.get(0)); 
     try 
     { 
      String[] tokens=inputString.split("\t"); 
      if (tokens.length<1) 
       return null; 
      Document document=new Document(tokens[0], true, false); 
      long timestamp_fetch=document.getTimestamp_fetch(); 
      long timestamp_pub=document.getTimestampPub(); 
      Tuple output = TupleFactory.getInstance().newTuple(2); 
      output.set(0,timestamp_pub); 
      output.set(1,timestamp_fetch); 
      return output; 
     } 
     catch(Exception e) 
     { 
      return null; 
     } 

    } catch (Exception e) { 
     System.out.println("Can't extract field; error = " + e.getMessage()); 
     return null; 
    }   
} 

答えて

0

あなたはそれの複数のインスタンスを作成し、同じクラスを使用することができます - コンストラクタにあなたは何の機能、それはあなたにそう

を行います状態になるとき

define udf1 my.package.udf.MultiFunc('1'); 
define udf2 my.package.udf.MultiFunc('2'); 

をそして、あなたはUDFクラスは次のようになります。:豚ますが、別のインスタンスを定義します

public class MultiFunc extends EvalFunc<String> { 

     private String operation; 
     public MultiFunc(String operation){ 
      this.data = operation; 

     } 

     @Override 
     public String exec(Tuple tuple) throws IOException { 
     switch (operation){ 
      case "+": 
       //your code here; 
       break; 

      case "-": 
       //your code here; 
       break; 

      break; 

     }   
} 
+0

標準的な方法はありますか?結果は得られるかもしれませんが、ハックです – Athar

関連する問題