前置
简单的WordCount练习一下手感
/**
*
*自定义Mapper类
*/
public class CustomMapper extend Mapper<LongWritable,Text,Text,IntWritable>{
private Text k = new Text();
private IntWritable v = new IntWritable(1);
@Override
protected void map(LongWritable key,Text value,Context context){
//读取一行
String line = value.toString();
//按空格拆分单词
String[] words = line.split("//s+");
for(String word:words){
k.set(word);
//发送到下游
collect.write(k,v);
}
}
}
/**
*
*自定义Reducer
*/
public class CustomReducer extend Reducer<Text,IntWritable,Text,IntWritable>{
private int sum;
private IntWritable v = new IntWritable();
@Override
public void reduce(Text key,Interable<IntWritable> value,Context context){
//累加计算单词个数
sum = 0;
for(IntWritable v:value){
sum += v.get(