[hadoop](3) MapReduce:创建计数器、任务状态和写入日志
前言
本章主要讲述了如何在mapreduce任务中添加自定义的计数器,从所有任务中聚合信息,并且最终输出到mapreduce web ui中得到统计信息。
准备工作
数据集:ufo-60000条记录,这个数据集有一系列包含下列字段的UFO目击事件记录组成,每条记录的字段都是以tab键分割,请看http://www.cnblogs.com/cafebabe-yun/p/8679994.html
- sighting date:UFO目击事件发生时间
- Recorded date:报告目击事件的时间
- Location:目击事件发生的地点
- Shape:UFO形状
- Duration:目击事件持续时间
- Dexcription:目击事件的大致描述
例子:
19950915 19950915 Redmond, WA 6 min. Young man w/ 2 co-workers witness tiny, distinctly white round disc drifting slowly toward NE. Flew in dir. 90 deg. to winds.
需要共享的数据:州名缩写与全称的对应关系
数据:
AL Alabama
AK Alaska
AZ Arizona
AR Arkansas
CA California
自定义计数器的使用
- 将数据集 ufo.tsv 上传到hdfs上
hadoop dfs -put ufo.tsv ufo.tsv
- 将共享数据数据上传到hdfs上,命令同上
- 创建文件 UFOCountingRecordValidationMapper.java ,并且输入以下代码:
import java.io.IOException; import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.lib.*; public class UFOCountingRecordValidationMapper extends MapReduceBase implements Mapper<LongWritable, Text, LongWritable, Text> {
public enum LineCounters {
BAD_LINES,
TOO_MANY_TABS,
TOO_FEW_TABS
}; @Override
public void map(LongWritable key, Text value, OutputCollector<LongWritable, Text> output, Reporter reporter) throws IOException {
String line = value.toString();
if(validate(line, reporter)) {
output.collect(key, value);
}
} private boolean validate(String line, Reporter reporter) {
String[] words = line.split("\t");
if (words.length != 6) {
if (words.length < 6) {
reporter.incrCounter(LineCounters.TOO_MANY_TABS
, 1);
} else {
reporter.incrCounter(LineCounters.TOO_FEW_TABS, 1);
}
reporter.incrCounter(LineCounters.BAD_LINES, 1);
if ((reporter.getCounter(LineCounters.BAD_LINES).getCounter() % 10) == 0) {
reporter.setStatus("Got 10 bad lines.");
System.err.println("Read another 10 bad lines.");
}
return false;
}
return true;
}
}
- 创建文件 UFOLocation3.java ,并输入以下代码:
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.regex.*; import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.lib.*; public class UFOLocation3 {
public static class MapClass extends MapReduceBase implements Mapper<LongWritable, Text, Text, LongWritable> {
private final static LongWritable one = new LongWritable(1);
private static Pattern locationPattern = Pattern.compile("[a-zA-Z]{2}[^a-zA-Z]*$");
private Map<String, String> stateNames; @Override
public void configure(JobConf job) {
try {
Path[] cacheFiles = DistributedCache.getLocalCacheFiles(job);
setupStateMap(cacheFiles[0].toString());
} catch (IOException e) {
System.err.println("Error reading state file.");
System.exit(1);
}
} private void setupStateMap(String fileName) throws IOException {
Map<String, String> stateCache = new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while((line = reader.readLine()) != null) {
String[] splits = line.split("\t");
stateCache.put(splits[0], splits[1]);
}
stateNames = stateCache;
} @Override
public void map(LongWritable key, Text value, OutputCollector<Text, LongWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
String[] fields = line.split("\t");
String location = fields[2].trim();
if(location.length() >= 2) {
Matcher matcher = locationPattern.matcher(location);
if(matcher.find()) {
int start = matcher.start();
String state = location.substring(start, start + 2);
output.collect(new Text(lookupState(state.toUpperCase())), one);
}
}
} private String lookupState(String state) {
String fullName = stateNames.get(state);
if(fullName == null || "".equals(fullName)) {
fullName = state;
}
return fullName;
}
} public static void main(String...args) throws Exception {
Configuration config = new Configuration();
JobConf conf = new JobConf(config, UFOLocation3.class);
conf.setJobName("UFOLocation3");
DistributedCache.addCacheFile(new URI("/user/root/states.txt"), conf);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(LongWritable.class); JobConf mapconf1 = new JobConf(false);
ChainMapper.addMapper(conf, UFOCountingRecordValidationMapper.class, LongWritable.class, Text.class, LongWritable.class, Text.class, true, mapconf1);
JobConf mapconf2 = new JobConf(false);
ChainMapper.addMapper(conf, MapClass.class, LongWritable.class, Text.class, Text.class, LongWritable.class, true, mapconf2);
conf.setMapperClass(ChainMapper.class);
conf.setCombinerClass(LongSumReducer.class);
conf.setReducerClass(LongSumReducer.class); FileInputFormat.setInputPaths(conf, args[0]);
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
- 编译上述的两个文件
javac UFOCountingRecordValidationMapper.java UFOLocation3.java
- 将编译好的文件打包成jar文件
jar cvf ufo3.jar UFO*class
- 在hadoop上执行jar包
hadoop cvf ufo3.jar UFOLocation3 ufo.tsv output
- 查看输出结果
hadoop dfs -cat output/part-00000
- 在mapreduce web ui页面上查看统计信息
- 相应的job,进入job的统计信息页面

- 查看统计信息

[hadoop](3) MapReduce:创建计数器、任务状态和写入日志的更多相关文章
- Hadoop基础-MapReduce的工作原理第一弹
Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...
- Hadoop基础-MapReduce的排序
Hadoop基础-MapReduce的排序 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MapReduce的排序分类 1>.部分排序 部分排序是对单个分区进行排序,举个 ...
- Hadoop 新 MapReduce 框架 Yarn 详解【转】
[转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/] 简介: 本文介绍了 Hadoop 自 0.23.0 版本 ...
- hadoop之mapreduce详解(进阶篇)
上篇文章hadoop之mapreduce详解(基础篇)我们了解了mapreduce的执行过程和shuffle过程,本篇文章主要从mapreduce的组件和输入输出方面进行阐述. 一.mapreduce ...
- hadoop(二MapReduce)
hadoop(二MapReduce) 介绍 MapReduce:其实就是把数据分开处理后再将数据合在一起. Map负责“分”,即把复杂的任务分解为若干个“简单的任务”来并行处理.可以进行拆分的前提是这 ...
- Hadoop 新 MapReduce 框架 Yarn 详解
Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...
- 用PHP编写Hadoop的MapReduce程序
用PHP编写Hadoop的MapReduce程序 Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...
- HADOOP之MAPREDUCE程序应用二
摘要:MapReduce程序进行单词计数. 关键词:MapReduce程序 单词计数 数据源:人工构造英文文档file1.txt,file2.txt. file1.txt 内容 Hello Ha ...
- 对于Hadoop的MapReduce编程makefile
根据近期需要hadoop的MapReduce程序集成到一个大的应用C/C++书面框架.在需求make当自己主动MapReduce编译和打包的应用. 在这里,一个简单的WordCount1一个例子详细的 ...
随机推荐
- 关于一段有趣代码引出的String创建对象的解释
通常来说,我们认为hashCode不相同就为不同的对象.就这样由一段代码引发了一场讨论,代码如下: @Test public void stringCompare() { String s1 = &q ...
- Shell脚本中单引号(‘)和双引号(“)的使用区别
在Linux操作系统上编写Shell脚本时候,我们是在变量的前面使用$符号来获取该变量的值,通常在脚本中使用”$param”这种带双引号的格式,但也有出现使用'$param'这种带引号的使用的场景,首 ...
- 查询IP地址
在黑窗口里面输入:ipconfig
- Mac015--在Mac下安装使用Vagrant
网址:http://yansu.org/2014/04/10/install-vagrant-in-mac.html 一.安装Vagrant 下载地址在http://www.vagrantup.com ...
- 今天起,重新开头学习Java - 一、安装环境
先拜领路人 https://blog.csdn.net/u011541946/article/category/6951961/3? 一.安装JDK 1. 下载 www.java.com JDK是Ja ...
- Java相关面试题总结+答案(十)
[JVM] 194. 说一下 JVM 的主要组成部分?及其作用? 类加载器(ClassLoader) 运行时数据区(Runtime Data Area) 执行引擎(Execution Engine) ...
- 词频统计小程序-WordCount.exe
一. 背景 最近顶哥为了完成学历提升学业中的小作业,做了一个词频统计的.exe小程序.因为当时做的时候网上的比较少,因此顶哥决定把自己拙略的作品发出来给需要的人提供一种思路,希望各位看官不要dis ...
- 《剑指offer》面试题18 树的子结构 Java版
(输入两棵二叉树A和B,判断B是不是A的子结构.补充下,根据书中的代码来看,子结构的定义并不包括叶子节点下的null,也就是说只要B的存在数字的结构存在于A中就行,那么如果B是null树,那么就不属于 ...
- [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...
- expdp和impdp的应用-高版本通过dblink导入到低版本
今天接到要进行数据库用户的部分数据迁移需求,需求如下 IMP.WO.INSA开头的表只要结构,不要数据 B.TEMP.TMP开头的表不用导 其他表需要导出数据和表结构,同时要求导出此用户下的所有其他对 ...