java map 倒序索引_MapReduce 案例之倒排索引

本文详细介绍了如何使用Java实现MapReduce构建倒排索引,包括Map、Combine和Reduce过程,以及如何根据词频对文档进行排序。通过案例展示了如何处理输入数据,组合关键词和文档URL,统计词频,并生成最终的倒排索引文件。
摘要由CSDN通过智能技术生成

1. 倒排索引

倒排索引是文档检索系统中最常用的数据结构,被广泛地应用于全文搜索引擎。 它主要是用来存储某个单词(或词组) 在一个文档或一组文档中的存储位置的映射,即提供了一种根据内容来查找文档的方式。由于不是根据文档来确定文档所包含的内容,而是进行相反的操作,因而称为倒排索引( Inverted Index)。

2. 实例描述

通常情况下,倒排索引由一个单词(或词组)以及相关的文档列表组成,文档列表中的文档或者是标识文档的 ID 号,或者是指文档所在位置的 URL。如下图所示:

c12ca6b1d94c

image.png

从上图可以看出,单词 1 出现在{文档 1,文档 5,文档 13, ……}中,单词 2 出现在{文档 2,文档 3,文档 5, ……}中,而单词 3 出现在{文档 2,文档 10,文档 16, ……}中。在实际应用中,还需要给每个文档添加一个权值,用来指出每个文档与搜索内容的相关度,如下图所示:

c12ca6b1d94c

image.png

最常用的是使用词频作为权重,即记录单词在文档中出现的次数。以英文为例,如下图所示,索引文件中的“ MapReduce”一行表示:“ MapReduce”这个单词在文本 T0 中 出现过 1 次,T1 中出现过 1 次,T2 中出现过 2 次。

c12ca6b1d94c

image.png

3. 设计思路

3.1 Map过程

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

c12ca6b1d94c

image.png

这里存在两个问题:第一, key/value对只能有两个值,需要根据情况将其中两个值合并成一个值,作为 key 或 value 值;

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

这里将单词和 URL 组成 key 值(如“ MapReduce: file1.txt”),将词频作为value,这样做的好处是可以利用 MapReduce 框架自带的Map 端排序,将同一文档的相同单词的词频组成列表,传递给 Combine 过程,实现类似于 WordCount 的功能。

3.2 Combine 过程

经过 map 方法处理后, Combine 过程将 key 值相同 value 值累加,得到一个单词在文档中的词频。 如果直接将图所示的输出作为 Reduce 过程的输入,在 Shuffle 过程时将面临一个问题:所有具有相同单词的记录(由单词、 URL 和词频组成)应该交由同一个Reducer 处理,但当前的 key 值无法保证这一点,所以必须修改 key 值和 value 值。这次将单词作为 key 值, URL 和词频组成 value 值(如“ file1.txt: 1”)。这样做的好处是可以利用 MapReduce 框架默认的 HashPartitioner 类完成 Shuffle 过程,将相同单词的所有记录发送给同一个 Reducer 进行处理。

c12ca6b1d94c

image.png

3.3 Reduce 过程

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

c12ca6b1d94c

image.png

3.4 程序代码

pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.itcast

invertedIndex

1.0-SNAPSHOT

jar

invertedIndex

http://maven.apache.org

UTF-8

org.apache.hadoop

hadoop-common

2.6.4

org.apache.hadoop

hadoop-hdfs

2.6.4

org.apache.hadoop

hadoop-client

2.6.4

org.apache.hadoop

hadoop-mapreduce-client-core

2.6.4

org.apache.maven.plugins

maven-jar-plugin

2.4

true

lib/

cn.itcast.hadoop.mrwc.WordCountDriver

Map程序

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.lib.input.FileSplit;

public class InvertedIndexMapper extends Mapper{

private static Text keyInfo = new Text();// 存储单词和 URL 组合

private static final Text valueInfo = new Text("1");// 存储词频,初始化为1

@Override

protected void map(LongWritable key, Text value, Context context)

throws IOException, InterruptedException {

String line = value.toString();

String[] fields = line.split(" ");// 得到字段数组

FileSplit fileSplit = (FileSplit) context.getInputSplit();// 得到这行数据所在的文件切片

String fileName = fileSplit.getPath().getName();// 根据文件切片得到文件名

for (String field : fields) {

// key值由单词和URL组成,如“MapReduce:file1”

keyInfo.set(field + ":" + fileName);

context.write(keyInfo, valueInfo);

}

}

}

combine程序

import java.io.IOException;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexCombiner extends Reducer{

private static Text info = new Text();

// 输入:

// 输出:

@Override

protected void reduce(Text key, Iterable values, Context context)

throws IOException, InterruptedException {

int sum = 0;// 统计词频

for (Text value : values) {

sum += Integer.parseInt(value.toString());

}

int splitIndex = key.toString().indexOf(":");

// 重新设置 value 值由 URL 和词频组成

info.set(key.toString().substring(splitIndex + 1) + ":" + sum);

// 重新设置 key 值为单词

key.set(key.toString().substring(0, splitIndex));

context.write(key, info);

}

}

reduce程序

import java.io.IOException;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexReducer extends Reducer{

private static Text result = new Text();

// 输入:

// 输出:

@Override

protected void reduce(Text key, Iterable 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 org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class InvertedIndexRunner {

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

Configuration conf = new Configuration();

Job job = Job.getInstance(conf);

job.setJarByClass(InvertedIndexRunner.class);

job.setMapperClass(InvertedIndexMapper.class);

job.setCombinerClass(InvertedIndexCombiner.class);

job.setReducerClass(InvertedIndexReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

FileInputFormat.setInputPaths(job, new Path("D:\\ziliao\\data\\InvertedIndex\\input"));

// 指定处理完成之后的结果所保存的位置

FileOutputFormat.setOutputPath(job, new Path("D:\\ziliao\\data\\InvertedIndex\\output"));

// 向 yarn 集群提交这个 job

boolean res = job.waitForCompletion(true);

System.exit(res ? 0 : 1);

}

}

按权重排序

/**

* Created by Administrator on 2018/8/15.

*/

public class FileCount implements Comparable {

private String filename;

private long count;

//按照总流量倒序排

public int compareTo(FileCount bean) {

return bean.count>this.count?1:-1;

}

public FileCount(String filename, long count) {

this.filename = filename;

this.count = count;

}

@Override

public String toString() {

return filename + ":" + count;

}

}

新reduce程序

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexReducer extends Reducer{

private static Text result = new Text();

// 输入:

// 输出:

@Override

protected void reduce(Text key, Iterable values, Context context)

throws IOException, InterruptedException {

// 生成文档列表

String fileList = new String();

List FileCountList = new ArrayList();

for (Text value : values) {

String[] arr = value.toString().split(":");

FileCount FileCount = new FileCount(arr[0],Long.parseLong(arr[1]));

FileCountList.add(FileCount);

}

Collections.sort(FileCountList);

for(FileCount FileCount : FileCountList)

{

fileList += FileCount.toString() + ";";

}

result.set(fileList);

context.write(key, result);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值