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 ...
随机推荐
- javascript小数点后4舍5进
toFixed();获得小数点后面多少为数字.四舍五入的方法. 例如: var floatNum = 5.234 var temp = floatNum.toFixed(1); -- temp = 5 ...
- C# 汉字转拼音(支持GB2312字符集中所有汉字)
GB2312标准共收录6763个汉字,其中一级汉字3755个,二级汉字3008个. 分区表示 GB 2312中对所收汉字进行了“分区”处理,每区含有94个汉字/符号.这种表示方式也称为区位码. )- ...
- sql 删除重复行
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...
- DSOFramerControl简单使用
以前大学做项目(Web的毕业论系统)就看见过在网页中嵌入Office的word,那时候用了哪个控件当时没记下来,我倒是忘了,最近发现在WinForm要嵌入Office的话,可以使用DSOFramerC ...
- 用js解析经json序列化后的C#的DateTime类型数据
格式化日期(网上到处是),把下面的代码添加到jQuery.js文件中 //格式化日期 Date.prototype.format = function(format) { /* * eg:format ...
- C# 文件 文件夹
//判断文件夹(路径)是否存在 if (Directory.Exists(Path)) { } //获取文件大小 FileInfo file = new FileInfo(labOfPath); si ...
- iOS阶段学习第15天笔记(NSArray与NSMutableArray 数组)
iOS学习(OC语言)知识点整理 一.OC中的数组 1)数组:也是一个对象,数组中存放的是对象的地址,可以存放任意类型对象的地址,只能是对象不能是具体的数值,数组是有序的, 可以存放重复的元 ...
- ajax跳转传参
- 疯狂Android讲义 - 学习笔记(一)
常用开发工具的用法 android : 启动Android SDK管理器 android --help 查看具体用法 android create avd -n t10 -t 10 -b armea ...
- 总结一下工作中用到的Mybatis业务逻辑
1.简单说明. MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO( Plain Old Java Objects,普通的Java 对象)映射成数据库中的 ...