一、倒排索引简单介绍

倒排索引(英语: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}

对于中文分词,能够使用开源的中文分词工具,这里使用ik-analyzer。

准备几个文本文件,写入内容做測试。

file1.txt内容例如以下:

其实我们发现,互联网裁员潮频现甚至要高于其它行业领域

file2.txt内容例如以下:

面对寒冬,互联网企业不得不调整人员结构,优化雇员的投入产出

file3.txt内容例如以下:

在互联网内部,因为内部竞争机制以及要与竞争对手拼进度

file4.txt内容例如以下:

互联网大公司职员尽管能够从复杂性和专业分工中受益
互联网企业不得不调整人员结构

二、加入依赖

出了hadoop主要的jar包意外。加入中文分词的lucene-analyzers-commonik-analyzers


<!--Lucene分词模块-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>6.0.0</version>
</dependency> <!--IK分词 -->
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>ik-analyzers</artifactId>
<version>5.1.0</version>
</dependency>

三、MapReduce程序

关于Lucene 6.0中IK分词的配置參考http://blog.csdn.net/napoay/article/details/51911875,MapReduce程序例如以下。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map; /**
* Created by bee on 4/4/17.
*/
public class InvertIndexIk { public static class InvertMapper extends Mapper<Object, Text, Text, Text> { public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String filename = ((FileSplit) context.getInputSplit()).getPath().getName()
.toString();
Text fname = new Text(filename);
IKAnalyzer6x analyzer = new IKAnalyzer6x(true);
String line = value.toString();
StringReader reader = new StringReader(line);
TokenStream tokenStream = analyzer.tokenStream(line, reader);
tokenStream.reset();
CharTermAttribute termAttribute = tokenStream.getAttribute
(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
Text word = new Text(termAttribute.toString());
context.write(word, fname);
}
} } public static class InvertReducer extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values,Reducer<Text,Text,
Text,Text>.Context context) throws IOException, InterruptedException {
Map<String, Integer> map = new HashMap<String, Integer>();
for (Text val : values) {
if (map.containsKey(val.toString())) { map.put(val.toString(),map.get(val.toString())+1); } else {
map.put(val.toString(),1);
} }
int termFreq=0;
for (String mapKey:map.keySet()){
termFreq+=map.get(mapKey);
}
context.write(key,new Text(map.toString()+" "+termFreq));
} } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { HadoopUtil.deleteDir("output");
Configuration conf=new Configuration(); String[] otherargs=new
String[]{"input/InvertIndex",
"output"}; if (otherargs.length!=2){
System.err.println("Usage: mergesort <in> <out>");
System.exit(2);
} Job job=Job.getInstance();
job.setJarByClass(InvertIndexIk.class);
job.setMapperClass(InvertIndexIk.InvertMapper.class);
job.setReducerClass(InvertIndexIk.InvertReducer.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); }
}

四、执行结果

输出例如以下:

专业分工    {file4.txt=1}  1
中 {file4.txt=1} 1
其实 {file1.txt=1} 1
互联网 {file1.txt=1, file3.txt=1, file4.txt=2, file2.txt=1} 5
人员 {file4.txt=1, file2.txt=1} 2
企业 {file4.txt=1, file2.txt=1} 2
优化 {file2.txt=1} 1
内部 {file3.txt=2} 2
发现 {file1.txt=1} 1
受益 {file4.txt=1} 1
复杂性 {file4.txt=1} 1
大公司 {file4.txt=1} 1
寒冬 {file2.txt=1} 1
投入产出 {file2.txt=1} 1
拼 {file3.txt=1} 1
潮 {file1.txt=1} 1
现 {file1.txt=1} 1
竞争对手 {file3.txt=1} 1
竞争机制 {file3.txt=1} 1
结构 {file4.txt=1, file2.txt=1} 2
职员 {file4.txt=1} 1
行业 {file1.txt=1} 1
裁员 {file1.txt=1} 1
要与 {file3.txt=1} 1
调整 {file4.txt=1, file2.txt=1} 2
进度 {file3.txt=1} 1
雇员 {file2.txt=1} 1
面对 {file2.txt=1} 1
领域 {file1.txt=1} 1
频 {file1.txt=1} 1
高于 {file1.txt=1} 1

结果有三列。依次为词项、词项在单个文件里的词频以及总的词频。

五、參考资料

1.https://zh.wikipedia.org/wiki/ 倒排索引

2. Lucene 6.0下使用IK分词器

MapReduce编程(七) 倒排索引构建的更多相关文章

  1. [置顶] MapReduce 编程之 倒排索引

    本文调试环境: ubuntu 10.04 , hadoop-1.0.2 hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktra ...

  2. MapReduce编程之倒排索引

    任务要求: //输入文件格式 18661629496 110 13107702446 110 1234567 120 2345678 120 987654 110 2897839274 1866162 ...

  3. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)

    下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...

  4. 批处理引擎MapReduce编程模型

    批处理引擎MapReduce编程模型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MapReduce是一个经典的分布式批处理计算引擎,被广泛应用于搜索引擎索引构建,大规模数据处理 ...

  5. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  6. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  7. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  8. MapReduce编程模型及其在Hadoop上的实现

    转自:https://www.zybuluo.com/frank-shaw/note/206604 MapReduce基本过程 关于MapReduce中数据流的传输过程,下图是一个经典演示:  关于上 ...

  9. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

随机推荐

  1. 【个人专用&入门级】LAMP一键安装包

    最近自学了下Shell编程,也算是入门吧!按照如下教程,编译安装了LAMP(Apache-2.4.6 + MySQL-5.5.25 + PHP-5.3.27) CentOS6.3编译安装LAMP(1) ...

  2. BabyLinux制作过程详解

    转:http://www.360doc.com/content/05/0915/14/1429_12641.shtml BabyLinux制作过程详解 作者:GuCuiwen email:win2li ...

  3. [Linux] Proc 文件系统

    转载自:http://linux.chinaunix.net/doc/2004-10-05/16.shtml#324lfindex0 目录: /proc --- 一个虚拟文件系统 加载 proc 文件 ...

  4. java界面编程(3) ------ 控制布局

    本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在java 中,组件放置在窗口上的方式可能与其它的GUI系统都不同样.首先,它全 ...

  5. WAF攻击与防御

    背景 对于腾讯的业务来说,有两个方面决定着WAF能否发挥效果,一个是合适处理海量流量的架构,另一个关键因素则是规则系统.架构决定着WAF能否承受住海量流量的挑战,这个在之前的篇章中简单介绍过(详情见主 ...

  6. 预防U盘被病毒侵害的方法

    写在前面:此方法只能杜绝自己的u盘免收侵害,而不能杜绝自己的电脑免收其他u盘病毒的侵害,如果想知道如何让自己的电脑防止被u盘病毒侵害,可以阅读此文章:https://www.cnblogs.com/t ...

  7. Parallax Occlusion Mapping in GLSL [转]

    http://www.sunandblackcat.com/tipFullView.php?topicid=28   This lesson shows how to implement differ ...

  8. Google Python 命名规范

    Google Python命名规范 module_name,  模块 package_name,  包 ClassName,  类 method_name,  方法 ExceptionName,    ...

  9. 把文章里边的html标签去掉(去掉文字的样式,显示css设置的样式)

    java: validHeasers.add(tr[0].replace("<(.[^>]*)>",""));//去除HTML标签

  10. 十个书写Node.js REST API的最佳实践(下)

    收录待用,修改转载已取得腾讯云授权 5. 对你的Node.js REST API进行黑盒测试 测试你的REST API最好的方法之一就是把它们当成黑盒对待. 黑盒测试是一种测试方法,通过这种方法无需知 ...