Hadoop2.7.3 mapreduce(四)倒排索引的实现

一、倒排索引简介

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

二、Map过程

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

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

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

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

package com.yc.hadoop.mapreduce.demo06;

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<LongWritable, Text, Text, Text> {

	public static final Text ONE = new Text("1");

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {

		// 拆分出这一行的数据
		String[] words = value.toString().split("\\s+"); // 以空格拆分
		FileSplit fs = (FileSplit) context.getInputSplit();// 文件分片对象
		String temp = fs.getPath().getName();// 取得文件名称
		String fileName = temp.substring(0, temp.lastIndexOf(".")); // 去掉后缀名

		Text keyValue = new Text();

		for (String word : words) {
			keyValue.set(word + ":" + fileName); // 把单词与单词所在的文件作为key
			context.write(keyValue, ONE);
		}
	}

}

三、Combine过程

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

package com.yc.hadoop.mapreduce.demo06;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexCombiner extends Reducer<Text, Text, Text, Text> {

	@Override
	protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		String[] wf = key.toString().split(":");	// 将key以空格拆分
		int count = 0;
		for (Text v : values) {
			count += Integer.parseInt(v.toString());	// 将字符1 强转为数字1
		}
		context.write(new Text(wf[0]), new Text(wf[1] + ":" + count));
	}

}

四、Reduce过程

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

package com.yc.hadoop.mapreduce.demo06;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexReducer extends Reducer<Text, Text, Text, Text> {

	@Override
	protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		StringBuilder sb = new StringBuilder();
		for (Text v : values) {
			sb.append(v + ";");
		}
		Text value = new Text(sb.substring(0, sb.length() - 1));
		context.write(key, value);
	}

}

五、驱动实现

package com.yc.hadoop.mapreduce.demo06;

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;

/**
 * 倒排索引
 * 
 * @author S3
 *
 */
public class InvertedIndexDemo {

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "ReverseIndexDemo");
		job.setJarByClass(InvertedIndexDemo.class);

		// map
		job.setMapperClass(InvertedIndexMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		// combiner
		job.setCombinerClass(InvertedIndexCombiner.class); // 本机合并

		// reduce
		job.setReducerClass(InvertedIndexReducer.class); // 集群合并

		Path inPath = new Path("/hadoop_data/data/text*.txt");
		Path outPath = new Path("/hadoop_data/out");
		
		FileSystem fs = outPath.getFileSystem(conf);
		if (fs.exists(outPath)) {
			fs.delete(outPath, true);
		}
		FileInputFormat.setInputPaths(job, inPath);
		FileOutputFormat.setOutputPath(job, outPath);
		System.out.println(job.waitForCompletion(true) ? 0 : 1);
	}
}

六、运行结果

(1)map 结果:

key:Hello:text1, value:1
key:world:text1, value:1
key:Hello:text1, value:1
key:haddop:text1, value:1
key:Hello:text1, value:1
key:java:text1, value:1


key:Hello:text2, value:1
key:codeFarmer:text2, value:1
key:Hello:text2, value:1
key:yc:text2, value:1

(2)combine 结果:

key:Hello:text1, value:3
key:haddop:text1, value:1
key:java:text1, value:1
key:world:text1, value:1


key:Hello:text2, value:2
key:codeFarmer:text2, value:1
key:yc:text2, value:1

(3)reduce 结果:

key:Hello, value:text2:2;text1:3
key:codeFarmer, value:text2:1
key:haddop, value:text1:1
key:java, value:text1:1
key:world, value:text1:1
key:yc, value:text2:1

(4)输出文本:

Hello    text2:2;text1:3
codeFarmer    text2:1
haddop    text1:1
java    text1:1
world    text1:1
yc    text2:1



参考:【Hadoop基础教程】9、Hadoop之倒排索引


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值