筛选器上的 Pig udf

Pig udf on Filter

我有一个用例,我需要将一个月的日期计算到 return 上个月的最后一个日期。

Ex: input:20150331 output:20150228

我将使用上个月的最后日期来过滤每日分区(在猪脚本中)。

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

我创建了一个 UDF(GetPrevMonth),它获取日期和 return 上个月的最后一个 date.But 无法在过滤器上使用它。

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
        }

需要提前help.Thanks。

您可以在 FILTER 中调用 UDF,但是您将一个数字传递给函数,同时您希望它接收 String(Pig 中的 chararray):

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

简单的解决方案是在调用 UDF 时将其转换为 chararray

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 的值是错误的。

最后一件事,即使没有必要,将来您可能想要编写纯 FILTER UDF。在这种情况下,您需要从 FilterFunc 和 return 继承一个 Boolean 值,而不是从 EvalFunc 继承:

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;
        }
    }
}