2015-07-28 74 views
0

我有一個用例,我需要在一個月的日期返回上個月的最後一個日期。過濾器上的豬udf

Ex: input:20150331 output:20150228 

我將使用此前一個月的最後日期來過濾每日分區(在豬腳本中)。

B = filter A by daily_partition == GetPrevMonth(20150331); 

我創建了需要的日期,並返回前一個月的最後date.But無法使用它在過濾器上的UDF(GetPrevMonth)。

ERROR:Could not infer the matching function for GetPrevMonth as multiple or none of them fit. Please use an explicit cast. 

我的udf以元組爲輸入。 谷歌搜索說,UDF不能應用於過濾器。 有什麼解決方法嗎?或者我在哪裏錯了?

UDF:public class GetPrevMonth extends EvalFunc<Integer> { 

    public Integer exec(Tuple input) throws IOException { 
     String getdate = (String) input.get(0); 
     if (getdate != null){ 
     try{ 
      //LOGIC to return prev month date 
     } 

需要幫助。提前致謝。

+0

你應該接受Balduz的答案,除非你覺得這是不能令人滿意的(對我來說,似乎右) – Eyal

回答

3

你可以調用UDF在FILTER,但你傳遞一個數字的功能,而你希望它接收Stringchararray內豬):

String getdate = (String) input.get(0); 

簡單的解決辦法是投它chararray時調用的UDF:

B = filter A by daily_partition == GetPrevMonth((chararray)20150331); 

一般來說,當你看到這樣Could not infer the matching function for X as multiple or none of them fit一定的誤差,時間99%的原因是,您要傳遞給UDF的數值爲w榮。

最後一件事,即使沒有必要,在將來你可能會想寫一個純粹的FILTER UDF。在這種情況下,而不是從EvalFunc繼承,你需要從FilterFunc繼承和返回Boolean值:

public class IsPrevMonth extends FilterFunc { 
    @Override 
    public Boolean exec(Tuple input) throws IOException { 
     try { 
      String getdate = (String) input.get(0); 
      if (getdate != null){ 
       //LOGIC to retrieve prevMonthDate 

       if (getdate.equals(prevMonthDate)) { 
        return true; 
       } else { 
        return false; 
       } 
      } else { 
       return false; 
      } 
     } catch (ExecException ee) { 
      throw ee; 
     } 
    } 
}