定义Mapper实现
WordCountMapper extends Mapper
public class Mapper {
......
}
KEYIN : mapping 输入 key 的类型,即每行的偏移量offset(每行第一个字符在整个文本中的位置),Long 类型,对应 Hadoop 中的 LongWritable 类型;
VALUEIN : mapping 输入 value 的类型, 即其实就是一行行的字符串,即每行数据;String 类型,对应 Hadoop 中 Text 类型;
KEYOUT :mapping 输出的 key 的类型,即每个单词;String 类型,对应 Hadoop 中 Text 类型;
VALUEOUT:mapping 输出 value 的类型,即每个单词出现的次数;这里用 int 类型,对应 IntWritable 类型。
WordCountMapper
package com.bigdata.hadoop.mr.wc;
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;
public class WordCountMapper extends Mapper {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//把value按照指定的分割符分开
String[] words=value.toString().split("\t");
for(String word: words){
context.write(new Text(word),new IntWritable(1));
}
}
}
自定义Reducer实现
public class WordCountReducer extends Reducer {
@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
}
}
Text数据类型 :字符串类型 String
IntWritable : reduce阶段的输入类型 int
Text : reduce阶段的输出数据类型 String类型
IntWritable : 输出词频个数 In