Skip to content

Instantly share code, notes, and snippets.

@sdpatil
Created April 6, 2014 19:26
Show Gist options
  • Select an option

  • Save sdpatil/10010442 to your computer and use it in GitHub Desktop.

Select an option

Save sdpatil/10010442 to your computer and use it in GitHub Desktop.
WordCountMapper.java
package com.spnotes.hadoop;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.LoggerFactory;
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
org.slf4j.Logger logger = LoggerFactory.getLogger(WordCountMapper.class);
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
protected void map(LongWritable key, Text value,
Context context)
throws IOException, InterruptedException {
logger.debug("Entering WordCountMapper.map() " + this);
String line = value.toString();
StringTokenizer st = new StringTokenizer(line," ");
while(st.hasMoreTokens()){
word.set(st.nextToken());
context.write(word,one);
}
logger.debug("Exiting WordCountMapper.map()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment