首页 > 解决方案 > Hadoop 计数问题中的 Map Reduce

问题描述

我在 map reduce 上做一些事情。例如,我有一些数据集如下

node_id node_id 权重

1 2 7

1 3 20

2 3 3

3 1 5

4 1 9

5 6 10

这意味着节点 1 到节点 2 到目前为止的权重为 7 等等......而且一开始,我想计算节点的数量,因此,我在地图类中执行以下操作

    public static class countMapper
    extends Mapper<Text,Text,Text,Text>{
    public void map(Text key, Text value, Context context)
        throws IOException, InterruptedException{
        context.write(key, value);
        context.write(new Text(value.toString().split(" ")[0]), new Text());
    }
}

另外,我将 from 节点和 to 节点分别写入一次,以计算节点数。

    public static class countReducer
        extends Reducer<Text, Text, Text, Text>{

        private long numNodes = 0;

        public void reduce(Text key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException{
            numNodes += 1;
        }

        public void cleanup(Context context)
            throws IOException, InterruptedException{
            context.getCounter(PageRank.myCounter.numNodes).setValue(numNodes);
        }
}

我在 Reducer Class 中声明了 numNodes 。它似乎适用于计算节点的数量。结果显示6!正确的!!我很困惑为什么它有效?map reduce 不是处理问题的分布式系统。

每个 JVM 都会运行它们的 reduce 类。似乎无法正确计算 numNodes。因为有很多reducer类。每个 reducer 类之间不会相互通信。因此,我在这种情况下很困惑。我在带有 2 个数据节点和 1 个名称节点的 aws ec2 上运行它。它仍然显示了正确的节点数。

即使我创建了一个具有 12000000 个节点的文件来模拟非常大的文件以导致文件拆分。另外,我设置 mapred.reduce.tasks =2 来强制使用多个减速器。它仍然显示正确的答案。
谁能告诉我为什么?

标签: javahadoopmapreducereducers

解决方案


推荐阅读