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. swift 第十四课 可视化view: @IBDesignable 、@IBInspectable

    以前应objctiew-c 写项目的时候,就知道有这两个关键字,现在用swift了.用法稍作改变,基本用法还是一致的 虽然使用这个之后,有时候会报错的非常的莫名其妙----(其实还是自己技术不够牛…… ...

  2. UE4 RHI与条件式编译

    RHI即RenderHardwareInterface, 即渲染硬件接口, 是UE为实现跨平台而实现的一套API. 每个RHI接口都为OpenGL, Vulkan, DX11等做了不同的实现. 在引擎 ...

  3. Java文章翻译

    一.基础 1.String 使用" "还是构造函数创建字符串? 画图说明字符串的不变性 在Java中字符串为什么是不可变的 Java中的字符串是按引用传递? 排名前十的Java字符 ...

  4. linux中alarm函数和pause函数详解实例

    alarm(time);执行之后告诉内核,让内核在time秒时间之后向该进程发送一个定时信号,然后该进程捕获该信号并处理:pause()函数使该进程暂停让出CPU,但是该函数的暂停和前面的那个slee ...

  5. java实现List<People>的排序

    1.首先新建测试的实体类(People类): import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsCon ...

  6. msyql 主从切换

    从库是192.168.1.70 ,主库是192.168.1.64,主从切换一次 即:主库是192.168.1.70,从库是192.168.1.64 1.从库上执行,修改为主 修改从库为非只读库修改配置 ...

  7. (五)mybatis开发dao层

    目录 SqlSession 是线程不安全的 原始 dao 开发方法 Mapper 代理方法 关于代理对象 SqlSession 是线程不安全的 SqlSession 是 线程不安全 的: 对于它,我们 ...

  8. Block Breaker HDU - 6699(深搜,水,写下涨涨记性)

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  9. codeforces 1249D1/D2 Too Many Segments (贪心)

    (点击此处查看原题) 题意说明 有n个区间,第i个区间覆盖范围[li,ri]内所有点,问删除最少哪些区间,使得所有点被区间覆盖的次数少于等于k次 解题思路 看到这个题的时候,觉得和开关(反转)问题很像 ...

  10. H5网页唤醒app,判断app安装

    在阅读本文之前你首先应该对js有基本对掌握,并且对Scheme,intent有一定的理解.更多的是代码 上午给朋友做了一个产品引导页,但是需要判断ios系统的TestFlight是否安装,进行了goo ...