1.1.倒排索引 

    根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确

定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(invertedindex)

    例如:单词——文档矩阵(将属性值放在前面作为索引)

    

1.2.MapReduce实现倒排索引

需求:对大量的文本(文档、网页),需要建立搜索索引

代码实现:

package cn.bigdata.hdfs.mr;
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; /**
* 使用MapRedeuce程序建立倒排索引文件
* 文件列表如下:
* a.txt b.txt c.txt
* hello tom hello jerry hello jerry
* hello jerry hello jerry hello tom
* hello tom tom jerry
*/ public class InverIndexStepOne { static class InverIndexStepOneMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
Text k = new Text();
IntWritable v = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
//将得到的每行文本数据根据空格" "进行切分
String [] words = line.split(" "); //根据切片信息获取文件名
FileSplit inputSplit = (FileSplit)context.getInputSplit();
String fileName = inputSplit.getPath().getName();
for(String word : words){
k.set(word + "--" + fileName);
context.write(k, v);
}
}
} static class InverIndexStepOneReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ @Override
protected void reduce(Text key, Iterable<IntWritable> values ,Context context) throws IOException, InterruptedException {
int count = 0;
for(IntWritable value : values){
count += value.get();
}
context.write(key, new IntWritable(count));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(InverIndexStepOne.class); job.setMapperClass(InverIndexStepOneMapper.class);
job.setReducerClass(InverIndexStepOneReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //输入文件路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
}
}

运行结果输出文件:E:\inverseOut\part-r-00000

hello--a.txt    3
hello--b.txt 2
hello--c.txt 2
jerry--a.txt 1
jerry--b.txt 3
jerry--c.txt 1
tom--a.txt 2
tom--b.txt 1
tom--c.txt 1

在原来的基础上进行二次合并,格式如上图单词矩阵,代码实现如下:

package cn.bigdata.hdfs.mr;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.output.FileOutputFormat;
/**
* 对第一次的输出结果进行合并,使得一个value对应的多个文档记录组成一条完整记录
* @author Administrator
*
*/ public class IndexStepTwo { static class IndexStepTwoMapper extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] files = line.split("--");
context.write(new Text(files[0]), new Text(files[1]));
}
} static class IndexStepTwoReducer extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//定义Buffer缓冲数组
StringBuffer sb = new StringBuffer();
for (Text text : values) {
sb.append(text.toString().replace("\t", "-->") + "\t");
}
context.write(key, new Text(sb.toString()));
}
} public static void main(String[] args) throws Exception{
if (args.length < 1 || args == null) {
args = new String[]{"E:/inverseOut/part-r-00000", "D:/inverseOut2"};
} Configuration config = new Configuration();
Job job = Job.getInstance(config); job.setMapperClass(IndexStepTwoMapper.class);
job.setReducerClass(IndexStepTwoReducer.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) ? 1:0);
}
}

运行结果:

hello    c.txt-->2    b.txt-->2    a.txt-->3
jerry c.txt-->1 b.txt-->3 a.txt-->1
tom c.txt-->1 b.txt-->1 a.txt-->2

总结:

  对大量的文档建立索引无非就两个过程,一个是分词,另一个是统计分词在每个文档中出现的次数,根据分词在每个文档

中出现的次数建立索引文件,下次搜索词的时候直接查询索引文件,从而返回文档的摘要等信息;

Hadoop_23_MapReduce倒排索引实现的更多相关文章

  1. Hadoop之倒排索引

    前言: 从IT跨度到DT,如今的数据每天都在海量的增长.面对如此巨大的数据,如何能让搜索引擎更好的工作呢?本文作为Hadoop系列的第二篇,将介绍分布式情况下搜索引擎的基础实现,即“倒排索引”. 1. ...

  2. MapReduce实现倒排索引(类似协同过滤)

    一.问题背景 倒排索引其实就是出现次数越多,那么权重越大,不过我国有凤巢....zf为啥不管,总局回应推广是不是广告有争议... eclipse里ctrl+t找接口或者抽象类的实现类,看看都有啥方法, ...

  3. [Search Engine] 搜索引擎技术之倒排索引

    倒排索引是搜索引擎中最为核心的一项技术之一,可以说是搜索引擎的基石.可以说正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作. 1. 倒排索引的思想 倒排索引源于实际应用中需要根据属 ...

  4. Lucene 工作原理 之倒排索引

      1.简介 倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排 ...

  5. MapReduce实例-倒排索引

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

  6. 倒排索引压缩:改进的PForDelta算法

    由于倒排索引文件往往占用巨大的磁盘空间,我们自然想到对数据进行压缩.同时,引进压缩算法后,使得磁盘占用减少,操作系统在query processing过程中磁盘读取效率也能提升.另外,压缩算法不仅要考 ...

  7. hadoop学习笔记之倒排索引

    开发工具:eclipse 目标:对下面文档phone_numbers进行倒排索引: 13599999999 1008613899999999 12013944444444 13800138000137 ...

  8. 【hadoop2.6.0】倒排索引遇到问题了

    想实现书上倒排索引的例子,但是我不会java想用C++写,如果用hadoop streaming 那么输入必须是标准输入的形式, 那么我怎么获得每个文件的文件名呢? 查了一下,还有一种方法叫做hado ...

  9. hadoop倒排索引

    1.前言 学习hadoop的童鞋,倒排索引这个算法还是挺重要的.这是以后展开工作的基础.首先,我们来认识下什么是倒拍索引: 倒排索引简单地就是:根据单词,返回它在哪个文件中出现过,而且频率是多少的结果 ...

随机推荐

  1. LODOP中用ADD_PRINT_IMAGE缩放非图片超文本

    LODOP中HTML,URL,带有img标签的IMAGE,TBALE等打印项都属于超文本.关于LODOP中的纯文本和超文本,可查看本博客相关博文:LODOP中的纯文本和超文本打印项 ADD_PRINT ...

  2. 【ARTS】01_27_左耳听风-201900513~201900519

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  3. SpringBoot: 14.异常处理方式4(使用SimpleMappingExceptionResolver处理异常)(转)

    修改异常处理方法3中的全局异常处理Controller即可 package bjsxt.exception; import org.springframework.context.annotation ...

  4. Hbuilder第三方插件开发demo--第三方授权分享支付,推送等

    <template> <view class="content"> <button id="loginByWX" @click=& ...

  5. appium(api操作)

    driver.current_activity #获取当前activity driver.current_package #获取包名 driver.lock(seconds=2) #息屏 #收起虚拟键 ...

  6. Spring mybatis源码篇章-动态SQL基础语法以及原理

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...

  7. SQL SERVER导入EXCEL文件:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序

    1.安装相关组件  2.程序生成属性32位改为64位

  8. NIT校赛-- 雷顿女士与分队

    题意:https://ac.nowcoder.com/acm/contest/2995/D 思路: 和最大子串很像,dp[i]=max(dp[i-1]+a[i],a[i]),要不和前面连一起,要不就是 ...

  9. Photon Server初识(六) --- 客户端与服务端消息传递

    前一章客户端与服务端连接成功,现在需要前后端进行数据传递. 一.前端发送消息.在项目Scripts目录中新建脚本 TestSer.cs.并挂载到相机上 二.客户端发送数据给服务端.编辑客户端代码 Te ...

  10. taskverse学习

    简介 taskverse是<linux二进制分析>一书作者编写的一个隐藏进程的检测工具,它使用/proc/kcore来访问内核内存,github的地址在这里:https://github. ...