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 ...
随机推荐
- 关于C#基础
前几天帮人做个社交网站,还是用的控件方式,不过学习了ajax和一般处理程序ashx后,也用在了里面一些,今天回来继续写博客.继续上次总结下基础知识,学的内容多,总结的可能比较杂乱,分条总结为平时能自己 ...
- SignalR入门之小试身手
建立好持久性连接类TestConnection之后,现在为我们的SignalR程序配置持久性连接类以及访问路径. 进入刚刚建立的Startup类,进入Configuration这个方法里来配置: us ...
- 用python生成一个导出数据库的bat脚本文件
# 环境: python3.x def getExportDbSql(db, index): # 获取导出一个数据库实例的sql语句 sql = 'mysqldump -u%s -p%s -h%s - ...
- 15天玩转redis —— 第四篇 哈希对象类型
redis中的hash也是我们使用中的高频数据结构,它的构造基本上和编程语言中的HashTable,Dictionary大同小异,如果大家往后有什么逻辑需要用 Dictionary存放的话,可以根据场 ...
- iOS 阶段学习第七天笔记(函数、递归)
iOS学习(C语言)知识点整理笔记 一.函数 1)概念:具有特定功能的代码块的封装 2)函数的定义: 函数类型+函数名(形参列表) 函数类型 函数名(形参类型1 形参名1,形参类型2 形参名2 ...
- 暴风冯鑫:去美国香港的99%都亏,互联网公司打死都要回A股
“上市之后,我回答得最多的两句话:一句是运气好:另一句是有好运气要好好地使用它.” 5月18日,暴风科技上市55天后,首享科技大厦办公室里,暴风科技CEO冯鑫这样对我说. 在经历了36个涨停之 ...
- EL表达式之sessionScope
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以 $ ...
- JMS中的消息通信模型
1. MQ简介: 消息队列(Message Queue,简称MQ),是应用程序与应用程序之间的一种通信方法.应用程序通过发送和检索出入列队的针对应用程序的数据 - 消息来通信,而无需专用连接来链接它们 ...
- 第 24 章 CSS3 变形效果[下]
学习要点: 1.3D 变形简介 2.transform-style 3.perspective 4.3D 变形属性 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS3 的变形效果,主要接着上节课 ...
- 双重OAuth 2.0架构
OAuth 2.0支持几种grant type,由于安全性不同,所以适用范围也不同.背景知识:<理解OAuth 2.0> grant type 是否需要secret 是否出现授权界面 授权 ...