相信接触过搜索引擎开发的同学对倒排索引并不陌生,谷歌、百度等搜索引擎都是用的倒排索引,关于倒排索引的有关知识,这里就不再深入讲解,有兴趣的同学到网上了解一下。这篇博文就带着大家一起学习下如何利用Hadoop的MR程序来实现倒排索引的功能。
一、数据准备
1、输入文件数据
这里我们准备三个输入文件,分别如下所示
a.txt
hello tom hello jerry hello tom
b.txt
hello jerry hello jerry tom jerry
c.txt
hello jerry hello tom
2、最终输出文件数据
最终输出文件的结果为:
[plain] view plain copy 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<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
//获取一行数据
String line = value.toString();
//切分出每个单词
String[] fields = StringUtils.split(line, " ");
//获取数据的切片信息
FileSplit fileSplit = (FileSplit) context.getInputSplit();
//根据切片信息获取文件名称
String fileName = fileSplit.getPath().getName();
for(String field : fields){
context.write(new Text(field + "-->" + fileName), new LongWritable(1));
}
}
}
/**
* 完成倒排索引第一步的Reducer程序
* 最终输出结果为:
* 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 StepOneReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text key, Iterable<LongWritable> values,
Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
long counter = 0;
for(LongWritable value : values){
counter += value.get();
}
context.write(key, new LongWritable(counter));
}
}
//运行第一步的MR程序
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(InverseIndexStepOne.class);
job.setMapperClass(StepOneMapper.class);
job.setReducerClass(StepOneReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path("D:/hadoop_data/ii"));
FileOutputFormat.setOutputPath(job, new Path("D:/hadoop_data/ii/result"));
job.waitForCompletion(true);
}
}
3.1.1 输入数据
a.txt
hello tom hello jerry hello tom
b.txt
hello jerry hello jerry tom jerry
c.txt
hello jerry hello tom
3.1.2
输出结果:
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
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<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] fields = StringUtils.split(line, "\t");
String[] wordAndFileName = StringUtils.split(fields[0], "-->");
String word = wordAndFileName[0];
String fileName = wordAndFileName[1];
long counter = Long.parseLong(fields[1]);
context.write(new Text(word), new Text(fileName + "-->" + counter));
}
}
/**
* 完成倒排索引第二步的Reducer程序
* 得到的输入信息格式为:
* <"hello", {"a.txt->3", "b.txt->2", "c.txt->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
* @author liuyazhuang
*
*/
public static class StepTwoReducer 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 result = "";
for(Text value : values){
result += value + " ";
}
context.write(key, new Text(result));
}
}
//运行第一步的MR程序
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(InverseIndexStepTwo.class);
job.setMapperClass(StepTwoMapper.class);
job.setReducerClass(StepTwoReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("D:/hadoop_data/ii/result/part-r-00000"));
FileOutputFormat.setOutputPath(job, new Path("D:/hadoop_data/ii/result/final"));
job.waitForCompletion(true);
}
}
3.2.1 输入数据
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
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
总结
以上就是本文关于Hadoop编程基于MR程序实现倒排索引示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Hadoop对文本文件的快速全局排序实现方法及分析、hadoop重新格式化HDFS步骤解析、浅谈七种常见的Hadoop和Spark项目案例等,有什么问题可以直接留言,小编会及时回复大家的。感谢朋友们对本站的支持!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]