rohit kumar
rohit kumar

Reputation: 11

ArrayIndexOutOfBoundException in Mapping code

the input is 1000:rohit:male:dev:2500

In this i want to count the male and female count. when i use split, assigning each gender field to reducer shows ArrayIndexOutOfBounfException:2

public class DeptEmpcountMapper extends
        Mapper<LongWritable, Text, Text, LongWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context)

            throws IOException, InterruptedException {

        String st = value.toString();

        String[] field = st.split(":");
        String st1 = field[2];

        context.write(new Text(st1), new LongWritable(1));
    }

}

Upvotes: 1

Views: 29

Answers (1)

Ben Watson
Ben Watson

Reputation: 5521

You're making the dangerous assumption that every input record takes the form 1000:rohit:male:dev:2500. From your error, it's clear that isn't the case. Bad data is always something that needs to be accounted for.

Consider some simple validation of your input beforehand:

String[] field = st.split(":");
if(ArrayUtils.getLength(field) == 5) {
    String st1 = field[2];
    context.write(new Text(st1), new LongWritable(1));
} else {
    //Consider printing "st" to see what the bad input looks like
}

Upvotes: 1

Related Questions