1

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

}
1
  • 1
    You say the reducer is throwing an exception but you have posted your mapper's code. Commented Jul 30, 2017 at 17:43

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

But I think the OP has specified that the input, specifically, is 1000:rohit:male:dev:2500 or maybe I'm interpreting it incorrectly. Nevertheless, helpful answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.