先来简单介绍一下什么是文档倒排索引
倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上。主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来查找文档的方式。
简单点来讲呢,就是根据内容找文章。
 
倒排索引的概念说明白了,就该说说怎么用MapReduce实现。
测试数据奉上:
file1:MapReduce is simple
file2:MapReduce is powerful is simple
file3:Hello MapReduce bye MapReduce 
输出的结果:
Hello    file3.txt:1;
MapReduce    file3.txt:2;file:2.txt:1;file1.txt:1;
bye    file"3.txt:1;
is    file2.txt:2;file1.txt:1; 
powerful    file2.txt:1;
simple    file2.txt:1;file1.txt:1;
 
设计思路
map、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
context.write("MapReduce:file1", 1)  context.write("is:file1", 1)  context.write("simple:file1", 1)  context.write("MapReduce :file2", 1)  context.write("is:file2", 1)  context.write("powerful :file2", 1) context.write("is:file2", 1) 
 
<"MapReduce:file1", {1}> <"is:file1", {1}> <"simple:file1", {1}>  <"simple:file1", {1}>  <"is:file2",  {1, 1}> ..........................
combine、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
context.write("MapReduce" , "file1:1") context.write("is" , "file1:1")  context.write("simple" , "file1:1")  context.write("MapReduce " , "file2:1")  context.write("is" , "file2:2")................................
<"MapReduce",{ "file1:1","file2:1"}>  <"is",{ "file1:1","file2:2"}>   <"simple",{ "file1:1"}> .......................
reduce、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
context.write("MapReduce","file1:1,file2:1")..................................
 
这个过程中的Combine是不可插拔的,也就是不可以省略的,因为它和Reduce的业务逻辑不一样。
 
代码奉上
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat; public class InvertedIndex { public static class InvertedMap extends
Mapper<LongWritable, Text, Text, IntWritable> {
private Text kText = new Text();
private IntWritable vIntWritable = new IntWritable(1);
private FileSplit split; @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] lineSplit = line.split("\t");
// 获取文档名称
split = (FileSplit) context.getInputSplit();
int indexOfFile = split.getPath().toString().indexOf("file");
String fileName = split.getPath().toString().substring(indexOfFile); for (int i = 0; i < lineSplit.length; i++) {
kText.set(lineSplit[i] + ":" + fileName);
context.write(kText, vIntWritable);
} } } public static class InvertedConbine extends
Reducer<Text, IntWritable, Text, Text> {
private Text kText = new Text();
private Text vText = new Text(); protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
// 词频统计
int sum = 0;
for (IntWritable v : values) {
sum += v.get();
}
int indexOf = key.toString().indexOf(":");
kText.set(key.toString().substring(0, indexOf));
vText.set(key.toString().substring(indexOf + 1) + ":" + sum);
context.write(kText, vText); } } public static class InvertedReduce extends Reducer<Text, Text, Text, Text> {
private Text vText = new Text(); protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String filelist = new String();
for (Text v : values) {
filelist += v.toString() + ";";
}
vText.set(filelist);
context.write(key, vText);
} } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(InvertedIndex.class); job.setMapperClass(InvertedMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setCombinerClass(InvertedConbine.class); job.setReducerClass(InvertedReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)? 0:1);
} }
 

Hadoop-Map/Reduce实现实现倒排索引的更多相关文章

  1. Hadoop Map/Reduce教程

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...

  2. 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)

    Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...

  3. Hadoop Map/Reduce

    Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集.一个Map/Reduce ...

  4. Hadoop Map/Reduce 示例程序WordCount

    #进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...

  5. Hadoop Map/Reduce的工作流

    问题描述 我们的数据分析平台是单一的Map/Reduce过程,由于半年来不断地增加需求,导致了问题已经不是那么地简单,特别是在Reduce阶段,一些大对象会常驻内存.因此越来越顶不住压力了,当前内存问 ...

  6. (转载)Hadoop map reduce 过程获取环境变量

    来源:http://www.linuxidc.com/Linux/2012-07/66337.htm   作者: lmc_wy Hadoop任务执行过程中,在每一个map节点或者reduce节点能获取 ...

  7. Hadoop map reduce 任务数量优化

    mapred.tasktracker.map.tasks.maximum 官方解释:The maximum number of map tasks that will be run  simultan ...

  8. hadoop2.2编程:自定义hadoop map/reduce输入文件切割InputFormat

    hadoop会对原始输入文件进行文件切割,然后把每个split传入mapper程序中进行处理,FileInputFormat是所有以文件作为数据源的InputFormat实现的基类,FileInput ...

  9. hadoop map reduce 实例wordcount的使用

    hadoop jar hadoop-mapreduce-examples-2.7.3.jar wordcount /wordcount.txt /wc/output3

  10. Hadoop学习:Map/Reduce初探与小Demo实现

    原文地址:https://blog.csdn.net/liyong199012/article/details/25423221 一.    概念知识介绍 Hadoop MapReduce是一个用于处 ...

随机推荐

  1. Tomcat的SessionID引起的Session Fixation和Session Hijacking问题

    上一篇说到<Spring MVC防御CSRF.XSS和SQL注入攻击>,今天说说SessionID带来的漏洞攻击问题.首先,什么是Session Fixation攻击和Session Hi ...

  2. mac用virtualbox 装win7联网及分辨率设置

    1.关掉虚拟机,进入设置 2.选择网络->NAT方式->高级->控制芯片选择 1000MT 桌面,其他默认勾选 通过安装增强功能,可以随意改变分辨率,但是系统和远程连接后,东西都被缩 ...

  3. QTP之对测试用例的自动化过程的分解

    第一部分:自动化一个测试用例 当你要开始自动化一个测试用例的时候,有一些重要的事情需要完成.当你完成所有这些事情的时候,测试脚本的自动化也随之完成. 在这里,我们将首先在大部分的自动化测试用例里找出所 ...

  4. PHP优化杂烩

    讲 PHP 优化的文章往往都是教大家如何编写高效的代码,本文打算从另一个角度来讨论问题,教大家如何配置高效的环境,如此同样能够达到优化的目的. pool 一个让人沮丧的消息是绝大多数 PHP 程序员都 ...

  5. 车牌识别LPR(一)-- 研究背景

    在年尾用了几天的时间将2014年的所有工作都总结了一遍,将之前的文档综合了下. 以下是LPR系统,车牌识别的一些总结资料. 第一篇:LPR研究背景 汽车的出现改变了以往出行徒步和以马代步的时代,极大地 ...

  6. [转载]浅析Java中的final关键字

    浅析Java中的final关键字 谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来 ...

  7. iOS开发:插件记录

    进入沙盒的插件 https://github.com/TongeJie/ZLGotoSandboxPlugin 图片提示的插件 https://github.com/ksuther/KSImageNa ...

  8. jquery获取div距离顶部的距离

    获取元素到页面顶部距离的语句为: 1.jquery写法:$(“#divID”).offset().top //推荐 $("#vertical").position().top 2. ...

  9. C#计算程序执行速度

    long t1 = DateTime.Now.Ticks; //执行程序,例如处理100个文件 long t2 = DateTime.Now.Ticks; Response.Write("执 ...

  10. IONIC beta.14 版本变更一览

    由网友(58758323)提供 重构 视图缓存 之前用户一旦在应用程序中执行导航动作,每个退出的视图元素和scope都会被销毁.如果相同的视图再次被访问,应用程序会重新生成元素.现在,视图可以被缓存以 ...