mapreduce实现搜索引擎简单的倒排索引

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1010885678/article/details/44458825 使用hadoop版本为2.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1010885678/article/details/44458825

使用hadoop版本为2.2.0

倒排索引简单的可以理解为全文检索某个词

例如:在a.txt 和b.txt两篇文章分别中查找统计hello这个单词出现的次数,出现次数越多,和关键词的吻合度就越高

现有a.txt内容如下:

hello tom

hello jerry

hello kitty

hello world

hello tom

b.txt内容如下:

hello jerry

hello tom

hello world

在hadoop平台上编写mr代码分析统计各个单词在两个文本中出现的次数

其实也只是WordCount程序的改版而已~


将两个文本上传到hdfs根目录的ii文件夹下(mr直接读取ii文件夹,会读取所有没有以_(下划线)开头的文件)

编写mr代码

首先分析,map输入的格式为


该行偏移量 该行文本


如:

0 hello


我们知道,map的输出之后会根据相同的key来进行合并

而每个单词都不是唯一的,它可能在两个文本中都出现,使用单词作为key的话无法分辨出该单词属于哪个文本

而使用文本名字作为key的话,那么将达到我们原来的目的,因为map的输出就会变成a.txt->单词..单词..单词

这显然不是我们想要的结果


所以map输出的格式应该为


单个单词->所在文本 1


如:

hello->a.txt 1


这里用->作为单词和所在文本的分隔

这样就可以在根据key进行合并的时候不会影响到我们的结果


map代码如下:

public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {

		private Text k = new Text();
		private Text v = new Text();

		protected void map(
				LongWritable key,
				Text value,
				org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, Text>.Context context)
				throws java.io.IOException, InterruptedException {
			String[] data = value.toString().split(" ");
			//FileSplit类从context上下文中得到,可以获得当前读取的文件的路径
			FileSplit fileSplit = (FileSplit) context.getInputSplit();
			//文件路径为hdfs://hadoop:9000/ii/a.txt
			//根据/分割取最后一块即可得到当前的文件名
			String[] fileNames = fileSplit.getPath().toString().split("/");
			String fileName = fileNames[fileNames.length - 1];
			for (String d : data) {
				k.set(d + "->" + fileName);
				v.set("1");
				context.write(k, v);
			}
		};
	}

在map执行完毕之后

我们需要一个combiner来帮助完成一些工作

注意,combiner的输入格式和输出格式是一致的,也就是map的输出格式,否则会出错

再次分析,根据key合并value之后的键值对是这个样子的:

(hello->a.txt,{1,1,1,1,1})

combiner要做的工作就是讲values统计累加

并将key的单词和文本分隔开,将文本名和统计之后的values组合在一起形成新的value

如:

(hello,a.txt->5)

为什么要这么做?

因为在combiner执行完毕之后

还会根据key进行一次value的合并,跟map之后的是一样的

将key相同的value组成一个values集合

如此一来,在经过combiner执行之后,到达reduce的输入就变成了

(hello,{a.txt->5,b.txt->3})

这样的格式,然后在reduce中循环将values输出不就是我们想要的结果了吗~

combiner代码如下:

public static class MyCombiner extends Reducer<Text, Text, Text, Text> {

		private Text k = new Text();
		private Text v = new Text();

		protected void reduce(
				Text key,
				java.lang.Iterable<Text> values,
				org.apache.hadoop.mapreduce.Reducer<Text, Text, Text, Text>.Context context)
				throws java.io.IOException, InterruptedException {
			//分割文件名和单词
			String[] wordAndPath = key.toString().split("->");
			//统计出现次数
			int counts = 0;
			for (Text t : values) {
				counts += Integer.parseInt(t.toString());
			}
			//组成新的key-value输出
			k.set(wordAndPath[0]);
			v.set(wordAndPath[1] + "->" + counts);
			context.write(k, v);
		};
	}

接下来reduce的工作就简单了

代码如下:

public static class MyReducer extends Reducer<Text, Text, Text, Text> {

		private Text v = new Text();

		protected void reduce(
				Text key,
				java.lang.Iterable<Text> values,
				org.apache.hadoop.mapreduce.Reducer<Text, Text, Text, Text>.Context context)
				throws java.io.IOException, InterruptedException {
			String res = "";
			for (Text text : values) {
				res += text.toString() + "\r";
			}
			v.set(res);
			context.write(key, v);
		};
	}

main方法代码:

public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path inPath = new Path("hdfs://hadoop:9000" + args[0]);
		Path outPath = new Path("hdfs://hadoop:9000" + args[1]);
		if (fs.exists(outPath)) {
			fs.delete(outPath, true);
		}
		Job job = Job.getInstance(conf);
		job.setJarByClass(InverseIndex.class);

		FileInputFormat.setInputPaths(job, inPath);
		job.setInputFormatClass(TextInputFormat.class);

		job.setMapperClass(MyMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		job.setCombinerClass(MyCombiner.class);

		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		FileOutputFormat.setOutputPath(job, outPath);
		job.setOutputFormatClass(TextOutputFormat.class);

		job.waitForCompletion(true);
	}


在hadoop上运行jar包执行结果如图:




初学hadoop,仅作笔记之用,其中如有错误望请告知^-^




相关文章
|
4月前
|
存储 分布式计算 搜索推荐
【大数据技术Hadoop+Spark】MapReduce之单词计数和倒排索引实战(附源码和数据集 超详细)
【大数据技术Hadoop+Spark】MapReduce之单词计数和倒排索引实战(附源码和数据集 超详细)
46 0
|
11月前
|
存储 分布式计算 搜索推荐
MapReduce 案例之倒排索引
MapReduce 案例之倒排索引
124 0
MapReduce 案例之倒排索引
|
分布式计算 Hadoop 索引
Hadoop学习:MapReduce实现倒排索引
Hadoop学习:MapReduce实现倒排索引
190 0
Hadoop学习:MapReduce实现倒排索引
|
分布式计算 索引 Java
|
分布式计算 自然语言处理 Apache
MapReduce编程(七) 倒排索引构建
一、倒排索引简介 倒排索引(英语:Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。
1206 0
|
分布式计算 搜索推荐 索引
MapReduce实现倒排索引(类似协同过滤)
一、问题背景   倒排索引其实就是出现次数越多,那么权重越大,不过我国有凤巢....zf为啥不管,总局回应推广是不是广告有争议...   eclipse里ctrl+t找接口或者抽象类的实现类,看看都有啥方法,有时候hadoop的抽象类返回的接口没有需要的方法,那么我们返回他的实现类。
1037 0
|
存储 分布式计算 索引
|
索引 分布式计算
MapReduce编程实例之倒排索引 1
任务描述: 有一批电话清单,记录了用户A拨打给用户B的记录 做一个倒排索引,记录拨打给用户B所有的用户A、example data: 13614004876 110 18940084808 10086 13342445911 10001 13614004876 120 1894...
943 0
|
存储 分布式计算 搜索推荐
搜索引擎:MapReduce实战----倒排索引
1.倒排索引简介 倒排索引(Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。
1042 0