Reputation: 29
I am very confused why this is happening. I have been working on this for some time and I just don't understand.
My Map code works as I am able to verify the output in the directory it is in.
This is the method:
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String stateKeyword = value.toString();
String[] pieces = new String[] {stateKeyword};
for (String element : pieces) {
String name = element.split(":")[0].trim();
String id = element.split(":")[1].trim();
Integer rank = Integer.parseInt(element.split(":")[2].trim());
context.write(new Text(name), new Text(id + ":" + rank));
}
}
So my Output
will have the concatenation of the id and rank field. I can see it in the output file if I print the value normally.
However, any split
manipulation I execute throws aArrayOutOfBoundsException
and I can't understand why. I even do a check if the value contains a ":" and it will print but it won't split. But when I don't make this check I get the exception.
Here is my reduce:
public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
List<String> elements = new ArrayList<String>();
Text word = new Text();
for (Text val : values) {
if (val.toString().contains(":")) {
String state = val.toString().split(":")[0];
word.set(state);
}
context.write(key, word);
}
}
My output in my file looks like this:
Name id:rank
Name id:rank
Name id:rank
...
...
...
But why can't I split off the id and rank?
Upvotes: 0
Views: 462
Reputation: 94
To avoid ArrayOutOfBoundsException, check array size before getting value out of the array. Something like this will be more appropriate:
String[] temp = element.split(":");
if(element.size()==2){
String name = temp[0].trim();
String id = temp[1].trim();
}
Upvotes: 0