当前位置:   article > 正文

hadoop 学习:mapreduce 入门案例一:WordCount 统计一个文本中单词的个数_wordcount示例程序统计word.txt中的单

wordcount示例程序统计word.txt中的单

一 需求

这个案例的需求很简单

现在这里有一个文本wordcount.txt,内容如下

现要求你使用 mapreduce 框架统计每个单词的出现个数 

这样一个案例虽然简单但可以让新学习大数据的同学熟悉 mapreduce 框架

二 准备工作

(1)创建一个 maven 工程,maven 工程框架可以选择quickstart

(2)在properties中添加 hadoop.version,导入依赖,pom.xml内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>maven_hadoop</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.11</version>
  14. <scope>test</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.hadoop</groupId>
  18. <artifactId>hadoop-common</artifactId>
  19. <version>${hadoop.version}</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.hadoop</groupId>
  23. <artifactId>hadoop-hdfs</artifactId>
  24. <version>${hadoop.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.hadoop</groupId>
  28. <artifactId>hadoop-mapreduce-client-core</artifactId>
  29. <version>${hadoop.version}</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.hadoop</groupId>
  33. <artifactId>hadoop-mapreduce-client-common</artifactId>
  34. <version>${hadoop.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.apache.hadoop</groupId>
  38. <artifactId>hadoop-client</artifactId>
  39. <version>${hadoop.version}</version>
  40. </dependency>
  41. </dependencies>
  42. <properties>
  43. <maven.compiler.source>8</maven.compiler.source>
  44. <maven.compiler.target>8</maven.compiler.target>
  45. <hadoop.version>3.1.3</hadoop.version>
  46. </properties>
  47. </project>

(3)准备数据,创建两个文件夹 in,out(一个是输入文件,一个是输出文件),输入文件放在 in 文件夹中

三 编写 WordCountMapper 类

  1. import org.apache.hadoop.io.IntWritable;
  2. import org.apache.hadoop.io.LongWritable;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Mapper;
  5. import java.io.IOException;
  6. // <0, hello java, hello, 1 >
  7. // <0, hello java, java, 1 >
  8. // alt + ins
  9. public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
  10. Text text = new Text();
  11. IntWritable intWritable = new IntWritable();
  12. @Override
  13. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  14. System.out.println("WordCountMap stage Key:"+key+" Value:"+value);
  15. String[] words = value.toString().split(" "); // "hello java"--->[hello,java]
  16. for (String word :
  17. words) {
  18. text.set(word);
  19. intWritable.set(1);
  20. context.write(text,intWritable); //<hello,1>,<java,1>
  21. }
  22. }
  23. }

四 编写 WordCountReducer 类

  1. import org.apache.hadoop.io.IntWritable;
  2. import org.apache.hadoop.io.LongWritable;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Reducer;
  5. import java.io.IOException;
  6. public class WordCountReduce extends Reducer<Text, IntWritable, Text, LongWritable> {
  7. @Override
  8. protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  9. System.out.println("Reduce stage Key:" + key + " Values:" + values.toString());
  10. int count = 0;
  11. for (IntWritable intWritable :
  12. values) {
  13. count+=intWritable.get();
  14. }
  15. LongWritable longWritable = new LongWritable(count);
  16. System.out.println("ReduceResult key:"+key+" resultValue:"+longWritable.get());
  17. context.write(key,longWritable);
  18. }
  19. }

五 编写WordCountDriver 类

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.FileSystem;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.LongWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  10. import java.io.IOException;
  11. public class WordCountDriver {
  12. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  13. Configuration conf = new Configuration();
  14. Job job = Job.getInstance(conf);
  15. job.setJarByClass(WordCountDriver.class);
  16. // 设置job的map阶段 工作任务
  17. job.setMapperClass(WordCountMapper.class);
  18. job.setMapOutputKeyClass(Text.class);
  19. job.setMapOutputValueClass(IntWritable.class);
  20. // 设置job的reduce阶段 工作任务
  21. job.setReducerClass(WordCountReduce.class);
  22. job.setOutputKeyClass(Text.class);
  23. job.setOutputValueClass(LongWritable.class);
  24. // 指定job map阶段的输入文件的路径
  25. FileInputFormat.setInputPaths(job, new Path("D:\\bigdataworkspace\\kb23\\hadoopstu\\in\\wordcount.txt"));
  26. // 指定job reduce阶段的输出文件路径
  27. Path path = new Path("D:\\bigdataworkspace\\kb23\\hadoopstu\\out1");
  28. FileSystem fileSystem = FileSystem.get(path.toUri(), conf);
  29. if (fileSystem.exists(path))
  30. fileSystem.delete(path,true);
  31. FileOutputFormat.setOutputPath(job, path);
  32. // 启动job
  33. job.waitForCompletion(true);
  34. }
  35. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/337619
推荐阅读
相关标签
  

闽ICP备14008679号