Reputation: 812
I am modifying normal word count program which counts every word to make it count only specific words.
The reducer and map class are same as of normal word count. Not getting word count properly. I have multiple occurrences of same specific word in file but getting one as the count.
public class wordcountmapper extends MapReduceBase implements Mapper<LongWritable, Tex, Text, IntWritable> // mapper function implemented.
{
private final static IntWritable one = new IntWritable(1); // intwritable
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString(); // conversion in string
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
if (line.compareTo("Cold") == 0) { //cold is the specific word to get count for
output.collect(word, one); // getting 1 as a count for 'cold' as if its counting only first line 'cold' and not going to next line.
}
}
}
}
Upvotes: 0
Views: 4310
Reputation: 177
For a start your if statement
is comparing the line object with "Cold" which is wrong. It should be comparing the tokenized word with "Cold" if(tokenizer.nextToken().equals("Cold"))
.
I am not sure how with the current logic you are getting the count of "Cold" as 1. May be in your input you have a line with a single word as "Cold".
Upvotes: 1