Created
August 26, 2012 06:57
-
-
Save anonymous/3475348 to your computer and use it in GitHub Desktop.
Data Intensive Text Processing with MapReduce #3 figure3.3.x Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.moaikids.mapred.map; | |
import info.moaikids.chunker.Chunker; | |
import info.moaikids.chunker.KuromojiChunker; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.apache.hadoop.io.IntWritable; | |
import org.apache.hadoop.io.LongWritable; | |
import org.apache.hadoop.io.Text; | |
import org.apache.hadoop.mapreduce.Mapper; | |
public class Figure33xMapper extends | |
Mapper<LongWritable, Text, Text, IntWritable> { | |
Map<String, Integer> associativeArray; | |
Chunker chunker = new KuromojiChunker(); | |
static final int MAX = 128; | |
@Override | |
protected void setup(Context context) throws IOException, | |
InterruptedException { | |
super.setup(context); | |
associativeArray = new HashMap<String, Integer>(); | |
} | |
@Override | |
protected void map(LongWritable key, Text value, Context context) | |
throws IOException, InterruptedException { | |
for (String chunk : chunker.chunking(value.toString())) { | |
if (chunk.trim().isEmpty()) { | |
continue; | |
} | |
if (associativeArray.containsKey(chunk)) { | |
associativeArray.put(chunk, associativeArray.get(chunk) + 1); | |
} else { | |
associativeArray.put(chunk, 1); | |
} | |
} | |
if (associativeArray.size() > MAX) { | |
emit(context); | |
} | |
} | |
protected void emit(Context context) throws IOException, | |
InterruptedException { | |
for (Entry<String, Integer> entry : associativeArray.entrySet()) { | |
context.write(new Text(entry.getKey()), | |
new IntWritable(entry.getValue())); | |
} | |
associativeArray.clear(); | |
} | |
@Override | |
protected void cleanup(Context context) throws IOException, | |
InterruptedException { | |
emit(context); | |
super.cleanup(context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment