【问题标题】:mapreduce difference in countmapreduce 计数差异
【发布时间】:2019-10-18 03:19:02
【问题描述】:

我正在尝试编写一个程序来输出 2 列中计数之间的差异。所以我的数据看起来像这样:

2,1
2,3
1,2
3,1
4,2

我想计算 col1 中 key 的出现次数和 col2 中 key 的出现次数并取差值。输出应如下所示:

1,-1
2,0
3,0
4,1

这可以在一个 mapreduce 过程中完成吗(mapper、reducer)?

【问题讨论】:

    标签: java maven hadoop mapreduce


    【解决方案1】:

    在每行的映射器中,您将创建两个键,一个用于 col1,另一个用于 col2,其中值从每列计数,如下所示:

    2,1 -> 2:{1, 0} 和 1:{0, 1}

    2,3 -> 2:{1, 0} 和 3:{0, 1}

    1,2 -> 1:{1, 0} 和 2:{0, 1}

    3,1 -> 3:{1, 0} 和 1:{0, 1}

    4,2 -> 4:{1, 0} 和 2:{0, 1}

    然后在 reducer 中,您将获得这些结果,其中每一行是每个 reduce 调用的键和值组合:

    1 -> {0, 1}, {1, 0}, {0, 1}(添加它们将产生 -1)

    2 -> {1, 0}, 2:{1, 0}, 2:{0, 1}, 2:{0, 1}(相加将产生 0)

    3 -> {0, 1}, {1, 0}(添加它们将产生 0)

    4 -> {1, 0}(添加它们将产生 1)

    更新:

    这是 Hadoop 示例(它未经测试,可能需要一些调整才能使其正常工作):

    public class TheMapper extends Mapper<LongWritable, Text, Text, ArrayPrimitiveWritable>{        
    
        protected void map(LongWritable offset, Text value, Context context) 
        throws IOException, InterruptedException {
    
            StringTokenizer tok = new StringTokenizer( value.toString(), "," );
    
            Text col1 = new Text( tok.nextToken() );
            context.write( col1, toArray(1, 0) );
    
            Text col2 = new Text( tok.nextToken() );        
            context.write( col2, toArray(0, 1) );
        }
    
        private ArrayPrimitiveWritable toArray(int v1, int v2){     
            return new ArrayPrimitiveWritable( new int[]{i1, i2} );
        }   
    }
    
    public class TheReducer extends Reducer<Text, ArrayPrimitiveWritable, Text, Text> {
    
      public void reduce(Text key, Iterable<ArrayPrimitiveWritable> values, Context context) 
      throws IOException, InterruptedException {
    
          Iterator<ArrayPrimitiveWritable> i = values.iterator();
          int count = 0;
          while ( i.hasNext() ){
              int[] counts = (int[])i.next().get();
              count += counts[0];
              count -= counts[1];
          }
    
          context.write( key, new Text("" + count) );
      }
    }
    

    【讨论】:

    • 谢谢,我明白了。你将如何在 Java 中实现这一点?您是否需要公共 void 映射中的 2 个对象键{}?
    • 这是一个很好的解决方案,对我有用,但是,我很好奇你为什么使用 Text 而不是 IntWritable,因为我们正在处理数字节点度数。
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2015-04-07
    • 2015-12-30
    • 2016-06-14
    • 2020-07-25
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多