MapReduce当中的reduce当中的cleanup的用法

reduce当中的cleanup的用法:
马克-to-win @ 马克java社区:上面的topN是解决每个组里的topN,比如每个订单中的最小的。但如果需要横向的比较所有的key(初学者忽略:cleanup方法慎用, 如果所有的key的数据巨大量怎么办?Map map = new HashMap();内存都不够了, 所以考虑多步mapreduce),选出topN,得用cleanup。
马克-to-win @ 马克java社区:从现在开始,我们讲一些特殊用法,我们知道,map是读一行执行一次,reduce是对每一key,或一组,执行一次。但是如果需求是当我们得到全部数据之后,需要再做一些处理,之后再输出怎么办?这时候setUp或cleanUp就登场了,他们像servlet的init和destroy一样都只执行一次。map和reduce都有setUp或cleanUp,原理一样。我们只拿reduce做例子。
马克-to-win @ 马克java社区:这样对于最终数据的过滤筛选和输出步骤,要放在cleanUp中。前面我们的例子都是一行一行(对于map),一组一组(对于reduce)输出,借助cleanup,我们可以全部拿到数据,完全按照java过去的算法,最后过滤输出。下面我们用它解决topN问题。
还以wordcount为例,求出单词出现数量前三名。

package com;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ReduceSetupTestMark_to_win {

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private IntWritable one = new IntWritable(1);
        private Text word = new Text();
        /*
          hello a hello win
          hello a to
          hello mark
         */
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            System.out.println("key is " + key.toString() + " value is " + value.toString());
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {       

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44594249/article/details/97152203

上一篇:125. 验证回文串--双指针


下一篇:Nginx源码理解 - 内存池