一、准备测试数据

1、在本地Linux系统/var/lib/Hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容如下图所示:

2、在hdfs中,准备/input路径,并上传两个文件file1.txt和file2.txt,如下图所示:

二、编写代码,封装Jar包并上传至linux

将代码封装成TestMapReduce.jar,并上传至linux的/usr/local路径下,如下图所示:

三、运行命令

执行命令如下:hadoop jar /usr/local/TestMapReduce.jar com.jngreen.mapreduce.test.WordCount /input/file1.txt /input/file2.txt /output/output

命令执行过程截图如下:

四、查看运行结果

查看hdfs输出路径/output下的结果,如下图所示:

运行结果为Hello 4、Hadoop 1、Man 1、Boy 1、Word 1,完全正确!

五、WordCount展示

源码如下:

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. public class WordCount {
  13. // TokenizerMapper作为Map阶段,需要继承Mapper,并重写map()函数
  14. public static class TokenizerMapper
  15. extends Mapper<Object, Text, Text, IntWritable>{
  16. private final static IntWritable one = new IntWritable(1);
  17. private Text word = new Text();
  18. public void map(Object key, Text value, Context context
  19. ) throws IOException, InterruptedException {
  20. // 用StringTokenizer作为分词器,对value进行分词
  21. StringTokenizer itr = new StringTokenizer(value.toString());
  22. // 遍历分词后结果
  23. while (itr.hasMoreTokens()) {
  24. // 将String设置入Text类型word
  25. word.set(itr.nextToken());
  26. // 将(word,1),即(Text,IntWritable)写入上下文context,供后续Reduce阶段使用
  27. context.write(word, one);
  28. }
  29. }
  30. }
  31. // IntSumReducer作为Reduce阶段,需要继承Reducer,并重写reduce()函数
  32. public static class IntSumReducer
  33. extends Reducer<Text,IntWritable,Text,IntWritable> {
  34. private IntWritable result = new IntWritable();
  35. public void reduce(Text key, Iterable<IntWritable> values,
  36. Context context
  37. ) throws IOException, InterruptedException {
  38. int sum = 0;
  39. // 遍历map阶段输出结果中的values中每个val,累加至sum
  40. for (IntWritable val : values) {
  41. sum += val.get();
  42. }
  43. // 将sum设置入IntWritable类型result
  44. result.set(sum);
  45. // 通过上下文context的write()方法,输出结果(key, result),即(Text,IntWritable)
  46. context.write(key, result);
  47. }
  48. }
  49. public static void main(String[] args) throws Exception {
  50. // 加载hadoop配置
  51. Configuration conf = new Configuration();
  52. // 校验命令行输入参数
  53. if (args.length < 2) {
  54. System.err.println("Usage: wordcount <in> [<in>...] <out>");
  55. System.exit(2);
  56. }
  57. // 构造一个Job实例job,并命名为"word count"
  58. Job job = new Job(conf, "word count");
  59. // 设置jar
  60. job.setJarByClass(WordCount.class);
  61. // 设置Mapper
  62. job.setMapperClass(TokenizerMapper.class);
  63. // 设置Combiner
  64. job.setCombinerClass(IntSumReducer.class);
  65. // 设置Reducer
  66. job.setReducerClass(IntSumReducer.class);
  67. // 设置OutputKey
  68. job.setOutputKeyClass(Text.class);
  69. // 设置OutputValue
  70. job.setOutputValueClass(IntWritable.class);
  71. // 添加输入路径
  72. for (int i = 0; i < args.length - 1; ++i) {
  73. FileInputFormat.addInputPath(job, new Path(args[i]));
  74. }
  75. // 添加输出路径
  76. FileOutputFormat.setOutputPath(job,
  77. new Path(args[args.length - 1]));
  78. // 等待作业job运行完成并退出
  79. System.exit(job.waitForCompletion(true) ? 0 : 1);
  80. }
  81. }

最新文章

  1. podfile The dependency `` is not used in any concrete target
  2. Surprise团队第一周项目总结
  3. [转]ASP.NET中使用UpdatePanel实现局部异步刷新方法和攻略
  4. BuildingAssetBundles in 5.x
  5. Python中的join()函数split()函数
  6. stl string常用函数
  7. gulp管理静态资源缓存
  8. Linux-2.6.25 TCPIP函数调用大致流程
  9. C# - 设计模式 - 模板模式
  10. Java GC相关知识
  11. java0425 wen IO
  12. git push时报错:Updates were rejected because the tip of your current branch is behind
  13. FlatList 核心运用
  14. Centos 7.6配置nginx反向代理,直接yum安装
  15. [ZJOI2016]小星星&amp;[SHOI2016]黑暗前的幻想乡(容斥)
  16. QML-关于Qt.rgba()颜色无法正常显示问题
  17. docker安装和使用
  18. linux文件软链接、硬链接
  19. 访问Oracle数据库的工具【unfinished】
  20. Spring Boot fastJSON的使用

热门文章

  1. Java中堆和栈有什么区别
  2. [51Nod1487]占领资源
  3. C# 事件和Unity3D
  4. 代理模式(Proxy)--动态代理(CGLIB)
  5. Calendar对象
  6. Android TextView 阴影效果(投影)
  7. SONY的几款秋季新品都还是很不错的
  8. C++ development cross platforms
  9. FIREDAC记录SQL日志
  10. FIREDAC驱动MYSQL数据库