Filter function example: IsEmpty
Monday, February 28, 2011
1:42 PM
import java.io.IOException;
import java.util.Map;
import org.apache.pig.FilterFunc;
import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.DataBag;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.DataType;
import org.apache.pig.impl.util.WrappedIOException;

public class IsEmpty extends FilterFunc {
    public Boolean exec(Tuple input) throws IOException {
        if (input == null || input.size() == 0)
            return null;
        try {
            Object values = input.get(0);
            if (values instanceof DataBag)
                return ((DataBag)values).size() == 0;
            else if (values instanceof Map)
                return ((Map)values).size() == 0;
            else{
                throw new IOException("Cannot test a " +
                    DataType.findTypeName(values) + " for emptiness.");
            }
        } catch (ExecException ee) {
            throw WrappedIOException.wrap("Caught exception processing input row ", ee);   
        }
    }
}
Pasted from <[link]> 
Some attributes
Input polymorphism
Object values = input.get(0); 
if (values instanceof DataBag) { 
// do something with (DataBag)values
} else if (values instanceof Tuple) { 
// do something with (Tuple)values 
} 
Variable argument lists: 
Input.get(0): first element of input Tuple. 
Input.get(1): second element. 
… 