亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java.io.IOException in MapReduce

java.io.IOException in MapReduce

嚕嚕噠 2022-09-28 15:43:50
我想使用MapReduce在txt文件中獲取每年的最大值和最小值。文件中的內容如下所示:1979 23 23 2 43 24 25 26 26 26 26 25 261980 26 27 28 28 28 30 31 31 31 30 30 301981 31 32 32 32 33 34 35 36 36 34 34 341984 39 38 39 39 39 41 42 43 40 39 38 381985 38 39 39 39 39 41 41 41 00 40 39 39第一列表示年份。我希望地圖還原給我一個最終的輸出,如下所示:1979 2, 261980 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”,因為地圖的輸出是(文本,不可寫),而減少的輸入也是(文本,不可寫),所以我不知道為什么,任何人都可以幫助我嗎?
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

合并器必須能夠接受來自映射器的數據,并且必須輸出可用作化簡器輸入的數據。在本例中,“合并器”輸出類型為 ,但“化簡器”輸入類型為 ,因此它們不匹配。<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++) {

    temp = Integer.parseInt(elements[i])

    if (temp < minTemp) {

        minTemp = temp;

    } else if (temp > maxTemp) {

        maxTemp = temp;

    }

}


System.out.println("For year " + year + ", the minimum temperature was " + minTemp + " and the maximum temperature was " + maxTemp);


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號