Get compilation error in reducer’s for loop “Can only iterate over an array or an instance of java.lang.Iterable”… here is a solution to the problem.
Get compilation error in reducer’s for loop “Can only iterate over an array or an instance of java.lang.Iterable”
The
compilation error “Can only iterate over an array or an instance of java.lang.Iterable” appears in the reducer’s for loop.
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> Output, Reporter arg3)
throws IOException {
TODO Auto-generated method stub
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
In the above code, the compilation error “Can only iterate over an array or an instance of java.lang.Iterable” appears at “for(IntWritable val : values)"
. How do I use loops?
Solution
Or pass an Iterable<IntWritable> instead of Iterator< IntWritable>
method, or use hasNext
() and next()
to traverse Iterator
elements:
while (values.hasNext()) {
IntWritable val = values.next();
...
}