在mapreduce的mapper / reducer类之外访问静态HashMap

问题描述 投票:0回答:1

我正在尝试编写一个mapreduce程序,其中map函数将项目添加到HashMap,然后reducer访问这些项目并将其写入输出。

public class MyClass {
    static HashMap<String, Integer> temp = new HashMap<String, Integer>();

    public static class Map1 extends Mapper<LongWritable, Text, Text, IntWritable> {
        public void map(LongWritable key, Text value, Context context) {
            temp.put("1", 1);
        }
    }

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

        @Override
        public void reduce(Text key, Iterable<IntWritable> values, Context context) {
            Iterator it = temp.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Integer> pair = (Map.Entry<String, Integer>)it.next();
                String key = pair.getKey();
                String val = Integer.toString(pair.getValue());
                context.write(new Text(key), new Text(val));
            }
        }
    }

编译正常,但减速器的输出为空。我不太擅长Java,所以我不太确定这里出了什么问题。

java hadoop mapreduce
1个回答
0
投票

您误解为MapReduce是一种在许多进程和可能的机器之间运行的分布式算法。

在该过程中创建的每个新MyClass实例将为空,并且不会在应用程序生命周期中共享

另外,您的映射器没有执行任何操作。将数据发送到减速器的方法是使用context.write

请参阅Hadoop官方文档中的单词计数代码。

© www.soinside.com 2019 - 2024. All rights reserved.