猿问

java.io.IOException in MapReduce

我想使用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”,因为地图的输出是(文本,不可写),而减少的输入也是(文本,不可写),所以我不知道为什么,任何人都可以帮助我吗?


噜噜哒
浏览 79回答 1
1回答

扬帆大鱼

合并器必须能够接受来自映射器的数据,并且必须输出可用作化简器输入的数据。在本例中,“合并器”输出类型为 ,但“化简器”输入类型为 ,因此它们不匹配。<Text, Text><Text, IntWritable>对于这个问题,您实际上并不需要MapReduce,因为您在每条线上都有每年的所有可用数据,并且您不需要在行之间进行比较。String line = value.toString();String[] elements = line.split("\\s");Text year = new Text(elements[0]);int maxTemp = INTEGER.MIN_VALUE;int minTemp = INTEGER.MAX_VALUE;int temp;for(int i = 1; i<elements.length;i++) {&nbsp; &nbsp; temp = Integer.parseInt(elements[i])&nbsp; &nbsp; if (temp < minTemp) {&nbsp; &nbsp; &nbsp; &nbsp; minTemp = temp;&nbsp; &nbsp; } else if (temp > maxTemp) {&nbsp; &nbsp; &nbsp; &nbsp; maxTemp = temp;&nbsp; &nbsp; }}System.out.println("For year " + year + ", the minimum temperature was " + minTemp + " and the maximum temperature was " + maxTemp);
随时随地看视频慕课网APP

相关分类

Java
我要回答