MapReduce中的倒排索引
0.倒排索引资料:
http://blog.csdn.net/pzasdq/article/details/51442856
1.三个日志源文件:
a.txt
hello tom
hello jerry
hello tom
b.txt
hello jerry
hello jerry
tom jerry
c.txt
hello jerry
hello tom
希望统计出来的结果如下:
hello a.txt->3 b.txt->2 c.txt->2
jerry b.txt->3 a.txt->1 c.txt->1
tom a.txt->2 b.txt->1 c.txt->1
2.上代码:
import java.io.IOException; 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; public class InverseIndex { public static class IndexMapper extends Mapper<LongWritable, Text, Text, Text>{
private Text k = new Text();
private Text v = new Text();
@Override
protected void map(LongWritable key, Text value,Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String [] words = line.split(" ");
FileSplit inputSplit = (FileSplit)context.getInputSplit();//返回mapper读取的是哪个切片split
//path=hdfs://itcast:9000/ii/a.txt
//k2,v2 为 hello->a.txt {1,1,1}
String path = inputSplit.getPath().toString();
for (String word : words) {
k.set(word + "->" + path);
v.set("1");
context.write(k, v);
}
}
} public static class IndexCombiner extends Reducer<Text, Text, Text, Text>{
private Text k = new Text();
private Text v = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
//k2,v2 为hello->a.txt {1,1,1} -----> k3,v3为 hello,a.txt->3
int counter = 0;
for(Text text :values){
counter += Integer.parseInt(text.toString());
}
String[] wordAndPath = key.toString().split("->");
String word = wordAndPath[0];
String path = wordAndPath[1];
k.set(word);
v.set(path+"->"+counter);
context.write(k,v);
}
} public static class IndexReducer extends Reducer<Text, Text, Text, Text>{
private Text v = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
//Reducer这里 是把所有key相同的搞到一块了,这个地方对应的values为Iterable也证实这一点.
//不同的Map根据k2 到达Reducer 把k2相同的汇聚到一起...对应的k2对应的v2组成一个集合.
//从combiner过来的k和v为 hello,a.txt->3 经过reducer变成
String result = "";
for(Text t:values){
result += t.toString() + "\t";
}
v.set(result);
context.write(key,v);
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(InverseIndex.class); job.setMapperClass(IndexMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setCombinerClass(IndexCombiner.class);
FileInputFormat.setInputPaths(job, new Path(args[0])); job.setReducerClass(IndexReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);//0是正常推出以 1是异常退出.
}
}
3.打成jar包,通过命令执行
hadoop jar /root/itcastmr.jar itcastmr.inverseindex.InverseIndex /user/root/InverseIndex /InverseIndexResult
查看结果文件:
MapReduce中的倒排索引的更多相关文章
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- Hadoop学习笔记—12.MapReduce中的常见算法
一.MapReduce中有哪些常见算法 (1)经典之王:单词计数 这个是MapReduce的经典案例,经典的不能再经典了! (2)数据去重 "数据去重"主要是为了掌握和利用并行化思 ...
- MapReduce中作业调度机制
MapReduce中作业调度机制主要有3种: 1.先入先出FIFO Hadoop 中默认的调度器,它先按照作业的优先级高低,再按照到达时间的先后选择被执行的作业. 2.公平调度器(相当于时间 ...
- Mapreduce中的字符串编码
Mapreduce中的字符串编码 $$$ Shuffle的执行过程,需要经过多次比较排序.如果对每一个数据的比较都需要先反序列化,对性能影响极大. RawComparator的作用就不言而喻,能够直接 ...
- MapReduce中一次reduce方法的调用中key的值不断变化分析及源码解析
摘要:mapreduce中执行reduce(KEYIN key, Iterable<VALUEIN> values, Context context),调用一次reduce方法,迭代val ...
- Hadoop学习之路(二十三)MapReduce中的shuffle详解
概述 1.MapReduce 中,mapper 阶段处理的数据如何传递给 reducer 阶段,是 MapReduce 框架中 最关键的一个流程,这个流程就叫 Shuffle 2.Shuffle: 数 ...
- [MapReduce_5] MapReduce 中的 Combiner 组件应用
0. 说明 Combiner 介绍 && 在 MapReduce 中的应用 1. 介绍 Combiner: Map 端的 Reduce,有自己的使用场景 在相同 Key 过多的情况下 ...
- Hadoop案例(七)MapReduce中多表合并
MapReduce中多表合并案例 一.案例需求 订单数据表t_order: id pid amount 1001 01 1 1002 02 2 1003 03 3 订单数据order.txt 商品信息 ...
- MapReduce中的分布式缓存使用
MapReduce中的分布式缓存使用 @(Hadoop) 简介 DistributedCache是Hadoop为MapReduce框架提供的一种分布式缓存机制,它会将需要缓存的文件分发到各个执行任务的 ...
随机推荐
- rm与管道使用
一 问题初始:用通常意义的管道使用这样可以:(1)ls -l | sed -n '/~$/p' 我用显示出系统自己建立的备份文件这时,我想删除这些文件,我仍然使用了管道,并执行了以下命令(2)ls - ...
- vmware虚拟机centOs安装教程
1安装vmware 虚拟机软件 1.解压vmware安装 汉化vmware虚拟机 复制注册码,并填写进vmware 2安装linux(centos)虚拟机 1. 点击文件----->新建虚拟机 ...
- 安卓开机logo和开机动画的几种实现方法
安卓4.2可用方法2-4,第一种方法未验证. 从理论上来说,android 有4个开机启动画面. 第一个应该是U-BOOT的启动画面,有些设备为了满足按动电源即有显示,在UBOOT里加了开机画面,实现 ...
- estimator = KerasClassifier
如何在scikit-learn模型中使用Keras 通过用 KerasClassifier 或 KerasRegressor 类包装Keras模型,可将其用于scikit-learn. 要使用这些包装 ...
- referraluserid推广ID号跟踪JS处理A标签
网站推广ID号跟踪 xxx.html?referraluserid=123 referraluserid.js JS源文件 referraluserid的参数会自动绑定页面A标签 有时是Post 表单 ...
- js 循环向上滚动
aaaaaaaaaaaaaaaaa最开头 aaaaaaaaaaa 1 aaaaaaaaaaa 2 aaaaaaaaaaa 3 aaaaaaaaaaa 4 aaaaaaaaaaa 5 aaaaaaaaa ...
- 【JS库】URI.js
做前端的,应该有不少人都写过操作URL的代码,比如提取问号后面的参数.或者主机名什么的,比如这样: var url="http://jszai.com/foo?bar=baz", ...
- day24(JAVAWEB上传与下载)
javaWeb上传与下载 上传: 上传方式: jspSmartUpload :应用在jsp上的文件上传与下载组件. FileUpload :用用在jaava环境上的上传的功能 ...
- jdbc的配置(更新中)
MySQL的 JDBC URL 格式 for Connector/J 如下例: 格式如下: jdbc:mysql://[host][,failoverhost...][:port]/[databas ...
- SRM472
这次是rng_58出的题目,思维难度还是相当大的的..很值得一做. 250pt: 题意:盒子里有n 个 potatoes,甲乙两个人,每次只能拿4的幂次方数(1,4,16...),最后不能拿的输.求谁 ...