当前位置:   article > 正文

【大数据技术Hadoop+Spark】MapReduce之单词计数和倒排索引实战(附源码和数据集 超详细)_大数据 单词计数

大数据 单词计数

源码和数据集请点赞关注收藏后评论区留言私信~~~

一、统计单词出现次数

单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版“Hello World。其主要功能是统计一系列文本文件中每个单词出现的次数

程序解析

首先MapReduce将文件拆分成splits,由于测试用的文件较小,只有二行文字,所以每个文件为一个split,并将文件按行分割形成<key, value>对,如下图所示,这一步由MapReduce框架自动完成,其中偏移量(即key值)包括了回车所占的字符数(Windows和Linux环境会不同)

 (2)将分割好的<key, value>对交给用户定义的Map方法进行处理,生成新的<key, value>对

(3)得到Map方法输出的<key,value>对后,Mapper会将它们按照key值进行排序,并执行Combine过程,将key至相同value值累加,得到Mapper的最终输出结果 

(4)Reducer先对从Mapper接收的数据进行排序,再交由用户自定义的Reduce方法进行处理,得到新的<key,value>对,并作为WordCount的输出结果

 

主要编写Map和Reduce函数.这个Map函数使用StringTokenizer函数对字符串进行分隔,通过write方法把单词存入word中 k/v来自于Map函数中的context,可能经过了进一步处理(combiner),同样通过context输出

运行程序后通过浏览器访问页面即可获取结果

代码如下

  1. package com.bigdata.wc;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Job;
  7. import org.apache.hadoop.mapreduce.Mapper;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  11. import org.apache.hadoop.util.GenericOptionsParser;
  12. import java.io.IOException;
  13. import java.util.StringTokenizer;
  14. public class WordCount {
  15. public static class TokenizerMapper
  16. extends Mapper<Object, Text, Text, IntWritable> {
  17. private final static IntWritable one = new IntWritable(1);
  18. private Text word = new Text();
  19. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  20. StringTokenizer itr = new StringTokenizer(value.toString());
  21. while (itr.hasMoreTokens()) {
  22. word.set(itr.nextToken());
  23. System.out.println(word);
  24. context.write(word, one);
  25. }
  26. }
  27. }
  28. public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  29. private IntWritable result = new IntWritable();
  30. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  31. int sum = 0;
  32. for (IntWritable val : values) {
  33. sum += val.get();
  34. }
  35. result.set(sum);
  36. context.write(key, result);
  37. }
  38. }
  39. public static void main(String[] args) throws Exception {
  40. System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.0");
  41. System.setProperty("HADOOP_USER_NAME", "root");
  42. Configuration conf = new Configuration();
  43. conf.set("yarn.resourcemanager.address", "bigdata01:8032");
  44. conf.set("dfs.client.use.datanode.hostname", "true");
  45. conf.set("fs.defaultFS", "hdfs://bigdata02:9000/");
  46. conf.set("mapreduce.app-submission.cross-platform", "true");
  47. conf.set("mapreduce.framework.name", "yarn");
  48. conf.set("mapred.jar","D:\\hadoopdemo\\WordCount\\target\\WordCount-1.0-SNAPSHOT-jar-with-dependencies.jar");
  49. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  50. if (otherArgs.length < 2) {
  51. System.err.println("Usage: wordcount <in> [<in>...] <out>");
  52. System.exit(2);
  53. }
  54. Job job = Job.getInstance(conf, "word count");
  55. job.setJarByClass(WordCount.class);
  56. job.setMapperClass(TokenizerMapper.class);
  57. job.setCombinerClass(IntSumReducer.class);
  58. job.setReducerClass(IntSumReducer.class);
  59. job.setOutputKeyClass(Text.class);
  60. job.setOutputValueClass(IntWritable.class);
  61. for (int i = 0; i < otherArgs.length - 1; ++i) {
  62. FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
  63. }
  64. FileOutputFormat.setOutputPath(job,
  65. new Path(otherArgs[otherArgs.length - 1]));
  66. System.exit(job.waitForCompletion(true) ? 0 : 1);
  67. }
  68. }

 二、倒排索引

倒排索引是文档检索系统中最常用的数据结构,被广泛应用于全文搜索引擎。倒排索引主要用来存储某个单词(或词组)在一组文档中的存储位置的映射,提供了可以根据内容来查找文档的方式,而不是根据文档来确定内容,因此称为倒排索引(Inverted Index)。带有倒排索引的文件我们称为倒排索引文件,简称倒排文件(Inverted File)。

现假设有三个源文件file1.txt、file2.txt和file3.txt,需要使用倒排索引的方式对这三个源文件内容实现倒排索引,并将最后的倒排索引文件输出。

 首先,使用默认的TextInputFormat类对每个输入文件进行处理,得到文本中每行的偏移量及其内容。Map过程首先分析输入的<key,value>键值对,经过处理可以得到倒排索引中需要的三个信息:单词、文档名称和词频。

经过Map阶段数据转换后,同一个文档中相同的单词会出现多个的情况,而单纯依靠后续Reduce阶段无法同时完成词频统计和生成文档列表,所以必须增加一个Combine阶段,先完成每一个文档的词频统计。

 

经过上述两个阶段的处理后,Reduce阶段只需将所有文件中相同key值的value值进行统计,并组合成倒排索引文件所需的格式。

 

 效果测试如下

 

 部分代码如下 全部代码和数据集请点赞关注收藏后评论区留言私信

  1. package com.mr.InvertedIndex;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Reducer;
  5. public class InvertedIndexReducer extends Reducer<Text, Text, Text, Text> {
  6. private static Text result = new Text();
  7. // 输入:<MapReduce file3:2>
  8. // 输出:<MapReduce file1:1;file2:1;file3:2;>
  9. @Override
  10. protected void reduce(Text key, Iterable<Text> values, Context context)
  11. throws IOException, InterruptedException {
  12. // 生成文档列表
  13. String fileList = new String();
  14. for (Text value : values) {
  15. fileList += value.toString() + ";";
  16. }
  17. result.set(fileList);
  18. context.write(key, result);
  19. }
  20. }

创作不易 觉得有帮助请点赞关注收藏~~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/337656
推荐阅读
相关标签
  

闽ICP备14008679号