是使用多线程的Mapreduce作业



我很好奇mapreduce作业是否在一台机器上使用多线程。例如,我在hadoop集群中有10台服务器,默认情况下,如果输入文件足够大,就会有10个映射器。单个映射器是否在单个机器中使用多线程?

单个映射器是否在单个机器中使用多线程?

是的。Mapreduce作业可以使用多线程映射器(多个线程或线程池运行map方法)

  • 我已经使用了更好的CPU利用率仅映射Hbase作业

    MultiThreadedMapper是一个很好的选择,如果你的操作是高度CPU密集的,可以提高速度。

映射器类应该扩展org.apache.hadoop.mapreduce.lib.map.MultithreadedMapper而不是常规的org.apache.hadoop.mapreduce.Mapper

Multithreadedmapper有不同的run()实现方法。像下面。

run(org.apache.hadoop.mapreduce.Mapper.Context context)

使用线程池运行应用程序的映射

您可以通过

设置MultiThreadedMapper中映射器内的线程数。

MultithreadedMapper.setNumberOfThreads(n);或者您可以在加载属性文件mapred.map.multithreadedrunner.threads = n时设置该属性并使用上述setter方法(以作业为基础)来控制CPU占用较少的作业。

这样做的影响,你可以看到在mapreduce计数器,特别是CPU相关的计数器。

multithreadadmapper实现示例代码片段:

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.MultithreadedMapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import java.io.IOException;
import java.util.regex.Pattern;

public class MultithreadedWordCount {
    // class should be thread safe
    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        public static enum PREPOST { SETUP, CLEANUP }
        @Override()
        protected void setup(Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws java.io.IOException, java.lang.InterruptedException {
            // will be called several times
            context.getCounter(PREPOST.SETUP).increment(1);
        }
        @Override
        protected void map(LongWritable key, Text value,
                     Context context) throws IOException, InterruptedException {
            String[] words = value.toString().toLowerCase().split("[\p{Blank}[\p{Punct}]]+");
            for (String word : words) {
                context.write(new Text(word), new LongWritable(1));
            }
        }
        @Override()
        protected void cleanup(Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws java.io.IOException, InterruptedException {
            // will be called several times
            context.getCounter(PREPOST.CLEANUP).increment(1);
        }
    }
    public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context
                        ) throws IOException, InterruptedException {
            long sum = 0;
            for (LongWritable value: values) {
              sum += value.get();
            }
            context.write(key, new LongWritable(sum));
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = new Job();
        job.setJarByClass(WordCount.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        MultithreadedMapper.setMapperClass(job, MultithreadedWordCount.WordCountMapper.class);
        MultithreadedMapper.setNumberOfThreads(job, 10);
        job.setMapperClass(MultithreadedMapper.class);
        job.setCombinerClass(MultithreadedWordCount.WordCountReducer.class);
        job.setReducerClass(MultithreadedWordCount.WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        /* begin defaults */
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        /* end defaults */
        job.waitForCompletion(true);
    }
}

请参考https://hadoop.apache.org/docs/r2.6.3/api/org/apache/hadoop/mapreduce/Mapper.html

应用程序可以覆盖run(Context)方法来对映射处理施加更大的控制,例如多线程映射器等。

此外,还有一个multithreaddmapper。我从来没用过这个

相关内容

  • 没有找到相关文章

最新更新