Mapreduce work with multithreading

I am curious if the mapreduce job works when using multiple threads on the same machine. For example, I have 10 servers in a hadoop cluster, by default, if the input file is large enough, then there will be 10 cards. Is one mapper multiple threads in the same machine?

+4
source share
2 answers

Is one mapper multiple threads in the same machine?

YES. The Mapreduce task can use a multithreaded mapmaker (multiple threads or a pool of threads running under map).

  • Hbase...

    MultiThreadedMapper , , .

mapper 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 ( ) , .

mapreduce .

MultithreadedMapper:

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);
    }
}
+4

Source: https://habr.com/ru/post/1656947/


All Articles