2015-07-28 71 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; 
     } 
    } 
}