MR案例:倒排索引 && MultipleInputs
本案例采用 MultipleInputs类 实现多路径输入的倒排索引。解读:MR多路径输入
package test0820; import java.io.IOException;
import java.lang.reflect.Method; 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.InputSplit;
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.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WC0826 { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(WC0826.class); job.setMapperClass(IIMapper.class);
job.setCombinerClass(IICombiner.class);
job.setReducerClass(IIReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //使用MultipleInputs类指定多路径输入
MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class);
MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class);
FileOutputFormat.setOutputPath(job, new Path(args[2])); System.exit(job.waitForCompletion(true)? 0:1);
} //map
public static class IIMapper extends Mapper<LongWritable, Text, Text, Text>{ String fileName; /**
* 使用 MultipleInputs 获得 FileName 必须添加的类
*/
private Path getFilePath(Context context) throws IOException { InputSplit split = context.getInputSplit();
Class<? extends InputSplit> splitClass = split.getClass(); FileSplit fileSplit = null;
if (splitClass.equals(FileSplit.class)) {
fileSplit = (FileSplit) split;
} else if (splitClass.getName().
equals("org.apache.hadoop.mapreduce.lib.input.TaggedInputSplit")) { // begin reflection hackery...
try {
Method getInputSplitMethod = splitClass.getDeclaredMethod("getInputSplit");
getInputSplitMethod.setAccessible(true);
fileSplit = (FileSplit) getInputSplitMethod.invoke(split);
} catch (Exception e) {
// wrap and re-throw error
throw new IOException(e);
}
// end reflection hackery
}
return fileSplit.getPath();
} @Override
protected void setup(Context context)
throws IOException, InterruptedException { //get file name
fileName = getFilePath(context).getName();
} @Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException { String[] splited = value.toString().split("\t"); for(String word : splited){
Text word_fileName=new Text(word+"@"+fileName);
context.write(word_fileName,new Text("1"));
}
}
} //combiner
public static class IICombiner extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> v2s, Context context)
throws IOException, InterruptedException { Long sum = 0L;
String value=new String(); String[] splited = key.toString().split("@"); for(Text vl :v2s){
sum += Long.parseLong(vl.toString());
value = splited[1]+"@"+sum.toString();
}
context.write(new Text(splited[0]), new Text(value));
}
} //reduce
public static class IIReducer extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> v2s, Context context)
throws IOException, InterruptedException { String value=new String(); for(Text text : v2s){
value = text.toString()+":"+value;
}
//去掉最后的":"
context.write(key, new Text(value.substring(0, value.length()-1)));
}
}
}
出现问题01:使用MultipleInputs类指定输入路径,当setup()方法中调用getInputSplit()方法获取当前split对应的FileName时会报IO异常:
Error: java.lang.ClassCastException: org.apache.hadoop.mapreduce.lib.input.TaggedInputSplit cannot be cast to org.apache.hadoop.mapreduce.lib.input.FileSplit
问题原因01:filesplit实际上就是TaggedInputSplit中的成员变量inputSplit,而TaggedInputSplit类并不是public的(默认是default声明类型),所以不能直接获得对应的信息。
解决方案01:
- 第一种方法:在当前项目中新建对应的TaggedInputSplit类,并声明为public。即覆盖掉原有TaggedInputSplit类的声明类型。然后通过以下代码就可以正确调用:
(FileSplit)((TaggedInputSplit)reporter.getInputSplit()).getInputSplit();
- 第二种方法:通过反射机制。代码如下:
/**
* 反射机制
* 使用 MultipleInputs 获得 FileName 必须添加的类
*/
private Path getFilePath(Context context) throws IOException { InputSplit split = context.getInputSplit();
Class<? extends InputSplit> splitClass = split.getClass(); FileSplit fileSplit = null;
if (splitClass.equals(FileSplit.class)) {
fileSplit = (FileSplit) split;
} else if (splitClass.getName().
equals("org.apache.hadoop.mapreduce.lib.input.TaggedInputSplit")) { // begin reflection hackery...
try {
Method getInputSplitMethod = splitClass.getDeclaredMethod("getInputSplit");
getInputSplitMethod.setAccessible(true);
fileSplit = (FileSplit) getInputSplitMethod.invoke(split);
} catch (Exception e) {
// wrap and re-throw error
throw new IOException(e);
}
// end reflection hackery
}
return fileSplit.getPath();
}
出现问题02:
map<Object,Text,Text,IntWritble>
combiner<Text,IntWritble,Text,Text>
reduce<Text,Text,Text,Text>
这样设置,系统会异常。这是因为Combiner和Reducer其实是同一个函数,所以输入和输出类型必须保持一致。
Combiner实现对map端value的聚合,减少map 到 reudce 间数据传输,加快 shuffle 速度。牢记求平均值的MR不能使用Combiner。
MR案例:倒排索引 && MultipleInputs的更多相关文章
- MR案例:倒排索引
1.map阶段:将单词和URI组成Key值(如“MapReduce :1.txt”),将词频作为value. 利用MR框架自带的Map端排序,将同一文档的相同单词的词频组成列表,传递给Combine过 ...
- MR案例:Reduce-Join
问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...
- MR案例:小文件处理方案
HDFS被设计来存储大文件,而有时候会有大量的小文件生成,造成NameNode资源的浪费,同时也影响MapReduce的处理效率.有哪些方案可以合并这些小文件,或者提高处理小文件的效率呢? 1). 所 ...
- MR案例:CombineFileInputFormat
CombineFileInputFormat是一个抽象类.Hadoop提供了两个实现类CombineTextInputFormat和CombineSequenceFileInputFormat. 此案 ...
- MR案例:输出/输入SequenceFile
SequenceFile文件是Hadoop用来存储二进制形式的key-value对而设计的一种平面文件(Flat File).在SequenceFile文件中,每一个key-value对被看做是一条记 ...
- MR案例:分区和排序
现有一学生成绩数据,格式如下:<学号,姓名,学院,成绩> //<id, name, institute, grade>. 需求描述:查询成绩大于等于60分的学生数据,按学院分 ...
- MR案例:链式ChainMapper
类似于Linux管道重定向机制,前一个Map的输出直接作为下一个Map的输入,形成一个流水线.设想这样一个场景:在Map阶段,数据经过mapper01和mapper02处理:在Reduce阶段,数据经 ...
- MR案例:定制InputFormat
数据输入格式 InputFormat类用于描述MR作业的输入规范,主要功能:输入规范检查(比如输入文件目录的检查).对数据文件进行输入切分和从输入分块中将数据记录逐一读取出来.并转化为Map的输入键值 ...
- MR案例:基站相关01
字段解释: product_no:用户手机号: lac_id:用户所在基站: start_time:用户在此基站的开始时间: staytime:用户在此基站的逗留时间. product_no lac_ ...
随机推荐
- DPK750针式打印机驱动,750u.dll下载
http://pan.baidu.com/s/1dD1SFvV DPK750针式打印机 下载
- Vue.js_构造器及其实例化概念
Vue构造器 1.Vue.js是一个构造函数,编程中称之为构造器 2.每一个new Vue() 都是一个Vue构造函数的实例,这个过程叫做实例化 3.构造函数需要将其实例化后才会启用 var vm = ...
- CH5101 LCIS【线性dp】
5101 LCIS 0x50「动态规划」例题 描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了 ...
- fiddler弱网测试
模拟低速网路环境…启用方法如下: Rules → Performances → Simulate Modem Speeds (如下图)· 勾选之后,你会发现你的网路瞬间慢超多… (想当年国中时我们的网 ...
- linux mint19.1解决网易云音乐安装后打不开的问题
安装网易云音乐: sudo dpkg -i 文件路径#文件路径可以直接把刚才下载的软件包拖进终端sudo apt install -f 修复依赖关系 安装后打不开的问题: 1.sudo gedit / ...
- FreeMarker 的使用方法
1.FreeMarker 概述 FreeMarker 是一个用Java语言编写的模板引擎,使用模板来生成文本输出;主要用于做静态页面或页面展示; 2.FreeMarker 使用 // 导入jar包: ...
- leadecode 2 Add Two Numbers
package leadcode; /** * 2. Add Two Numbers * Medium * 4067 * 953 * * * You are given two non-empty l ...
- 转!mysql 命令行下 通过DELIMITER临时改变语句分隔符 执行存储过程
mysql 在 Navicat 界面工具 执行存储过程ok,但是在命令行下执行失败. 原因在于,默认的MySQL语句分隔符为' ; ',在输入' ; '的时候,“以为”语句已经结束了,但实际上语句还没 ...
- #define宏定义
1 #define的概念 #define命令是C语言中的一个宏定义命令,它用来将一个标识符定义为一个字符串,该标识符被称为宏名,被定义的字符串称为替换文本. 该命令有两种格式:一种是简单的宏定义, ...
- 利用Octopress在Github上搭建博客及后续问题总汇
首先贴一下我的新博客地址: http://findingsea.github.io 用Octopress在GitHub上搭建博客已经不是什么新鲜事了,网上的教程也多了去了,大题的方法什么都差不多,这篇 ...