(总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html   combiner是把同一个机器上的多个map的结果先聚合一次
现重新实现一个:
思路:
第一个mapreduce仅仅做 <word_docid,count>的统计,即某个单词在某一篇文章里出现的次数。(原理跟wordcount一样,只是word变成了word_docid)
第二个mapreduce将word_docid在map阶段拆开,重新组合为<word,docid_count> 然后在combine和reduce阶段(combine和reduce是同一个函数)组合为 <word,doc1:count1,doc2:count2,doc3:count3>这种格式import java.io.IOException;
1 思路:
0.txt MapReduce is simple
1.txt MapReduce is powerfull is simple
2.txt Hello MapReduce bye MapReduce 采用两个JOB的形式实现
一:第一个JOB(跟wordcount一致,只是wordcount中的word换做了word:dicid)
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 Partitioner函数:HashPartitioner
略,根据map函数的输出key(word:docid)进行分区
3 reduce函数:累加输入values
输出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
二:第二个JOB
1 map函数:
输入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
2 reduce函数 (组合values)
输出key 输出value
MapReduce 0.txt:1,1.txt:1 2.txt:2
is 0.txt:1,is 1.txt:2
simple 0.txt:1,1.txt:1
powerfull 1.txt:1
Hello 2.txt:1
bye 2.txt:1
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer; public class MyInvertIndex { public static class SplitMapper extends
Mapper<Object, Text, Text, IntWritable> { public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
FileSplit split = (FileSplit) context.getInputSplit();
//String pth = split.getPath().toString();
String name = split.getPath().getName();
String[] tokens = value.toString().split("\\s");
for (String token : tokens) {
context.write(new Text(token + ":" + name), new IntWritable(1));
}
}
} public static class CombineMapper extends
Mapper<Text, IntWritable, Text, Text> { public void map(Text key, IntWritable value, Context context)
throws IOException, InterruptedException {
int splitIndex = key.toString().indexOf(":");
context.write(new Text(key.toString().substring(0, splitIndex)),
new Text(key.toString().substring(splitIndex + 1) + ":"
+ value.toString()));
}
} public static class CombineReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
StringBuffer buff = new StringBuffer();
for (Text val : values) {
buff.append(val.toString() + ",");
}
context.write(key, new Text(buff.toString()));
}
} public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException { String dir_in = "hdfs://localhost:9000/in_invertedindex";
String dir_out = "hdfs://localhost:9000/out_invertedindex"; Path in = new Path(dir_in);
Path out = new Path(dir_out);
Path path_tmp = new Path("word_docid"
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE))); Configuration conf = new Configuration(); try {
Job countJob = new Job(conf, "invertedindex_count"); countJob.setJarByClass(MyInvertIndex.class); countJob.setInputFormatClass(TextInputFormat.class);
countJob.setMapperClass(SplitMapper.class);
countJob.setCombinerClass(IntSumReducer.class);
countJob.setPartitionerClass(HashPartitioner.class);
countJob.setMapOutputKeyClass(Text.class);
countJob.setMapOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(countJob, in); countJob.setReducerClass(IntSumReducer.class);
// countJob.setNumReduceTasks(1);
countJob.setOutputKeyClass(Text.class);
countJob.setOutputValueClass(IntWritable.class);
countJob.setOutputFormatClass(SequenceFileOutputFormat.class); FileOutputFormat.setOutputPath(countJob, path_tmp); countJob.waitForCompletion(true); Job combineJob = new Job(conf, "invertedindex_combine"); combineJob.setJarByClass(MyInvertIndex.class); combineJob.setInputFormatClass(SequenceFileInputFormat.class);
combineJob.setMapperClass(CombineMapper.class);
combineJob.setCombinerClass(CombineReducer.class);
combineJob.setPartitionerClass(HashPartitioner.class);
combineJob.setMapOutputKeyClass(Text.class);
combineJob.setMapOutputValueClass(Text.class); FileInputFormat.addInputPath(combineJob, path_tmp); combineJob.setReducerClass(CombineReducer.class);
// combineJob.setNumReduceTasks(1);
combineJob.setOutputKeyClass(Text.class);
combineJob.setOutputValueClass(Text.class);
combineJob.setOutputFormatClass(TextOutputFormat.class); FileOutputFormat.setOutputPath(combineJob, out); combineJob.waitForCompletion(true); } finally {
FileSystem.get(conf).delete(path_tmp, true);
} }
}
运行结果:
Hello 2.txt:1,,
MapReduce 2.txt:2,1.txt:1,0.txt:1,,
bye 2.txt:1,,
is 1.txt:2,0.txt:1,,
powerfull 1.txt:1,,
simple 1.txt:1,0.txt:1,,


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

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

    1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...

  2. MapReduce(五) mapreduce的shuffle机制 与 Yarn

    一.shuffle机制 1.概述 (1)MapReduce 中, map 阶段处理的数据如何传递给 reduce 阶段,是 MapReduce 框架中最关键的一个流程,这个流程就叫 Shuffle:( ...

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

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

  4. [C语言]声明解析器cdecl修改版

    一.写在前面 K&R曾经在书中承认,"C语言声明的语法有时会带来严重的问题.".由于历史原因(BCPL语言只有唯一一个类型——二进制字),C语言声明的语法在各种合理的组合下 ...

  5. Medoo个人修改版

    Medoo是一款轻量级的php数据库操作类,下面不会介绍Medoo的使用方法,想学习Medoo请前往官网自学:http://medoo.in/ 在接触Medoo之前,一直是用自己写的php数据库操作类 ...

  6. Android 仿美团网,大众点评购买框悬浮效果之修改版

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...

  7. 黄聪:WordPress图片插件:Auto Highslide修改版(转)

    一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...

  8. sqm(sqlmapGUI) pcat修改版

    sqlmap是一款开源的注入工具,支持几乎所有的数据库,支持get/post/cookie注入,支持错误回显注入/盲注,还有其他多种注入方法. 支持代理,指纹识别技术判断数据库 .而sqm(sqlma ...

  9. 转载:Eclipse+Spket插件+ExtJs4修改版提供代码提示功能[图]

    转载:Eclipse+Spket插件+ExtJs4修改版提供代码提示功能[图] ExtJs是一种主要用于创建前端用户界面,是一个基本与后台技术无关的前端ajax框架.功能丰富,无人能出其右.无论是界面 ...

随机推荐

  1. DataGridView 添加行 分类: DataGridView 2014-12-07 08:49 263人阅读 评论(0) 收藏

    说明: (1)dgvGoods 是DataGridView名 (2)index 是最大行索引 一. DataGridViewRow row = new DataGridViewRow(); int i ...

  2. UML 的基本组成

    UML 是由UML构造块.规则.通用机制三部分组成的.而UML构造块由建模元素(事物).关系和图组成. 建模元素 建模元素是对模型中最具有代表性的成分的抽象.一般情况下,将建模元素分为结构元素.行为元 ...

  3. cocos2d与cocos2d-X中的draw和update

    像其它的游戏引擎一样,我们有两个不同的方法来完成draw和update. 1: Draw:每一个CCNode都有一个draw方法,每一帧都会调用.我们只在这个方法里做描绘的事情. 2: Update: ...

  4. cubietruck+ vncserver+source+wlan0 分类: cubieboard 2014-11-08 17:25 159人阅读 评论(0) 收藏

    正常ubuntu下直接搜索remote desktop 进行配置,好用而且友好.. modprobe bcmdhd lsmod ifconfig wlan0 up vi /etc/network/in ...

  5. clock_gettime测代码运行时间

    //函数原型: // long clock_gettime (clockid_t which_clock, struct timespec *tp); //参数列表: // CLOCK_REALTIM ...

  6. Qt数据库sqlite总结

      QSqlDatabase类实现了数据库连接的操作QSqlQuery类用来执行SQL语句QSqlRecord类封装数据库所有记录QSqlRelationalTableModelQSqlQueryMo ...

  7. Java基础知识强化13:Java中单例模式案例使用(懒汉式)

    1.古往今来历史上皇帝通常只有一人.为了保证其唯一性,古人采用增加"防伪标识"的办法,如玉玺.更为简单的办法就是限制皇帝的创建.本案例中就是使用单例模式从而保证皇帝的唯一性.实例运 ...

  8. iOS viewController 和 view 的创建消失生命周期总结

    控制器创建的生命周期 1. 如果从stroryBoard 中产生一个controller,那么会先调用initWithCoder:, awakeFromNib, loadView,viewDidLoa ...

  9. 各大浏览器CSS Hack收集

    各大浏览器CSS Hack收集  >>>>>>>>>>>>>>>>>>>>> ...

  10. Java 单文件下载及重命名

    代码(仅供参考): /* * 另存为 */ @RequestMapping("/saveAs.do") public @ResponseBody void saveAs(Strin ...