hadoop之mapper类妙用
1. Mapper类
首先 Mapper类有四个方法:
(1) protected void setup(Context context)
(2) Protected void map(KEYIN key,VALUEIN value,Context context)
(3) protected void cleanup(Context context)
(4) public void run(Context context)
setup()方法一般用来加载一些初始化的工作,像全局文件\建立数据库的链接等等;cleanup()方法是收尾工作,如关闭文件或者执行map()后的键值分发等;map()函数就不多说了.
默认的Mapper的run()方法的核心代码如下:
public void run(Context context) throws IOException,InterruptedException
{
setup(context);
while(context.nextKeyValue())
map(context.getCurrentKey(),context,context.getCurrentValue(),context);
cleanup(context);
}
从代码中也可以看出先执行setup函数,然后是map处理代码,最后是cleanup的收尾工作.值得注意的是,setup函数和cleanup函数由系统作为回调函数只做一次,并不像map函数那样执行多次.
2.setup函数应用
经典的wordcount在setup函数中加入黑名单就可以实现对黑名单中单词的过滤,详细代码如下:
public class WordCount {
static private String blacklistFileName= "blacklist.dat"; public static class WordCountMap extends
Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable one = new IntWritable(1);
private Text word = new Text();
private Set<String> blacklist; protected void setup(Context context) throws IOException,InterruptedException {
blacklist=new TreeSet<String>(); try{
FileReader fileReader=new FileReader(blacklistFileName);
BufferedReader bufferedReader=bew BufferedReader(fileReader);
String str;
while((str=bufferedReader.readLine())!=null){
blacklist.add(str);
}
} catch(IOException e){
e.printStackTrace();
}
} public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer token = new StringTokenizer(line);
while (token.hasMoreTokens()) {
word.set(token.nextToken());
if(blacklist.contains(word.toString())){
continue;
}
context.write(word, one);
}
}
} public static class WordCountReduce extends
Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setJobName("wordcount"); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordCountMap.class);
job.setCombinerClass(WordCountReduce.class);
job.setReducerClass(WordCountReduce.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
3.cleanup应用
求最值最简单的办法就是对该文件进行一次遍历得出最值,但是现实中数据比量比较大,这种方法不能实现。在传统的MapReduce思想中,将文件的数据经 过map迭代出来送到reduce中,在Reduce中求出最大值。但这个方法显然不够优化,我们可采用“分而治之”的思想,不需要map的所有数据全部 送到reduce中,我们可以在map中先求出最大值,将该map任务的最大值送reduce中,这样就减少了数据的传输量。那么什么时候该把这个数据写 出去呢?我们知道,每一个键值对都会调用一次map(),由于数据量大调用map()的次数也就多了,显然在map()函数中将该数据写出去是不明智的, 所以最好的办法该Mapper任务结束后将该数据写出去。我们又知道,当Mapper/Reducer任务结束后会调用cleanup函数,所以我们可以 在该函数中将该数据写出去。了解了这些我们可以看一下程序的代码:
public class TopKApp {
static final String INPUT_PATH = "hdfs://hadoop:9000/input2";
static final String OUT_PATH = "hdfs://hadoop:9000/out2"; public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
final Path outPath = new Path(OUT_PATH);
if(fileSystem.exists(outPath)){
fileSystem.delete(outPath, true);
} final Job job = new Job(conf , WordCountApp.class.getSimpleName());
FileInputFormat.setInputPaths(job, INPUT_PATH);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(NullWritable.class);
FileOutputFormat.setOutputPath(job, outPath);
job.waitForCompletion(true);
}
static class MyMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable>{
long max = Long.MIN_VALUE;
protected void map(LongWritable k1, Text v1, Context context) throws java.io.IOException ,InterruptedException {
final long temp = Long.parseLong(v1.toString());
if(temp>max){
max = temp;
}
} protected void cleanup(org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,LongWritable, NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
context.write(new LongWritable(max), NullWritable.get());
}
} static class MyReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable>{
long max = Long.MIN_VALUE;
protected void reduce(LongWritable k2, java.lang.Iterable<NullWritable> arg1, org.apache.hadoop.mapreduce.Reducer<LongWritable,NullWritable,LongWritable,NullWritable>.Context arg2)
throws java.io.IOException ,InterruptedException {
final long temp = k2.get();
if(temp>max){
max = temp;
}
} protected void cleanup(org.apache.hadoop.mapreduce.Reducer<LongWritable,NullWritable,LongWritable,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
context.write(new LongWritable(max), NullWritable.get());
}
}
}
hadoop之mapper类妙用的更多相关文章
- [Hadoop源码解读](二)MapReduce篇之Mapper类
前面在讲InputFormat的时候,讲到了Mapper类是如何利用RecordReader来读取InputSplit中的K-V对的. 这一篇里,开始对Mapper.class的子类进行解读. 先回忆 ...
- MapReduce之Mapper类,Reducer类中的函数(转载)
Mapper类4个函数的解析 Mapper有setup(),map(),cleanup()和run()四个方法.其中setup()一般是用来进行一些map()前的准备工作,map()则一般承担主要的处 ...
- Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍
在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...
- Hadoop 2:Mapper和Reduce
Hadoop 2:Mapper和Reduce Understanding and Practicing Hadoop Mapper and Reduce 1 Mapper过程 Hadoop将输入数据划 ...
- Job流程:Mapper类分析
此文紧接Job流程:决定map个数的因素,Map任务被提交到Yarn后,被ApplicationMaster启动,任务的形式是YarnChild进程,在其中会执行MapTask的run()方法.无论是 ...
- 【mybatis】idea中 mybatis的mapper类去找对应的mapper.xml中的方法,使用插件mybatis-plugin
idea中 mybatis的mapper类去找对应的mapper.xml中的方法,使用插件mybatis-plugin,名字可能叫Free mybatis-plugin 安装上之后,可能需要重启ide ...
- 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别
启动报错: 2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: '127.0.0.1:50 ...
- Hadoop之TaskInputOutputContext类
在MapReduce过程中,每一个Job都会被分成若干个task,然后再进行处理.那么Hadoop是怎么将Job分成若干个task,并对其进行跟踪处理的呢?今天我们来看一个*Context类——Tas ...
- Hadoop_MapReduce中Mapper类和Reduce类
在权威指南中,有个关于处理温度的MapReduce类,具体如下: 第一部分:Map public class MaxTemperatureMapper extends MapReduceBase im ...
随机推荐
- C# 4.0 不要跨程序集用dynamic指向匿名类型 (转载)
今天写代码时偷懒用了dynamic,结果遇到问题,运行时始终无法获取dynamic对象的属性.原问题简化后如下: 程序集A包含SampleClass类,有一个静态方法,接收一个dynamic类型参数并 ...
- (转)Linux内核基数树应用分析
Linux内核基数树应用分析 ——lvyilong316 基数树(Radix tree)可看做是以二进制位串为关键字的trie树,是一种多叉树结构,同时又类似多层索引表,每个中间节点包含指向多个节点的 ...
- windows 建立任务执行计划 自动执行脚本
对于windows服务器网站如果要定时执行脚本,则需要在windows控制面板里找到 管理工具,点击任务计划程序,创建任务填写任务名称 触发器里新建触发条件,设置间隔时间 在操作项,新建触发时需要做的 ...
- Android 把url生成二维码并贴到给定的底图上
主要是用到了com.google.zxing jar包生成二维码的功能,这个jar包需要自己接下载 直接上代码 public static Bitmap CreateBinaryCodeImageBy ...
- SQL中的CONVERT();cast()函数
SQLSERVER中CONVERT函数格式: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参 ...
- Ubuntu如何挂载U盘
1. 以root用户登陆 2. 查看当前挂载 fdisk -l 一般情况未挂载的硬盘都在最后,这里是/dev/sdb1 3.新建一个目录来挂载硬盘 挂载到MNT/usb root@h-Default- ...
- Innodb和Mysiam引擎的区别
一:区别 Mysiam: 1.是非事务安全型. 2.是表级锁. 3.如果执行大量的select,Mysiam是更好的选择. 4.select count(*)from table.Mysiam只简单的 ...
- eclipse 配置
访问地址 https://www.eclipse.org/ . . . 配置工作目录:存放1.项目代码 2.IDE相关配置信息 //修改编码时的字体 //修改编码格式 没有配置to ...
- vue 创建项目的命令
1 cmd 创建项目 找到指定目录 vue create test 或 vue ui (可视化创建)推荐 ---------------------------------------- ...
- ionic2添加支付宝插件出现问题
安装本地路径插件正常 编译正常 在打开支付页面时候 就报这个错 在手机app点击无效 错误信息: ERROR Error: Uncaught (in promise): Error: No pro ...