本文地址:http://www.cnblogs.com/archimedes/p/mapreduce-inverted-index.html,转载请注明源地址。

1.倒排索引简介

倒排索引(Inverted index),也常被称为反向索引置入档案反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。

有两种不同的反向索引形式:

  • 一条记录的水平反向索引(或者反向档案索引)包含每个引用单词的文档的列表。
  • 一个单词的水平反向索引(或者完全反向索引)又包含每个单词在一个文档中的位置。

后者的形式提供了更多的兼容性(比如短语搜索),但是需要更多的时间和空间来创建。

举例:

以英文为例,下面是要被索引的文本:

  • T0 = "it is what it is"
  • T1 = "what is it"
  • T2 = "it is a banana"

我们就能得到下面的反向文件索引:

 "a":      {2}
"banana": {2}
"is": {0, 1, 2}
"it": {0, 1, 2}
"what": {0, 1}

检索的条件"what""is" 和 "it" 将对应这个集合:{0,1}∩{0,1,2}∩{0,1,2}={0,1}。

对相同的文字,我们得到后面这些完全反向索引,有文档数量和当前查询的单词结果组成的的成对数据。 同样,文档数量和当前查询的单词结果都从零开始。

所以,"banana": {(2, 3)} 就是说 "banana"在第三个文档里 (T2),而且在第三个文档的位置是第四个单词(地址为 3)。

"a":      {(2, 2)}
"banana": {(2, 3)}
"is": {(0, 1), (0, 4), (1, 1), (2, 1)}
"it": {(0, 0), (0, 3), (1, 2), (2, 0)}
"what": {(0, 2), (1, 0)}

如果我们执行短语搜索"what is it" 我们得到这个短语的全部单词各自的结果所在文档为文档0和文档1。但是这个短语检索的连续的条件仅仅在文档1得到。

2.分析和设计

(1)Map过程

首先使用默认的TextInputFormat类对输入文件进行处理,得到文本中每行的偏移量及其内容,Map过程首先必须分析输入的<key, value>对,得到倒排索引中需要的三个信息:单词、文档URI和词频,如图所示:

存在两个问题,第一:<key, value>对只能有两个值,在不使用Hadoop自定义数据类型的情况下,需要根据情况将其中的两个值合并成一个值,作为value或key值;

第二,通过一个Reduce过程无法同时完成词频统计和生成文档列表,所以必须增加一个Combine过程完成词频统计

public static class InvertedIndexMapper extends Mapper<Object, Text, Text, Text> {
private Text keyInfo = new Text(); //存储单词和URI的组合
private Text valueInfo = new Text();//存储词频
private FileSplit split; //存储Split对象 public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
//获得<key,value>对所属的FileSplit对象
split = (FileSplit)context.getInputSplit();
StringTokenizer itr = new StringTokenizer(value.toString()); while(itr.hasMoreTokens()) {
//key值由单词和URI组成,如"MapReduce:1.txt"
keyInfo.set(itr.nextToken() + ":" + split.getPath().toString());
// 词频初始为1
valueInfo.set("1");
context.write(keyInfo, valueInfo);
}
}
}

(2)Combine过程

将key值相同的value值累加,得到一个单词在文档中的词频,如图

public static class InvertedIndexCombiner extends Reducer<Text, Text, Text, Text> {
private Text info = new Text();
public void reduce(Text key, Iterable<Text>values, Context context) throws IOException, InterruptedException {
//统计词频
int sum = 0;
for(Text value : values) {
sum += Integer.parseInt(value.toString());
}
int splitIndex= key.toString().indexOf(":"); //重新设置value值由URI和词频组成
info.set(key.toString().substring(splitIndex + 1) + ":" + sum);
//重新设置key值为单词
key.set(key.toString().substring(0, splitIndex));
context.write(key, info);
}
}

(3)Reduce过程

讲过上述两个过程后,Reduce过程只需将相同key值的value值组合成倒排索引文件所需的格式即可,剩下的事情就可以直接交给MapReduce框架进行处理了

public static class InvertedIndexReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reducer(Text key, Iterable<Text>values, Context context) throws IOException, InterruptedException {
//生成文档列表
String fileList = new String();
for(Text value : values) {
fileList += value.toString() + ";";
}
result.set(fileList);
context.write(key, result);
}
}

完整代码如下:

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
import org.apache.hadoop.util.GenericOptionsParser; public class InvertedIndex {
public static class InvertedIndexMapper extends Mapper<Object, Text, Text, Text> {
private Text keyInfo = new Text();
private Text valueInfo = new Text();
private FileSplit split; public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
split = (FileSplit)context.getInputSplit();
StringTokenizer itr = new StringTokenizer(value.toString()); while(itr.hasMoreTokens()) {
keyInfo.set(itr.nextToken() + ":" + split.getPath().toString());
valueInfo.set("1");
context.write(keyInfo, valueInfo);
}
} }
public static class InvertedIndexCombiner extends Reducer<Text, Text, Text, Text> {
private Text info = new Text();
public void reduce(Text key, Iterable<Text>values, Context context) throws IOException, InterruptedException {
int sum = 0;
for(Text value : values) {
sum += Integer.parseInt(value.toString());
}
int splitIndex= key.toString().indexOf(":");
info.set(key.toString().substring(splitIndex + 1) + ":" + sum);
key.set(key.toString().substring(0, splitIndex));
context.write(key, info);
}
}
public static class InvertedIndexReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reducer(Text key, Iterable<Text>values, Context context) throws IOException, InterruptedException {
String fileList = new String();
for(Text value : values) {
fileList += value.toString() + ";";
}
result.set(fileList);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "InvertedIndex");
job.setJarByClass(InvertedIndex.class);
job.setMapperClass(InvertedIndexMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setCombinerClass(InvertedIndexCombiner.class);
job.setReducerClass(InvertedIndexReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

参考资料

http://zh.wikipedia.org/wiki/%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95

《实战Hadop:开启通向云计算的捷径.刘鹏》

MapReduce实战--倒排索引的更多相关文章

  1. MapReduce的倒排索引

    MapReduce的倒排索引 索引: 什么是索引:索引(Index)是帮助数据库高效获取数据的数据结构.索引是在基于数据库表创建的,它包含一个表中某些列的值以及记录对应的地址,并且把这些值存储在一个数 ...

  2. 利用MapReduce实现倒排索引

    这里来学习的是利用MapReduce的分布式编程模型来实现简单的倒排索引. 首先什么是倒排索引? 倒排索引是文档检索中最常用的数据结构,被广泛地应用于全文搜索引擎. 它主要是用来存储某个单词(或词组) ...

  3. Hadoop实战-MapReduce之倒排索引(八)

    倒排索引 (就是key和Value对调的显示结果) 一.需求:下面是用户播放音乐记录,统计歌曲被哪些用户播放过 tom        LittleApple jack       YesterdayO ...

  4. MapReduce实战(四)倒排索引的实现

    需求: 以上三个文件,用MapReduce进行处理,最终输出以下格式: hello c.txt-->2 b.txt-->2 a.txt-->3jerry c.txt-->1 b ...

  5. MapReduce实例-倒排索引

    环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境 数据:任意数量.格式的文本文件(我用的四个.java代码文件) 方案目标: 根据提供的文本文件,提取出每个单词在哪个文件 ...

  6. 《OD大数据实战》MapReduce实战

    一.github使用手册 1. 我也用github(2)——关联本地工程到github 2. Git错误non-fast-forward后的冲突解决 3. Git中从远程的分支获取最新的版本到本地 4 ...

  7. MapReduce实战:统计不同工作年限的薪资水平

    1.薪资数据集 我们要写一个薪资统计程序,统计数据来自于互联网招聘hadoop岗位的招聘网站,这些数据是按照记录方式存储的,因此非常适合使用 MapReduce 程序来统计. 2.数据格式 我们使用的 ...

  8. mapreduce实战:统计美国各个气象站30年来的平均气温项目分析

    气象数据集 我们要写一个气象数据挖掘的程序.气象数据是通过分布在美国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程序来 ...

  9. mapreduce (三) MapReduce实现倒排索引(二)

    hadoop api http://hadoop.apache.org/docs/r1.0.4/api/org/apache/hadoop/mapreduce/Reducer.html 改变一下需求: ...

随机推荐

  1. cocos2dx各个版本下载地址

    https://code.google.com/archive/p/cocos2d-x/downloads?page=1 各种工具包括 NDK 8 https://github.com/fusijie ...

  2. 使用fastadmin系统自带的图片上传plupload

    首先,form表单需要具有如下代码 <form class="form-horizontal" role="form" method="POST ...

  3. c++ 单例模式研究

    一篇博文:C++ 单例模式的几种实现研究 中 看到的几段代码 懒汉模式 class Singleton { public: static Singleton* GetInstance() { if ( ...

  4. 20169211《Linux内核原理与分析》第三周作业

    假期中抽时间学习了一下linux内核的启动过程,在此做一下学习总结. Linux启动过程描述: 1.启动BootLoader 2.Linux系统的初始化 3.Linux的应用程序的初始化 通用寄存器的 ...

  5. 趴一趴京东的Ajax动态价格页面

    AJAX,异步加载技术!!! 之前在网上看过很多朋友有一种疑问,为什么在看京东网页的源代码里面看不到价格或则折扣一类的数据,而在网页上正常显示却能看到?...之前我也没有想到是AJAX,因为我写写爬虫 ...

  6. Socket学习笔记(一)

    1.socket介绍 我们知道两个进程如果需要进行通讯最基本的一个前提能能够唯一的标示一个进程,在本地进程通讯中我们可以使用PID来唯一标示一个进程,但PID只在本地唯一,网络中的两个进程PID冲突几 ...

  7. 「ZJOI2018」历史

    「ZJOI2018」历史 前置知识 \(\text{LCT}\) 维护子树信息,考虑辅助树上一个节点的子树信息只是其代表的这一段链的信息,设 \(S(u)\) 为节点 \(u\) 的子树信息,那么在辅 ...

  8. 51nod1821 最优集合 贪心

    首先考虑一个集合的最大优美值怎么求出 考虑新增一个数,假设我们现在的优美值已经达到了$V$,那么只需要一个$[1, V + 1]$的数就可以使$V$达到更大 为了保证能添加尽可能多的数进来,我们这么构 ...

  9. 【计算几何】【二分图判定】Gym - 101485C - Cleaning Pipes

    题意:有n个水井,每个水井发出一些管线(都是线段),然后每条管线上最多只有一个水井.所有从不同的水井发出的管线的相交点都是清洁点(不存在清洁点是大于两条管线点的交点).你需要在某些管线上放出一些机器人 ...

  10. 【裸裸的左偏树】BZOJ1455-罗马游戏

    [题目大意] 给出一些数和一些操作.M:合并两个数所在的集合,如果有任意一个数被删除则忽略操作:K:删除某个数所在集合中最小的数. [思路] 裸裸的,复习^ ^ #include<iostrea ...