【问题标题】:If 2 Mappers output the same key , what will the input to the reducer be?如果 2 个 Mappers 输出相同的 key ,reducer 的输入是什么?
【发布时间】:2014-12-11 01:07:43
【问题描述】:

我在学习 Map reduce 时有以下疑问。如果有人能回答,那将是非常有帮助的。

我有两个映射器处理同一个文件 - 我使用 MultipleInputFormat 配置它们

映射器 1 - 预期输出 [提取文件的几列后]

a - 1234
b - 3456
c - 1345

Mapper 2 预期输出[提取同一文件的几列后]

a - Monday
b - Tuesday
c - Wednesday

还有一个 reducer 函数,它只输出它作为输入获得的键值对 所以我希望输出是我知道类似的键将被打乱以制作一个列表。

a - [1234,Monday]
b - [3456, Tuesday]
c - [1345, Wednesday]

但是我得到了一些奇怪的输出。我猜只有 1 个 Mapper 正在运行。 这不应该是预期的吗?每个映射器的输出会单独洗牌吗?两个映射器会并行运行吗?

如果这是一个蹩脚的问题,请原谅请理解我是 Hadoop 和 Map Reduce 的新手

下面是代码

//Mapper1
public class numbermapper extends Mapper<Object, Text, Text, Text>{

    public void map(Object key,Text value, Context context) throws IOException, InterruptedException {
        String record = value.toString();
        String[] parts = record.split(",");
        System.out.println("***Mapper number output "+parts[0]+"  "+parts[1]);
        context.write(new Text(parts[0]), new Text(parts[1]));

    }
}

//Mapper2
public class weekmapper extends Mapper<Object, Text, Text, Text> {
    public void map(Object key, Text value, Context context)
            throws IOException, InterruptedException {
        String record = value.toString();
        String[] parts = record.split(",");
        System.out.println("***Mapper week output "+parts[0]+"   "+parts[2]);
        context.write(new Text(parts[0]), new Text(parts[2]));
    }
}

//Reducer
public class rjoinreducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Text values, Context context)
    throws IOException, InterruptedException {
   context.write(key, values);

}
}

//Driver class
public class driver {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "Reduce-side join");
        job.setJarByClass(numbermapper.class);
        job.setReducerClass(rjoinreducer.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        MultipleInputs.addInputPath(job, new Path(args[0]),TextInputFormat.class, numbermapper.class);
        MultipleInputs.addInputPath(job, new Path(args[0]),TextInputFormat.class, weekmapper.class);
        Path outputPath = new Path(args[1]);


        FileOutputFormat.setOutputPath(job, outputPath);
        outputPath.getFileSystem(conf).delete(outputPath);
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

这是我得到的 O/P-

a     Monday
b     Tuesday
c     Wednesday

使用的数据集

a,1234,Monday
b,3456,Tuesday
c,1345,Wednesday

【问题讨论】:

  • 你的奇怪输出是什么?
  • 您能否提供一个演示,说明您如何编写代码以及“奇怪的输出”是什么?
  • 我编辑了问题以包括 I/P O/P 和我使用的代码。它只是给出第二个映射器的输出。

标签: hadoop mapreduce


【解决方案1】:

多输入格式只是获取 1 个文件并在其上运行一个映射器,因为我为两个映射器提供了相同的路径。

当我将数据集复制到不同的文件并运行相同的程序并获取两个不同的文件(相同的内容但文件的名称不同)时,我得到了预期的输出。

所以我现在明白了,不同映射器函数的输出也是基于 key 组合的,而不仅仅是同一个映射器函数的输出。

感谢您的帮助....!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多