c语言sscanf函数的用法是什么
394
2022-11-20
Hadoop之——基于MR程序实现倒排索引
相信接触过搜索引擎开发的同学对倒排索引并不陌生,谷歌、百度等搜索引擎都是用的倒排索引,关于倒排索引的有关知识,这里就不再深入讲解,有兴趣的同学到网上了解一下。这篇博文就带着大家一起学习下如何利用Hadoop的MR程序来实现倒排索引的功能。
一、数据准备
1、输入文件数据
这里我们准备三个输入文件,分别如下所示
a.txt
hello tomhello jerryhello tom
b.txt
hello jerryhello jerrytom jerry
c.txt
hello jerryhello tom
2、最终输出文件数据
最终输出文件的结果为:
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
二、倒排索引过程分析
根据输入文件数据和最终的输出文件结果可知,此程序需要利用两个MR实现,具体流程可总结归纳如下:
-------------第一步Mapper的输出结果格式如下:--------------------context.wirte("hello->a.txt", "1")context.wirte("hello->a.txt", "1")context.wirte("hello->a.txt", "1")context.wirte("hello->b.txt", "1")context.wirte("hello->b.txt", "1")context.wirte("hello->c.txt", "1")context.wirte("hello->c.txt", "1")-------------第一步Reducer的得到的输入数据格式如下:-------------<"hello->a.txt", {1,1,1}><"hello->b.txt", {1,1}><"hello->c.txt", {1,1}>-------------第一步Reducer的输出数据格式如下---------------------context.write("hello->a.txt", "3")context.write("hello->b.txt", "2")context.write("hello->c.txt", "2")-------------第二步Mapper得到的输入数据格式如下:-----------------context.write("hello->a.txt", "3")context.write("hello->b.txt", "2")context.write("hello->c.txt", "2")-------------第二步Mapper输出的数据格式如下:--------------------context.write("hello", "a.txt->3")context.write("hello", "b.txt->2")context.write("hello", "c.txt->2")-------------第二步Reducer得到的输入数据格式如下:-----------------<"hello", {"a.txt->3", "b.txt->2", "c.txt->2"}>-------------第二步Reducer输出的数据格式如下:-----------------context.write("hello", "a.txt->3 b.txt->2 c.txt->2")最终结果为:hello a.txt->3 b.txt->2 c.txt->2
三、程序开发
3.1、第一步MR程序与输入输出
package com.lyz.hdfs.mr.ii;import java.io.IOException;import org.apache.commons.lang.StringUtils;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.input.FileSplit;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;/** * 倒排索引第一步Map Reduce程序,此处程序将所有的Map/Reduce/Runner程序放在一个类中 * @author liuyazhuang * */public class InverseIndexStepOne { /** * 完成倒排索引第一步的mapper程序 * @author liuyazhuang * */ public static class StepOneMapper extends Mapper
3.1.1 输入数据
a.txt
hello tomhello jerryhello tom
b.txt
hello jerryhello jerrytom jerry
c.txt
hello jerryhello tom
3.1.2
输出结果:
hello-->a.txt 3hello-->b.txt 2hello-->c.txt 2jerry-->a.txt 1jerry-->b.txt 3jerry-->c.txt 1tom-->a.txt 2tom-->b.txt 1tom-->c.txt 1
3.2 第二步MR程序与输入输出
package com.lyz.hdfs.mr.ii;import java.io.IOException;import org.apache.commons.lang.StringUtils;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;/** * 倒排索引第二步Map Reduce程序,此处程序将所有的Map/Reduce/Runner程序放在一个类中 * @author liuyazhuang * */public class InverseIndexStepTwo { /** * 完成倒排索引第二步的mapper程序 * * 从第一步MR程序中得到的输入信息为: * 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 * @author liuyazhuang * */ public static class StepTwoMapper extends Mapper
3.2.1 输入数据
hello-->a.txt 3hello-->b.txt 2hello-->c.txt 2jerry-->a.txt 1jerry-->b.txt 3jerry-->c.txt 1tom-->a.txt 2tom-->b.txt 1tom-->c.txt 1
3.2.2 输出结果
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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~