0
votes

my UDF converts the given input to UPPER case

package myudfs;
import java.io.IOException;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;

public class UPPER extends EvalFunc<String>
{
public String exec(Tuple input) throws IOException{

    if(input==null|| input.size()==0)
        return null;
    try{
        String str=(String)input.get(0);
        return str.toUpperCase();
    }catch(Exception e){
        throw new IOException("Caught exception processing input row",e);

    }
}
}

and my input file is

100,,King,SKING,515.123.4567,17-JUN-87,AD_PRES,24000,,90
101,Neena,Kochhar,NKOCHHAR,515.123.4568,21-SEP-89,AD_VP,17000,100,90
102,Lex,De Haan,LDEHAAN,515.123.4569,13-JAN-93,AD_VP,17000,100,90

I executed below steps-
1.) emp = LOAD' /home/warehouse/datasets/EMP.csv' USING PigStorage(',') AS (EMPLOYEE_ID:INT,FIRST_NAME:CHARARRAY,LAST_NAME:CHARARRAY,EMAIL:CHARARRAY,PHONE_NUMBER:CHARARRAY,HIRE_DATE:CHARARRAY,JOB_ID:CHARARRAY,SALARY:INT,MANAGER_ID:CHARARRAY,DEPARTMENT_ID:CHARARRAY);

2.) B = FOREACH emp GENERATE EMPLOYEE_ID,myudfs.UPPER(FIRST_NAME) AS LINE;

when i do DUMP B; I am getting "java.lang.NullPointerException", i think this is because my FIRST-NAME has null have in one of the rows.

i tried removing null values and then dumping the result but still i am getting null pointer exception.
C = FILTER B BY line IS NOT NULL; DUMP C;

Please help me with this.

2

2 Answers

0
votes

or better go with this kind of catch block...so that, only Null Pointer Exception is ignored...

catch(NullPointerException e){
        System.out.println("NullPointerException");
        return null;
       } catch (Exception e){
        throw WrappedIOException.wrap("Caught exception processing input row ", e);
       }
0
votes

Change your Catch Block to return null in case of an exception, so job won't stop... somewhat like as shown below...

catch (Exception e) {
    System.err.println("Error with ...");
    return null;
}