1.编写mapper类
public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
Text word = new Text();
IntWritable one = new IntWritable(1);
/**
* map函数:处理行,有几行处理几行。如test1.txt文本中有4行,那么map()调用4次
*/
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()){
word.set(itr.nextToken());
context.write(word,one);
}
}
}
2.编写Reducer
public class MyReduce extends TableReducer<Text, IntWritable, Text> {
IntWritable result = new IntWritable();
protected void reduce(Text k2, Iterable<IntWritable> v2,Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : v2) {
sum&