wordCount &简易知识点

总体概述

MapReduce 总共分为map和reduce 一个分一个合 本次主要记录代码的含义和书写
我是写在一个文件夹里面进行的 并没有分成三个文件夹 接下来我会粘贴三部分得代码 和总体代码

总体三层

数据类型变化
1 基本基础知识&WordCount - 图1

map

  1. public static class MapperDemo extends Mapper<LongWritable,Text, Text, IntWritable>{
  2. /**
  3. * 声明MapperDemo类 继承 org.apache.hadoop.mapreduce.Mapper; 必须继承这个类
  4. * 继承这个类需要输入泛型 <KEYIN, VALUEIN, KEYOUT, VALUEOUT>
  5. * 这个案列分别是
  6. * KEYIN 编号 int or long 注意java和mapreduce有区别 不能直接给int和 Long
  7. 应该是 IntWritable or IntWritable
  8. * VALUEIN 一行数据 string 应该给 Text
  9. * KEYOUT 应该是一个单词 Text
  10. * VALUEOUT 1 IntWritable
  11. */
  12. // v2
  13. private final IntWritable one = new IntWritable(1);
  14. // 声明Text对象 用于存放key2
  15. Text text = new Text();
  16. @Override
  17. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  18. // 声明一个分词对象 传入参数 string:一行数据(value.toString) dim: "," 按照什么分割
  19. StringTokenizer tokenizer = new StringTokenizer(value.toString(), ",");
  20. // 分割完成就是一个可迭代得 通过while循环 构建k2 v2
  21. while (tokenizer.hasMoreTokens()){
  22. // 分词出来得单词转换成Text
  23. this.text.set(tokenizer.nextToken());
  24. // 传入上下文对象 比如 "hello, 1"
  25. context.write(text, one);
  26. }
  27. }
  28. }

reduce

  1. public static class ReduceDemo extends Reducer<Text, IntWritable, Text, IntWritable>{
  2. /**
  3. * 此功能和map很像 继承得类变成了 org.apache.hadoop.mapreduce.Reducer;
  4. * 也需要实现泛型 <> k2 v2 k3 v3
  5. * 示例 reduce方法首先会把同一个k得value放在一起 比如hello <1,1,1,1,>
  6. 最后得出得结果就是 hello, 4
  7. */
  8. private IntWritable count = new IntWritable();
  9. @Override
  10. protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  11. // 用于计数
  12. int sum = 0;
  13. // 循环迭代 获取value
  14. for (IntWritable value : values) {
  15. // .get() 获取方法
  16. sum += value.get();
  17. count.set(sum);
  18. }
  19. // 放入上下文中
  20. context.write(key, count);
  21. }
  22. }

job提交main

  1. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  2. System.setProperty("hadoop.home.dir", "H:\\winutils\\winutils-master\\hadoop-2.6.0");
  3. // Configuration
  4. Configuration conf = new Configuration();
  5. // 生成job
  6. Job job = Job.getInstance(conf, "word count");
  7. // 指定打包类
  8. job.setJarByClass(JobMainTwo.class);
  9. // 指定map类
  10. job.setMapperClass(JobMainTwo.MapperDemo.class);
  11. // 输出得key 和 value 数据类型
  12. job.setMapOutputKeyClass(Text.class);
  13. job.setMapOutputValueClass(IntWritable.class);
  14. job.setCombinerClass(JobMainTwo.ReduceDemo.class);
  15. // 指定reduce类
  16. job.setReducerClass(JobMainTwo.ReduceDemo.class);
  17. // 输出得key 和 value 数据类型
  18. job.setOutputKeyClass(Text.class);
  19. job.setOutputValueClass(IntWritable.class);
  20. // 读取文件的路径
  21. FileInputFormat.addInputPath(job,new Path("data/java/hello.txt"));
  22. // 输出文件得路径
  23. FileOutputFormat.setOutputPath(job,new Path("data/java/hello-put2"));
  24. // 输出运行结果
  25. System.exit(job.waitForCompletion(true)?0:1);
  26. }

数据结构

hello,world
hello,haha
hello,hadoop

完整代码

  1. package MapReduce_one;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.StringTokenizer;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15. import org.apache.hadoop.util.GenericOptionsParser;
  16. public class JobMainTwo {
  17. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  18. System.setProperty("hadoop.home.dir", "H:\\winutils\\winutils-master\\hadoop-2.6.0");
  19. // Configuration
  20. Configuration conf = new Configuration();
  21. // 生成job
  22. Job job = Job.getInstance(conf, "word count");
  23. // 指定打包类
  24. job.setJarByClass(JobMainTwo.class);
  25. // 指定map类
  26. job.setMapperClass(JobMainTwo.MapperDemo.class);
  27. // 输出得key 和 value 数据类型
  28. job.setMapOutputKeyClass(Text.class);
  29. job.setMapOutputValueClass(IntWritable.class);
  30. job.setCombinerClass(JobMainTwo.ReduceDemo.class);
  31. // 指定reduce类
  32. job.setReducerClass(JobMainTwo.ReduceDemo.class);
  33. // 输出得key 和 value 数据类型
  34. job.setOutputKeyClass(Text.class);
  35. job.setOutputValueClass(IntWritable.class);
  36. // 读取文件的路径
  37. FileInputFormat.addInputPath(job,new Path("data/java/hello.txt"));
  38. // 输出文件得路径
  39. FileOutputFormat.setOutputPath(job,new Path("data/java/hello-put2"));
  40. // 输出运行结果
  41. System.exit(job.waitForCompletion(true)?0:1);
  42. }
  43. public static class MapperDemo extends Mapper<LongWritable,Text, Text, IntWritable>{
  44. /**
  45. * 声明MapperDemo类 继承 org.apache.hadoop.mapreduce.Mapper; 必须继承这个类
  46. * 继承这个类需要输入泛型 <KEYIN, VALUEIN, KEYOUT, VALUEOUT>
  47. * 这个案列分别是
  48. * KEYIN 编号 int or long 注意java和mapreduce有区别 不能直接给int和 Long 应该是 IntWritable or IntWritable
  49. * VALUEIN 一行数据 string 应该给 Text
  50. * KEYOUT 应该是一个单词 Text
  51. * VALUEOUT 1 IntWritable
  52. */
  53. private final IntWritable one = new IntWritable(1);
  54. Text text = new Text();
  55. @Override
  56. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  57. StringTokenizer tokenizer = new StringTokenizer(value.toString(), ",");
  58. while (tokenizer.hasMoreTokens()){
  59. this.text.set(tokenizer.nextToken());
  60. context.write(text, one);
  61. }
  62. }
  63. }
  64. public static class ReduceDemo extends Reducer<Text, IntWritable, Text, IntWritable>{
  65. /**
  66. * 此功能和map很像 继承得类变成了 org.apache.hadoop.mapreduce.Reducer;
  67. * 也需要实现泛型 <> k2 v2 k3 v3
  68. * 示例 reduce方法首先会把同一个k得value放在一起 比如hello <1,1,1,1,> 最后得出得结果就是 hello, 4
  69. */
  70. private IntWritable count = new IntWritable();
  71. @Override
  72. protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  73. int sum = 0;
  74. for (IntWritable value : values) {
  75. sum += value.get();
  76. count.set(sum);
  77. }
  78. context.write(key, count);
  79. }
  80. }
  81. }

错误纠正

输出得文件不能重复 如果重复则需要删除 否则报错