Hadoop 如果不在MapReduce中使用setup()和closeup()方法怎么办?

Hadoop 如果不在MapReduce中使用setup()和closeup()方法怎么办?,hadoop,mapreduce,Hadoop,Mapreduce,假设我有一个映射器,如下所示,映射器类为每个映射器获取本地前10名 public class TopTenMapper extends Mapper<LongWritable, Text, Text, LongWritable> { private TreeMap<Long, String> tmap; // Called once in the beginning before each map method @Override pub

假设我有一个映射器,如下所示,映射器类为每个映射器获取本地前10名

public class TopTenMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

    private TreeMap<Long, String> tmap;
    // Called once in the beginning before each map method
    @Override
    public void setup(Context context) throws IOException, InterruptedException {
        tmap = new TreeMap<Long, String>();
    }

    // Called once for each key/value pair in the input split
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] tokens = value.toString().split("\\t");
        String streetName = tokens[0];
        Long numCrimes = Long.parseLong(tokens[1]);
        tmap.put(numCrimes, streetName);
        if(tmap.size() > 10){
            tmap.remove(tmap.firstKey());
        }
    }

    // Called once at the end of the task
    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        for(Map.Entry<Long, String> entry : tmap.entrySet()){
            context.write(new Text(entry.getValue()), new LongWritable(entry.getKey()));
        }
    }
}
我认为tmap仍然在map任务的每次启动时初始化,是吗?我必须使用
setup()
cleanup()
方法的原因和场景是什么?

map()
每个键值输入对调用一次。
TopTenMapper
类本身(因此
setup()
)在每个映射任务中只初始化一次

在第二个示例中,使用
tmap=newtreemap()
map()
中,您永远不会真正获得前十名,该映射中只有一个值。

map()
每个键值输入对调用一次。
TopTenMapper
类本身(因此
setup()
)在每个映射任务中只初始化一次


在第二个示例中,使用
tmap=newtreemap()
map()
中,您永远不会真正获得前十名,该地图中只有一个值。

谢谢您的解释!谢谢你的解释!
public class TopTenMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

    private TreeMap<Long, String> tmap;

    // Called once for each key/value pair in the input split
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        tmap = new TreeMap<Long, String>();

        String[] tokens = value.toString().split("\\t");
        String streetName = tokens[0];
        Long numCrimes = Long.parseLong(tokens[1]);
        tmap.put(numCrimes, streetName);
        if(tmap.size() > 10){
            tmap.remove(tmap.firstKey());
        }

        for(Map.Entry<Long, String> entry : tmap.entrySet()){
            context.write(new Text(entry.getValue()), new LongWritable(entry.getKey()));
        }
    }
}