我想使用MapReduce在txt文件中获取每年的最大值和最小值。文件中的内容如下所示:
1979 23 23 2 43 24 25 26 26 26 26 25 26
1980 26 27 28 28 28 30 31 31 31 30 30 30
1981 31 32 32 32 33 34 35 36 36 34 34 34
1984 39 38 39 39 39 41 42 43 40 39 38 38
1985 38 39 39 39 39 41 41 41 00 40 39 39
第一列表示年份。我希望地图还原给我一个最终的输出,如下所示:
1979 2, 26
1980 26, 31
...
所以我用Java编写代码是这样的:
public class MaxValue_MinValue {
public static class E_Mappter extends Mapper<Object, Text, Text, IntWritable> {
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] elements = line.split("\\s");
Text outputKey = new Text(elements[0]);
for(int i = 1; i<elements.length;i++) {
context.write(outputKey, new IntWritable(Integer.parseInt(elements[i])));
}
}
}
public static class E_Reducer extends Reducer<Text,IntWritable, Text, Text> {
public void reduce(Text inKey,Iterable<IntWritable> inValues, Context context) throws IOException, InterruptedException {
int maxTemp = 0;
int minTemp = 0;
for(IntWritable ele : inValues) {
if (ele.get() > maxTemp) {
maxTemp = ele.get();
}
if (ele.get() < minTemp) {
minTemp = ele.get();
}
}
context.write(inKey, new Text("Max is " + maxTemp + ", Min is " + minTemp));
}
}
我对这个错误感到困惑“来自地图的值的类型不匹配:预期的组织.apache.hadoop.io.文本,接收的组织.apache.hadoop.io.IntWriteable”,因为地图的输出是(文本,不可写),而减少的输入也是(文本,不可写),所以我不知道为什么,任何人都可以帮助我吗?
扬帆大鱼
相关分类