mapreduce任务在reducer之间的分布

zf2sa74q  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(231)

我开发了一个小的mapreduce程序。当我打开流程日志时,我看到框架创建了一个map和两个reducer。我只有一个输入文件和两个输出文件。现在请告诉我

1) Number of mapper and reducer are created by framework or it can be changed?
2) Number of output files always equal to number of reducers? i.e. each reducer
   creates its   own output file?
3) How one input file is distributed among mappers? And output of one mapper is 
   distributed among multiple reducers (this is done by framework or you can change)?
4) How to manage when multiple input files are there i.e. A directory ,
   containing input files?

请回答这些问题。我是mapreduce的初学者。

mwg9r5ms

mwg9r5ms1#

正如“ssaikia\u jtherocker”所解释的,Map器任务是根据hdfs块上的逻辑拆分总数创建的。我想补充一点问题#3“一个输入文件是如何在Map器之间分布的?一个Map器的输出分布在多个缩减器中(这是由框架完成的,或者您可以更改)?“例如,考虑我的字数计算程序,它计算文件中的字数,如下所示:

公共类wcmapper扩展了Map器{

@Override
public void map(LongWritable key, Text value, Context context) // Context context is output
        throws IOException, InterruptedException {

    // value = "How Are You"
    String line = value.toString(); // This is converting the Hadoop's "How Are you" to Java compatible "How Are You"

    StringTokenizer tokenizer = new StringTokenizer (line); // StringTokenizer returns an array tokenizer = {"How", "Are", "You"}

    while (tokenizer.hasMoreTokens()) // hasMoreTokens is a method in Java which returns boolean values 'True' or 'false'
    {
        value.set(tokenizer.nextToken()); // value's values are overwritten with "How" 
        context.write(value, new IntWritable(1)); // writing the current context to local disk
        // How, 1
        // Are, 1
        // You, 1
        // Mapper will run as many times as the number of lines 
    }
}

}

因此,在上面的程序中,stringtokenizer将行“how are you”拆分为3个单词,当在while循环中使用时,Map器的调用次数与单词数相同,因此这里调用3个Map器。
对于reducer,我们可以指定使用“job.setnumreducetasks(5);”生成输出的reducer数量声明。下面的代码片段将给你一个想法。

公共类bookmain{

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    // Use programArgs array to retrieve program arguments.
    String[] programArgs = new GenericOptionsParser(conf, args)
            .getRemainingArgs();
    Job job = new Job(conf);
    job.setJarByClass(BooksMain.class);
    job.setMapperClass(BookMapper.class);
    job.setReducerClass(BookReducer.class);
    job.setNumReduceTasks(5);

//job.setcombinerclass(bookreducer.class);

job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    // TODO: Update the input path for the location of the inputs of the map-reduce job.
    FileInputFormat.addInputPath(job, new Path(programArgs[0]));
    // TODO: Update the output path for the output directory of the map-reduce job.
    FileOutputFormat.setOutputPath(job, new Path(programArgs[1]));

    // Submit the job and wait for it to finish.
    job.waitForCompletion(true);
    // Submit and return immediately: 
    // job.submit();
}

}

ckocjqey

ckocjqey2#

让我试着回答你的问题。请告诉我你认为哪里不对-
1) Map器和缩减器的数量是由框架创建的还是可以更改的?
创建的Map任务总数取决于hdfs块中的逻辑拆分总数。因此,固定Map任务的数量可能并不总是可能的,因为不同的文件可能有不同的大小,并且总块的数量也不同。因此,如果您使用的是textinputformat,那么每个逻辑分割大致等于一个块,并且固定总Map任务的数量是不可能的,因为对于每个文件,可以创建不同数量的块。
与Map器的数量不同,还原器可以是固定的。
2) 输出文件的数量总是等于减速机的数量?i、 每个减速机创建自己的输出文件?
在某种程度上是的,但是有一些方法可以从一个reducer创建多个输出文件。例如:多次输出
3) 一个输入文件如何在Map器之间分布?一个Map器的输出分布在多个简化器中(这是由框架完成的,或者您可以更改)?
hdfs中的每个文件都由块组成。这些块被复制并可以保留在多个节点(机器)中。然后,Map任务被安排在这些块上运行。map任务可以运行的并发级别取决于每台机器拥有的处理器数量。e、 对于一个文件,如果计划了10000个Map任务,则取决于整个集群中的处理器总数,一次只能并发运行100个。
默认情况下,hadoop使用hashpartitioner,它计算从Map器发送到框架的键的hashcode,并将它们转换为分区。
例如。:

public int getPartition(K2 key, V2 value,
                          int numReduceTasks) {
    return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
  }

正如您在上面看到的,分区是从基于哈希代码固定的还原器总数中选择的。因此,如果numreducetask=4,则返回的值将在0到3之间。
4) 当存在多个输入文件(即包含输入文件的目录)时,如何进行管理?
hadoop支持由多个文件组成的目录作为作业的输入。

相关问题