一.是创建Mapper类
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 java.io.IOException;
/**
* KEYIN:Map任务读取数据的key类型,offset,是i每行数据其实位置的偏移量,Long类型
* VALUEIN:Map任务读数据的value类型,其实就是一行行的字符串,String
*
* KEYOUR:map方法自定义实现输出的key的类型 String
* VALUEOUT:map方法自定义实现输出的value的类型 Integer
*
*
* 词频统计:相同的次数
*
* Long,String,String,Integer是java里面的数据类型
* Hadoop自定义类型:序列化和反序列化
*
*
* LongWritable :Long
* Text: String
* IntWritable: Integer
*/
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 把value对应的行数据按照指定的分隔符拆开
String[] words = value.toString().split(" ");
for(String word : words) {
// (hello,1) (world,1)
//我们的word是字符串类型 而要求的write是要text类型 所以new text 把word穿进去转变
context.write(new Text(word.toLowerCase()), new IntWritable(1));
}
}
}
二.创建Reduce类
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.Iterator;
/**
* reducer 的输入是 mapper的输出 text IntWritable
* 统计单词出现的次数 前面的text intwritable 就是单词的次数 后面的就是累加起来
*/
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable > {
/**
*
* (hello,1) (world,1)
* (hello,1) (world,1)