How do I find Word Count shifted minus twenty (-5) using MapReduce?

Viewed 53

Here's the sample code for the wordcount:

SumReducer.java

    int wordCount = 0;

    for (IntWritable value : values) {
      
     
        wordCount += value.get();
    }
    
    
    context.write(key, new IntWritable(wordCount));
}

WordCount.java

if (args.length != 2) {
  System.out.printf(
      "Usage: WordCount <input dir> <output dir>\n");
  System.exit(-1);
}

Job job = new Job();

job.setJarByClass(WordCount.class);


job.setJobName("Word Count");

FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));


job.setMapperClass(WordMapper.class);
job.setReducerClass(SumReducer.class);

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


boolean success = job.waitForCompletion(true);
System.exit(success ? 0 : 1);

WordMapper.java

String line = value.toString();

for (String word : line.split("\\W+")) {
  if (word.length() > 0) {         
    context.write(new Text(word), new IntWritable(1));

The assigned shift value is minus twenty (-20). For example, assume the word “Alpha” appears sixteen (16) times in the input source, the shifted word count result for this word would be minus four (-4).

1 Answers

Just intialize the wordCount to -20 and it should work. Worked for me!

int wordCount = -20;

    for (IntWritable value : values) {
      
     
        wordCount += value.get();
    }
    
    
    context.write(key, new IntWritable(wordCount));
}
Related