【问题标题】:Mapreduce Mapper create 2 keys for reducer calculationMapreduce Mapper 为 reducer 计算创建 2 个键
【发布时间】:2017-11-03 13:56:59
【问题描述】:

我正在尝试从我的数据集中创建 2 个键,其中有 2 列数字,由制表符分隔。我知道如何制作 1 个键/值,但不确定如何制作第二对键/值。本质上,我想为每一列创建一个键/值。然后在reducer部分,取每个key的count之差。

这是我的映射器部分:

public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{

        private IntWritable one = new IntWritable(1);
        private Text nodeX = new Text();

        public void map(Object key, Text value, Context context
                        ) throws IOException, InterruptedException {
            String[] data = value.toString().split("\\t");
            String node0 = data[0];
            String node1 = data[1];
            StringTokenizer itr = new StringTokenizer(data);
            while(itr.hasMoreTokens()){
                nodeX.set(node0);
                context.write(nodeX, one)
                nodeY.set(node1);
                context.write(nodeY, one)
        }
    }

这是减速器:

public static class IntSumReducer
        extends Reducer<Text,IntWritable,Text,IntWritable> {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values,
                           Context context
                           ) throws IOException, InterruptedException {

            int sum0 = 0;
            for (IntWritable val : values) {
                sum0 += val.get()
            }
            int sum1 = 0;
            for (IntWritable val : values) {
                sum1 += val.get()
            }
            diff = sum0 - sum1;
            result.set(diff);
            context.write(key, diff);
        }
    }

我想我在数据从映射器传递到减速器的部分做了一些事情,可能需要 2 个键。 Java 新手,不知道如何正确获取它。

我的输入数据如下所示:

123 543
123 234
543 135
135 123

我希望输出是,我在其中计算 col1 键和 col2 键出现次数之和的差。

123 1
543 0
135 0
234 -1

【问题讨论】:

    标签: java maven hadoop mapreduce


    【解决方案1】:

    我认为您想将行拆分为单词并让单词成为一个数字,然后计算差异。您可以使用NLineInputFormat,键是行号,拆分值并计算。否则 。您可以定义一个静态长类型来记录行号。

    public static class TokenizerMapper extends
            Mapper<LongWritable, Text, LongWritable, IntWritable>
            {
    
        private IntWritable diffen = new IntWritable();
        private static long  row_num= 0;
    
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] data = value.toString().split("\\t");
            String node0 = data[0];
            String node1 = data[1];
            int dif = Integer.parseInt(node1)-Integer.parseInt(node0);
                diffen.set(dif);
                row_num++;
                context.write(new LongWritable(row_num), diffen);
        }
    }
    

    您也可以将值写入reduce 并拆分为两部分并计算不同的.ALL 即可;

    【讨论】:

    • 不确定 dif 的计算是否能解决我的问题。该方程按行计算差异,我需要按键计算差异。除非我理解你的代码错误。
    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 2020-10-05
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2019-06-20
    • 2023-03-29
    • 2015-06-06
    相关资源
    最近更新 更多