1. hadoop api
  2. http://hadoop.apache.org/docs/r1.0.4/api/org/apache/hadoop/mapreduce/Reducer.html
  1. 改变一下需求:要求“文档词频列表”是经过排序的,即 出现次数高的再前
  2.  
  3. 思路:
  4.  
  5. 代码:
  1. package proj;
  2.  
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import org.apache.hadoop.conf.Configuration;
  8. import org.apache.hadoop.fs.Path;
  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.input.FileSplit;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
  17. import org.apache.hadoop.util.GenericOptionsParser;
  18.  
  19. public class InvertedIndexSortByFreq {
  20.  
  21. // 将词分为<word:num,docid>
  22. public static class InvertedIndexMapper extends
  23. Mapper<Object, Text, Text, Text> {
  24.  
  25. private Text keyInfo = new Text();
  26. private Text valInfo = new Text();
  27. private FileSplit split;
  28.  
  29. public void map(Object key, Text value, Context context)
  30. throws IOException, InterruptedException {
  31. String[] tokens = value.toString().split(" ");
  32. split = (FileSplit) context.getInputSplit();
  33. String docid = split.getPath().getName();
  34. Map<String, Integer> map = new HashMap<String, Integer>();
  35. for (String token : tokens) {
  36. if (map.containsKey(token)) {
  37. Integer newInt = new Integer(map.get(token) + 1);
  38. map.put(token, newInt);
  39. } else {
  40. map.put(token, 1);
  41. }
  42. }
  43. for (String k : map.keySet()) {
  44. Integer num = map.get(k);
  45. keyInfo.set(k + ":" + num);
  46. valInfo.set(docid);
  47. context.write(keyInfo, valInfo);
  48. }
  49. }
  50. }
  51.  
  52. public static class InvertedIndexPartioner extends
  53. HashPartitioner<Text, Text> {
  54.  
  55. private Text term = new Text();
  56.  
  57. public int getPartition(Text key, Text value, int numReduceTasks) {
  58. term.set(key.toString().split(":")[0] + ":" + value);
  59. return super.getPartition(term, value, numReduceTasks);
  60. }
  61. }
  62.  
  63. // 组合成倒排索引文档
  64. public static class InvertedIndexReducer extends
  65. Reducer<Text, Text, Text, Text> {
  66. private Text keyInfo = new Text();
  67.  
  68. private Text valInfo = new Text();
  69.  
  70. private String tPrev = null;
  71.  
  72. private StringBuffer buff = new StringBuffer();
  73.  
  74. public void reduce(Text key, Iterable<Text> values, Context context)
  75. throws IOException, InterruptedException {
  76.  
  77. String[] tokens = key.toString().split(":");
  78. String current = tokens[0];
  79.  
  80. if (tPrev == null) {
  81. tPrev = current;
  82. for (Text val : values) {
  83. buff.append(tokens[1] + ":" + val.toString() + ";");
  84. }
  85. }
  86.  
  87. if(tPrev.equals(current)){
  88. for (Text val : values) {
  89. buff.append(tokens[1] + ":" + val.toString() + ";");
  90. }
  91. }else{
  92. keyInfo.set(tPrev);
  93. valInfo.set(buff.toString());
  94. context.write(keyInfo,valInfo);
  95. tPrev = current;
  96. buff = new StringBuffer();
  97. for (Text val : values) {
  98. buff.append(tokens[1] + ":" + val.toString() + ";");
  99. }
  100. }
  101. }
  102.  
  103. public void cleanup(Context context) throws IOException, InterruptedException{
  104. keyInfo.set(tPrev);
  105. valInfo.set(buff.toString());
  106. context.write(keyInfo,valInfo);
  107. super.cleanup(context);
  108. }
  109.  
  110. }
  111.  
  112. public static void main(String[] args) throws IOException,
  113. ClassNotFoundException, InterruptedException {
  114. Configuration conf = new Configuration();
  115. String[] otherArgs = new GenericOptionsParser(conf, args)
  116. .getRemainingArgs();
  117. Job job = new Job(conf, "InvertedIndex");
  118. job.setJarByClass(InvertedIndex.class);
  119. job.setMapperClass(InvertedIndexMapper.class);
  120. job.setMapOutputKeyClass(Text.class);
  121. job.setMapOutputValueClass(Text.class);
  122. job.setPartitionerClass(InvertedIndexPartioner.class);
  123. job.setReducerClass(InvertedIndexReducer.class);
  124. job.setOutputKeyClass(Text.class);
  125. job.setOutputValueClass(Text.class);
  126. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  127. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  128. System.exit(job.waitForCompletion(true) ? 0 : 1);
  129. }
  130.  
  131. }

mapreduce (三) MapReduce实现倒排索引(二)的更多相关文章

  1. MapReduce(三)

    MapReduce(三) MapReduce(三): 1.关于倒叙排序前10名 1)TreeMap根据key排序 2)TreeSet排序,传入一个对象,排序按照类中的compareTo方法排序 2.写 ...

  2. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  3. 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行

    搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...

  4. mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次

    (总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...

  5. Lucene.Net 2.3.1开发介绍 —— 三、索引(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(二) 2.索引中用到的核心类 在Lucene.Net索引开发中,用到的类不多,这些类是索引过程的核心类.其中Analyzer是索引建立的 ...

  6. Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...

  7. “全栈2019”Java第三十一章:二维数组和多维数组详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. hadoop学习第三天-MapReduce介绍&&WordCount示例&&倒排索引示例

    一.MapReduce介绍 (最好以下面的两个示例来理解原理) 1. MapReduce的基本思想 Map-reduce的思想就是“分而治之” Map Mapper负责“分”,即把复杂的任务分解为若干 ...

  9. hadoop系列三:mapreduce的使用(一)

    转载请在页首明显处注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/7224772.html 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的 ...

随机推荐

  1. Android获取cpu和内存信息、网址的代码

      android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...

  2. JQuery的Ajax跨域请求的

    JQuery的Ajax跨域请求的(Ajax) 什么是jsonp格式呢?API原文:假设获取的数据文件存放在远程server上(域名不同.也就是跨域获取数据),则须要使用jsonp类型.使用这样的类型的 ...

  3. [转] Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  4. Eclipse中使用git把项目导入到osc@git中

    方便Eclipse用户使用Git把项目导入到osc@git中进行管理. Eclipse Git osc@git 一.原有项目:  项目名为TestGit 二.在osc@git上面创建一个新项目库. 填 ...

  5. css中var函数

    引言: 在学习elementui的时候看到一个var.css, 其中写的全部都是以--开头的属性,上google查询不是css3新增的属性,于是决定一探究竟 :root { /* Transition ...

  6. ubuntu 安装 flash player

    ubuntu 软件中心里的 flash , 虽说写的支持 chrome , 但安装总不管用 可运行以下两个命令, 再重启 chrome 浏览器 (注意,一定要把打开的浏览器全部关闭, 再打开才可以) ...

  7. discuz! X3.2 自定义后台门户模块模板里的标签

    这里只提供对源码的修改, 至于插件, 暂不考虑... 想在首页里展示一些自定义字段的内容, 奈何dz无此功能, 无奈去扒源码. 首先切到 source 文件夹下 1. 在 class/block/po ...

  8. H.264视频在android手机端的解码与播放(转)

    随着无线网络和智能手机的发展,智能手机与人们日常生活联系越来越紧密,娱乐.商务应用.金融应用.交通出行各种功能的软件大批涌现,使得人们的生活丰富多彩.快捷便利,也让它成为人们生活中不可取代的一部分.其 ...

  9. json 序列化和反序列化

    Json串的格式: string strDataDiyList={"id":"1","name":"zhangsan", ...

  10. Web字体工具整理,网页图标字体以及使用方法整理

    常用网站app logo 下载: http://www.iconfont.cn/collections/show/268?spm=a313x.7781069.0.0.nDYawz 1.Web字体免费生 ...