image.png

  • map阶段的核心:把输入的数据经过切割,全部标记1。因此输出就是<单词,1>。

shuffle阶段核心:经过默认的排序分区分组,key相同的单词会作为一组数据构成新的kv对。

  • reduce阶段核心:处理shuffle完的一组数据,该组数据就是该单词所有的键值对。对所有的1

开发环境

image.png

程序实现

  1. //map过程
  2. //创建静态类继承Mapper给上输入输出形参
  3. public static class WordCountMap extends Mapper<LongWritable,Text,Text,LongWritable>{
  4. @Override //重写map方法
  5. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  6. //每一次获取一行数据
  7. String line = value.toString();//通过values获取一行数据
  8. context.write(new Text(line),new LongWritable(1));
  9. }
  10. }