Demo entry 6346398

WordCount-NoCombiner

   

Submitted by anonymous on Feb 10, 2017 at 06:48
Language: Java. Code size: 2.9 kB.

public class WordCount {

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

        private final static IntWritable one = new IntWritable(1);
        private Set<Character> set = initSet();

        private Set<Character> initSet() {
            Set<Character> set = new HashSet<Character>();
            set.add('m');
            set.add('n');
            set.add('o');
            set.add('p');
            set.add('q');
            return set;
        }

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                String s = itr.nextToken();
                if (set.contains(Character.toLowerCase(s.charAt(0)))) {
                    context.write(new Text(s), one);
                }
            }
        }
    }

    //Reducer
    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 sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    //Partitioner
    public static class WordsPartitioner
            extends Partitioner<Text, IntWritable> {

        public int getPartition(Text key, IntWritable value, int numReduceTasks) {
            if (numReduceTasks == 0) {
                return 0;
            }
            char c = Character.toLowerCase(key.toString().charAt(0));
            return (c - 'm') % numReduceTasks;
        }
    }


    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: wordcount <in> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setPartitionerClass(WordsPartitioner.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).