1

前月の最後の日付を返すために月の日付を取得する必要があるユースケースがあります。

Ex: input:20150331 output:20150228

この前月の最後の日付を使用して、毎日のパーティションをフィルター処理します (豚のスクリプトで)。

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

日付を取得して前月の最後の日付を返す 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
        }

助けが必要です。よろしくお願いします。

4

1 に答える 1

3

で UDF を呼び出すことができますが、 ( Pig 内で)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 に渡そうとしている値が間違っていることが原因です。

One last thing, even if it is not necessary, in a future you might want to write a pure FILTER UDF. In that case, instead of inheriting from EvalFunc, you need to inherit from FilterFunc and return a Boolean value:

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;
        }
    }
} 
于 2015-07-28T14:59:10.920 に答える