1. 1 思路:
    0.txt MapReduce is simple
    1.txt MapReduce is powerfull is simple
    2.txt Hello MapReduce bye MapReduce
  2.  
  3. 1 map函数:context.write(word:docid, 1) 即将word:docid作为map函数的输出
    输出key 输出value
    MapReduce:0.txt 1
    is:0.txt 1
    simple:0.txt 1
    Mapreduce:1.txt 1
    is:1.txt 1
    powerfull:1.txt 1
    is:1.txt 1
    simple:1.txt 1
    Hello:2.txt 1
    MapReduce:2.txt 1
    bye:2.txt 1
    MapReduce:2.txt 1
    2 combine函数:相同keyword:docid)的进行合并操作,然后context.write(word, docid:count),即将word做为输出keydocidcount作为输出value
    输入key 输出value 输出key 输出value
    MapReduce:0.txt 1 => MapReduce 0.txt:1
    is:0.txt 1 => is 0.txt:1
    simple:0.txt 1 => simple 0.txt:1
    Mapreduce:1.txt 1 => Mapreduce 1.txt:1
    is:1.txt 2 => is 1.txt:2
    powerfull:1.txt 1 => powerfull 1.txt:1
    simple:1.txt 1 => simple 1.txt:1
    Hello:2.txt 1 => Hello 2.txt:1
    MapReduce:2.txt 2 => MapReduce 2.txt:2
    bye:2.txt 1 => bye 2.txt:1
    3 Partitioner函数:HashPartitioner
    略,根据combine的输出key进行分区
    4 Reducer函数:仅仅是组合字符串了
    输出key 输出value
    MapReduce 0.txt:11.txt:1 2.txt:2
    is 0.txt:1is 1.txt:2
    simple 0.txt:11.txt:1
    powerfull 1.txt:1
    Hello 2.txt:1
    bye 2.txt:1

//感觉这个地方是 有问题的,Combiner相当于一个本地的reduce,万一如果某个文件大于64M(hadoop 2.x 是128M) 怎么办呢?会不会一个文件分到两个split中呢 那样在这里统计<word_docid, count>是不是会出现问题呢?
  //为了确保不出问题,可以采用两个mapreduce 任务实现。http://www.cnblogs.com/i80386/p/3600174.html
  combiner是把同一个机器上的多个map的结果先聚合一次

  1. 2 代码如下:
    package proj;
  2.  
  3. import java.io.IOException;
  4. import java.util.StringTokenizer;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15. import org.apache.hadoop.util.GenericOptionsParser;
  16.  
  17. public class InvertedIndex {
  18.  
  19. public static class InvertedIndexMapper extends
  20. Mapper<Object, Text, Text, Text> {
  21.  
  22. private Text keyInfo = new Text();
  23. private Text valueInfo = new Text();
  24. private FileSplit split;
  25.  
  26. public void map(Object key, Text value, Context context)
  27. throws IOException, InterruptedException {
  28.  
  29. split = (FileSplit) context.getInputSplit();
  30.  
  31. StringTokenizer itr = new StringTokenizer(value.toString());
  32. while (itr.hasMoreTokens()) {
  33. keyInfo.set(itr.nextToken() + ":" + split.getPath().toString());
  34. valueInfo.set("1");
  35. context.write(keyInfo, valueInfo);
  36. }
  37. }
  38. }
  39.   
  40.  
  41. //感觉这个地方是有问题的,Combiner相当于一个本地的reduce,万一如果某个文件大于64M(hadoop 2.x 是128M) 怎么办呢?会不会一个文件分到两个split中呢 那样在这里统计<word_docid, count>是不是会出现问题呢?
    //为了确保不出问题,可以采用两个mapreduce 任务实现。http://www.cnblogs.com/i80386/p/3600174.html
  42. public static class InvertedIndexCombiner extends
  43. Reducer<Text, Text, Text, Text> {
  44.  
  45. private Text info = new Text();
  46.  
  47. public void reduce(Text key, Iterable<Text> values, Context context)
  48. throws IOException, InterruptedException {
  49. int sum = 0;
  50. for (Text value : values) {
  51. sum += Integer.parseInt(value.toString());
  52. }
  53. int splitIndex = key.toString().indexOf(":");
  54. info.set(key.toString().substring(splitIndex + 1) + ":" + sum);
  55. key.set(key.toString().substring(0, splitIndex));
  56. context.write(key, info);
  57. }
  58. }
  59.  
  60. public static class InvertedIndexReducer extends
  61. Reducer<Text, Text, Text, Text> {
  62. private Text result = new Text();
  63.  
  64. public void reduce(Text key, Iterable<Text> values, Context context)
  65. throws IOException, InterruptedException {
  66. StringBuffer buff = new StringBuffer();
  67. for (Text val : values) {
  68. buff.append(val.toString() + ";");
  69. }
  70. result.set(buff.toString());
  71. context.write(key, result);
  72. }
  73.  
  74. }
  75.  
  76. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  77. Configuration conf = new Configuration();
  78. String[] otherArgs = new GenericOptionsParser(conf, args)
  79. .getRemainingArgs();
  80. Job job = new Job(conf, "InvertedIndex");
  81. job.setJarByClass(InvertedIndex.class);
  82. job.setMapperClass(InvertedIndexMapper.class);
  83. job.setMapOutputKeyClass(Text.class);
  84. job.setMapOutputValueClass(Text.class);
  85. job.setCombinerClass(InvertedIndexCombiner.class);
  86. job.setReducerClass(InvertedIndexReducer.class);
  87. job.setOutputKeyClass(Text.class);
  88. job.setOutputValueClass(Text.class);
  89. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  90. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  91. System.exit(job.waitForCompletion(true) ? 0 : 1);
  92. }
  93.  
  94. }

运行结果如下:

  1. Hello hdfs://localhost:9000/user/root/in/2.txt:1;
  2. MapReduce hdfs://localhost:9000/user/root/in/2.txt:2;hdfs://localhost:9000/user/root/in/0.txt:1;hdfs://localhost:9000/user/root/in/1.txt:1;
  3. bye hdfs://localhost:9000/user/root/in/2.txt:1;
  4. is hdfs://localhost:9000/user/root/in/0.txt:1;hdfs://localhost:9000/user/root/in/1.txt:2;
  5. powerfull hdfs://localhost:9000/user/root/in/1.txt:1;
  6. simple hdfs://localhost:9000/user/root/in/1.txt:1;hdfs://localhost:9000/user/root/in/0.txt:1;
  7.  
  8. 0.txt MapReduce is simple
  9. 1.txt MapReduce is powerfull is simple
  10. 2.txt Hello MapReduce bye MapReduce

mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次的更多相关文章

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

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

  2. hadoop(二MapReduce)

    hadoop(二MapReduce) 介绍 MapReduce:其实就是把数据分开处理后再将数据合在一起. Map负责“分”,即把复杂的任务分解为若干个“简单的任务”来并行处理.可以进行拆分的前提是这 ...

  3. java大数据最全课程学习笔记(6)--MapReduce精通(二)--MapReduce框架原理

    目前CSDN,博客园,简书同步发表中,更多精彩欢迎访问我的gitee pages 目录 MapReduce精通(二) MapReduce框架原理 MapReduce工作流程 InputFormat数据 ...

  4. Hadoop学习笔记: MapReduce二次排序

    本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...

  5. (转)MapReduce二次排序

    一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...

  6. MapReduce教程(二)MapReduce框架Partitioner分区<转>

    1 Partitioner分区 1.1 Partitioner分区描述 在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,按照手机号码段划分的话,需要把同一手机号码段的数据放 ...

  7. mapreduce二次排序详解

    什么是二次排序 待排序的数据具有多个字段,首先对第一个字段排序,再对第一字段相同的行按照第二字段排序,第二次排序不破坏第一次排序的结果,这个过程就称为二次排序. 如何在mapreduce中实现二次排序 ...

  8. 详细讲解MapReduce二次排序过程

    我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...

  9. 二 MapReduce 各阶段流程分析

    如果想要将问题变得清晰.精准和优雅, 需要关注 MapReduce 作业所需要的系统资源,尤其是集群内部网络资源使用情况. MR 可以运行在共享集群上处理 TB 级 甚至 PB 级的数据.同时,改作业 ...

随机推荐

  1. JSTL和select标签的组合使用

    1.用于根据不同的值显示对应的内容,不能选择 <select name="grade"> <c:choose> <c:when test=" ...

  2. 多台计算机之间的ssh无密钥登录

    在很多分布式系统中,我们最常遇到的一个问题是,需要在服务器集群上保证多台机器之间的SSH无密钥登录.以Hadoop为例,为了方便,我们需要在master和slaves之间配置密钥登录,这样我们启动Ha ...

  3. [转]windows10 64位环境下安装mysql5.7.17

    今天以zip模式在windows10 64位环境下安装mysql5.7,到最后一步提示mysql服务无法启动. 安装步骤如下: 1.配置环境变量 我的电脑->属性->高级->环境变量 ...

  4. 数据库框架 Litepal

    1.导包 dependencies {   compile 'org.litepal.android:core:1.4.1' } 2.在asstes中建立litepal.xml文件 <?xml ...

  5. php 数组排序 (转)

    // @param array $list 查询结果 // @param string $field 排序的字段名 // @param array $sortby 排序类型 // asc正向排序 de ...

  6. golang中channel的超时处理

    并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...

  7. Day10 - Python协程、异步IO、redis缓存、rabbitMQ队列

    Python之路,Day9 - 异步IO\数据库\队列\缓存   本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...

  8. IIS相关问题

    问题:使用vs开发项目完成后,发布在本地IIS上,访问链接出现如下情况: 解决方案:打开IIS--->>

  9. CSS基本知识介绍

    CSS (Cascading Style Sheet)叠层样式表.用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言. 样式的几种控制方法: 1.行内样式         <div ...

  10. Ci 简单分页,保证能实现

    某晚,自己写项目的时候去找资料,关于CI分页的, 发现百度出来的前几名的基本都是写的都是垃圾, 要么是实现不了,要么就是坑逼 所以我自己在这里写一个,不是很完美,只是说是简单的实现了原理 有了最基本的 ...