1. 程式人生 > >mapReduce計算 最大值-----

mapReduce計算 最大值-----

計算最大值,通過最後的cleanup函式,計算把所有的數目通過map ,

最後的時候把最大的值放入cleanup函式中,通過這個函式返回。

------------------------------------------------------------------------------

   private int maxNum = 0;


        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] str = value.toString().split(",", 2);
            try {// 對於非數字字元我們忽略掉
                int temp = Integer.parseInt(str[0]);
                if (temp > maxNum) {
                    maxNum = temp;
                }
            } catch (NumberFormatException e) {}
        }


        @Override
        protected void cleanup(Context context) throws IOException,
                InterruptedException {
            context.write(new IntWritable(maxNum), new IntWritable(maxNum));

        }

------------------------------------------------------------------------------

 private int maxNum = 0;


        public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
                throws IOException, InterruptedException {
            for (IntWritable val : values) {
                if (val.get() > maxNum) {
                    maxNum = val.get();
                }
            }
        }


        @Override
        protected void cleanup(Context context) throws IOException,
                InterruptedException {
            context.write(new IntWritable(maxNum), new IntWritable(maxNum));
        }

reduce的數量只能有一個,而且邏輯也是相同的。。