MapReduce实例-倒排索引
环境:
Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境
数据:任意数量、格式的文本文件(我用的四个.java代码文件)
方案目标:
根据提供的文本文件,提取出每个单词在哪个文件中出现了几次,组成倒排索引,格式如下
Ant FaultyWordCount.java : 1 , WordCount.java : 1
思路:
因为这个程序需要用到三个变量:单词、文件名、出现的频率,因此需要自定义Writable类,以单词为key,将文件名和出现的频率打包。
1.先将每行文本的单词进行分割,以K/V=Word/Filename:1的格式分割。
2.利用Combiner类,将本Map一个文件的先进行一次计数,减少传输量
3.在Reduce中对Combiner中传输过来的同一个单词的在不同文件出现的频率数据进行组合。
难点:这个程序主要是用到了一个Combiner和自定义了Writable类。在实现的时候,需要注意的是Writable必须默认无参构造函数。
主调用Main类:
package ren.snail; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class Main extends Configured implements Tool { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
int result = ToolRunner.run(new Configuration(), new Main(), args);
System.exit(result);
} @Override
public int run(String[] arg0) throws Exception {
// TODO Auto-generated method stub
Configuration configuration = getConf();
Job job = new Job(configuration, "InvertIndex");
job.setJarByClass(Main.class);
FileInputFormat.addInputPath(job, new Path(arg0[0]));
FileOutputFormat.setOutputPath(job, new Path(arg0[1])); job.setMapperClass(InvertMapper.class);
job.setCombinerClass(Combinner.class); //设置Combiner类
job.setReducerClass(InvertReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FileFreqWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
return 0;
} }
自定义Writbale类
package ren.snail; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable; public class FileFreqWritable implements Writable {
Text documentID;
IntWritable fequence; public FileFreqWritable() //必须提供无参构造函数
{
this.documentID = new Text();
this.fequence = new IntWritable();
}
public FileFreqWritable(Text id,IntWritable feq) {
// TODO Auto-generated constructor stub
this.documentID=id;
this.fequence =feq;
} public void set(String id,int feq)
{
this.documentID.set(id);
this.fequence.set(feq);
} @Override
public void readFields(DataInput in) throws IOException {
// TODO Auto-generated method stub
documentID.readFields(in);
fequence.readFields(in); } @Override
public void write(DataOutput out) throws IOException {
// TODO Auto-generated method stub
documentID.write(out);
fequence.write(out);
} public Text getDocumentID() {
return documentID;
} public String toString()
{
return documentID.toString()+" : "+fequence.get();
}
public IntWritable getFequence() {
return fequence;
} }
Map
package ren.snail; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
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 InvertMapper extends Mapper<LongWritable, Text, Text, FileFreqWritable>{
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
{
String data = value.toString().replaceAll("[^a-zA-Z0-9]+", " "); //将不需要的其他字符都设为空
String[] values = data.split(" ");
FileSplit fileSplit = (FileSplit)context.getInputSplit();
String filename = fileSplit.getPath().getName();
for (String temp : values) {
FileFreqWritable obj = new FileFreqWritable(new Text(filename),new IntWritable(1));
context.write(new Text(temp), obj);
} }
}
Combiner
package ren.snail; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class Combinner extends Reducer<Text, FileFreqWritable, Text, FileFreqWritable>{
public void reduce(Text key,Iterable<FileFreqWritable> values,Context context) throws IOException,InterruptedException
{
int count = 0 ;
String id = "";
for (FileFreqWritable temp : values) {
count++;
if(count == 1)
{
id=temp.getDocumentID().toString();
}
}
context.write(key,new FileFreqWritable(new Text(id), new IntWritable(count)));
}
}
Reduce
package ren.snail; import java.io.IOException; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class InvertReducer extends Reducer<Text, FileFreqWritable, Text, Text> { public void reduce(Text key,Iterable<FileFreqWritable> values,Context context) throws IOException,InterruptedException {
StringBuilder value = new StringBuilder();
for (FileFreqWritable fileFreqWritable : values) {
String temp = fileFreqWritable.toString();
value.append(temp+" , ");
}
context.write(key,new Text(value.toString()));
}
}
其实我的Reduce实现思路可能有点问题,不过大致是这样
MapReduce实例-倒排索引的更多相关文章
- MapReduce的倒排索引
MapReduce的倒排索引 索引: 什么是索引:索引(Index)是帮助数据库高效获取数据的数据结构.索引是在基于数据库表创建的,它包含一个表中某些列的值以及记录对应的地址,并且把这些值存储在一个数 ...
- MapReduce实例2(自定义compare、partition)& shuffle机制
MapReduce实例2(自定义compare.partition)& shuffle机制 实例:统计流量 有一份流量数据,结构是:时间戳.手机号.....上行流量.下行流量,需求是统计每个用 ...
- MapReduce实例&YARN框架
MapReduce实例&YARN框架 一个wordcount程序 统计一个相当大的数据文件中,每个单词出现的个数. 一.分析map和reduce的工作 map: 切分单词 遍历单词数据输出 r ...
- 利用MapReduce实现倒排索引
这里来学习的是利用MapReduce的分布式编程模型来实现简单的倒排索引. 首先什么是倒排索引? 倒排索引是文档检索中最常用的数据结构,被广泛地应用于全文搜索引擎. 它主要是用来存储某个单词(或词组) ...
- MapReduce实例浅析
在文章<MapReduce原理与设计思想>中,详细剖析了MapReduce的原理,这篇文章则通过实例重点剖析MapReduce 本文地址:http://www.cnblogs.com/ar ...
- MapReduce实例
1.WordCount(统计单词) 经典的运用MapReuce编程模型的实例 1.1 Description 给定一系列的单词/数据,输出每个单词/数据的数量 1.2 Sample a is b is ...
- mapreduce (三) MapReduce实现倒排索引(二)
hadoop api http://hadoop.apache.org/docs/r1.0.4/api/org/apache/hadoop/mapreduce/Reducer.html 改变一下需求: ...
- MapReduce实战--倒排索引
本文地址:http://www.cnblogs.com/archimedes/p/mapreduce-inverted-index.html,转载请注明源地址. 1.倒排索引简介 倒排索引(Inver ...
- Hadoop实战-MapReduce之倒排索引(八)
倒排索引 (就是key和Value对调的显示结果) 一.需求:下面是用户播放音乐记录,统计歌曲被哪些用户播放过 tom LittleApple jack YesterdayO ...
随机推荐
- 分享15个HTML5工具
HTML5 Working Draft Specification HTML5 Working Draft Specification译为HTML 5工作草案标准,它是 HTML5 的最新草案,由 W ...
- 【C#】递归搜索指定目录下的指定项目(文件或目录)
---------------更新:201411201121--------------- 主要更新说明:将原bool recurse参数改为int depth,这样可以指定递归深度,而不是笼统的是否 ...
- 【SQL】统计所有表的行数
环境:mssql ent 2k8 r2 原理:遍历所有用户表,用sp_spaceused过程分别获取每张表的行数并写入临时表,最后返回临时表 IF OBJECT_ID('tempdb..#TableR ...
- Using Recursive Common table expressions to represent Tree structures
http://www.postgresonline.com/journal/archives/131-Using-Recursive-Common-table-expressions-to-repre ...
- VS使用WinRAR软件以命令行方式打包软件至一个exe
由于项目需要,需要将一个绿色版软件(即无需在C盘写入文件)发给客户使用,要求是只有一个exe文件,双击即可执行. 网上说WinRAR软件创建自解压文件可以实现,链接http://blog.csdn. ...
- 事件委托 documentFragment
如果有一个列表页,页面结构是外面一个BODY, 里面有一万个子DIV,每个DIV里面有一些文字,这些文字都不相同.需求是,点击一个DIV,要求弹个alert框,将DIV里的文字显示出来.你会怎么做. ...
- NProgress.js template
NProgress.js:加载进度条:http://ricostacruz.com/nprogress/ 基础的这几个方法 这个网站上都有 我在一个地方看到这个代码 NProgress.configu ...
- jQuery cbpContentSlider 滑动切换
cbpContentSlider是一款选项卡插件,只要按照以下html结构就可以自动生成菜单切换内容特效. 在线实例 实例演示 使用方法 <div id="cbp-contentsli ...
- 根据网址把图片下载到服务器C#代码
根据网址把图片下载到服务器C#代码 ASPX页面代码: <%@ Page Language="C#" AutoEventWireup="true" Cod ...
- 【初探HTML本相】道之真谛不过自然,html标签脱俗还真
前言 须弥般若有无空,阴阳道化真虚同:洗尽前恩本非相,还吾面目下九重. 咳咳,其实老夫对佛教文化有点点研究啦,说以我们这里来了一点很有哲理的东西,因为我这里准备干一件很戳的事情,我准备来看看我们的ht ...