I am comparing example Java code from the O’Reilly book Hadoop: The Definitive Guide (by Tom White, 3rd edition) and my own attempt at recreating/understanding it. The issue I am having is as follows:
The class from the book compiles just fine:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MaxTemperatureReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}
But when I try to test a portion of it on my own, I get the compile error of “int cannot be dereferenced:”
public class TestMinValue {
public static void main(String[] args){
int[] values = {1,2,3,4,5};
int maxValue = Integer.MIN_VALUE;
for(int value : values){
maxValue = Math.max(maxValue, value.get());
}
}
}
I am new to Java and would like to understand the difference; why is the example class working, but my snippet of it isn’t?
The
IntWritabletype is a class, which has aget()method. You are using the primitive typeintinstead. Primitives do not have methods in Java.