MapReduce计算模型
MapReduce计算模型
- MapReduce两个重要角色:JobTracker和TaskTracker。
MapReduce Job
每个任务初始化一个Job,没个Job划分为两个阶段:Map和Reduce阶段。
Map函数接受一个
<key, value>形式的输入,输出一个<key, value>形式的中间输出。Hadoop负责将所有的相同中间key值的value集合到一起传递给Reduce函数。
Reduce函数接受一个
<key, (list of value)>,然后对value集合进行处理并输出结果,输出结果也是<key, value>形式的。
Hadoop 中的 Hello Word 程序
- wordCount 程序
package com.felix;
import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
public class WordCount
{
public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, IntWritable>
{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException
{
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens())
{
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable>
{
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException
{
int sum = 0;
while (values.hasNext())
{
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception
{
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount"); //设置一个用户定义的job名称
conf.setOutputKeyClass(Text.class); //为job的输出数据设置Key类
conf.setOutputValueClass(IntWritable.class); //为job输出设置value类
conf.setMapperClass(Map.class); //为job设置Mapper类
conf.setCombinerClass(Reduce.class); //为job设置Combiner类
conf.setReducerClass(Reduce.class); //为job设置Reduce类
conf.setInputFormat(TextInputFormat.class); //为map-reduce任务设置InputFormat实现类
conf.setOutputFormat(TextOutputFormat.class); //为map-reduce任务设置OutputFormat实现类
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf); //运行一个job
}
}
处理流程:读取文件 -> Map -> 切出其中的单词并标记数目为1,如
<word, 1>-> Reduce -> 收集相同key值的value形成<key, list of value>-> 将所有的1加起来 -> 输出<key, value>,即<word, count>执行过程:
// 初始化
JobConf conf = new JobConf(MyMapre.class);
// 为job命名
conf.setJobName("wordcount");
// 设成供Map处理的<key, value>对
conf.setInputFormat(TestInputFormat.class);
// 设置出输出格式
conf.setOutputFormat(TestOutputFormat.class);
conf.setMapperClass(Map.class);
conf.setReduceClass(Reduce.class);
// 设置输入输出路径
FileInputFormat.setInputPath(conf, new Path(args[0]);
FileOutputFormat.setOutputPath(conf, new Path(args[1]);
TextInputFormat中,没一行都会生成一条记录,每条记录表示为
<key, value>:key值是每个数据的记录在数据分片中的字节偏移量,数据类型是LongWritable
value是每行的内容,数据类型是Text
Map()函数集成自MapReduceBase
实现Mapper接口,有四种形式的参数(key值类型,输入的value值的类型,输出的key值类型,输出的value值类型)
实现此接口还要实现Map()方法,负责具体的对输入进行操作
本例中,首先以空格为单位进行切片,然后使用OutputCollect收集输出的
<word, 1>Reduce()与Map()相似
本例中,输入
<Text, IntWritable>,输出<Text, IntWritable>。
运行MapReduce程序
Eclipse中运行
命令行运行:
// 建立FirstJar
mkdir FirstJar
// 编译生成.class文件,存放到FirstJar中
javac -classpath ~/hadoop/hadoop-core.jar -d FirstJar
WordCount.java
jav -cvf wordcount.jar -C FirstJar/.
- 上传文件到hadoop
hadoop fs -mkdir ...
hadoop fs -put ...
- 运行
hadoop jar wordcount.jar WordCount input output
新的API
- 新的程序:
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static class TokenizerMapper extends
Mapper {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer tokenizer = new StringTokenizer(value.toString());
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer extends
Reducer {
private IntWritable result = new IntWritable();
protected void reduce(Text key, Iterator values,
Context context) throws IOException, InterruptedException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws IOException,
InterruptedException, ClassNotFoundException {
// section 1
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage : wordcount ");
System.exit(2);
}
Job job = new Job(conf, "wordcount");
job.setJarByClass(WordCount.class);
// section2
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// section3
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
// section4
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// section5
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
新的API中,Mapper和Reducer不是接口而是抽象类。Map和Reduce函数竭诚Mapper和Reducer抽象类。
更加频繁的用context对象,进行MapReduce间的通信,MapContext充当OutputCollector和Reporter。
Job的配置统一由Configuration来完成,不必额外的使用JobConf对守护进程进行配置。
由Job来负责Job的控制,而不是JobClient。
MapReduce的数据流和控制流
- 简单的控制流:
JobTracker调度任务给TaskTracker,TaskTracker执行任务时,返回进度报告。JobTracker记录进度的进行状况,如果某个TaskTracker上的任务失败,那么JobTracker会把这个任务分配给另一台TaskTracker。
MapReduce优化
MapReduce任务擅长处理少量的大数据,而在处理大量的小数据时,MapReduce性能会逊色不少。
当以个Map任务的运行时间在一分钟左右比较合适。
MapReduce任务槽:Map/Reduce任务槽就是这个集群能够同事运行的Map/Reduce任务的最大数量。
MapReduce计算模型的更多相关文章
- MapReduce计算模型的优化
MapReduce 计算模型的优化涉及了方方面面的内容,但是主要集中在两个方面:一是计算性能方面的优化:二是I/O操作方面的优化.这其中,又包含六个方面的内容. 1.任务调度 任务调度是Hadoop中 ...
- MapReduce计算模型二
之前写过关于Hadoop方面的MapReduce框架的文章MapReduce框架Hadoop应用(一) 介绍了MapReduce的模型和Hadoop下的MapReduce框架,此文章将进一步介绍map ...
- 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型
前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...
- MapReduce 计算模型
前言 本文讲解Hadoop中的编程及计算模型MapReduce,并将给出在MapReduce模型下编程的基本套路. 模型架构 在Hadoop中,用于执行计算任务(MapReduce任务)的机器有两个角 ...
- 第四篇:MapReduce计算模型
前言 本文讲解Hadoop中的编程及计算模型MapReduce,并将给出在MapReduce模型下编程的基本套路. 模型架构 在Hadoop中,用于执行计算任务(MapReduce任务)的机器有两个角 ...
- 【MapReduce】二、MapReduce编程模型
通过前面的实例,可以基本了解MapReduce对于少量输入数据是如何工作的,但是MapReduce主要用于面向大规模数据集的并行计算.所以,还需要重点了解MapReduce的并行编程模型和运行机制 ...
- 【MapReduce】经常使用计算模型具体解释
前一阵子參加炼数成金的MapReduce培训,培训中的作业样例比較有代表性,用于解释问题再好只是了. 有一本国外的有关MR的教材,比較有用.点此下载. 一.MapReduce应用场景 MR能解决什么问 ...
- 重要 | Spark和MapReduce的对比,不仅仅是计算模型?
[前言:笔者将分上下篇文章进行阐述Spark和MapReduce的对比,首篇侧重于"宏观"上的对比,更多的是笔者总结的针对"相对于MapReduce我们为什么选择Spar ...
- 使用mapreduce计算环比的实例
最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...
随机推荐
- 【技术贴】webservice cxf2 客户端动态调用报错No operation was found with the name
No operation was found with the name xxx 出错原因是因为发布服务的接口所在包路径和此接口实现类包路径不一致,比如你的服务接口可能放在了包com.x.interF ...
- Apache URL rewrite 配置
下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...
- hdu 1018
数学题 用的这个方法比较烂 g++超时 c++ 406ms /******************************************************************* ...
- 云风的BLOG❳可靠 UDP 传输
http://mp.weixin.qq.com/s?__biz=MzA3NjYxOTA0MQ==&mid=405432715&idx=1&sn=2e40ceafd4b298e1 ...
- Time.deltaTime 含义和应用
第一種:使用Time.deltaTime 一秒內從第1個Frame到最後一個Frame所花的時間,所以不管電腦是一秒跑60格或者一秒30格.24格,值都會趨近於一. 就結果而言,deltaTime是為 ...
- winform自定义文件程序-- 不允许所请求的注册表访问权(ZSSQL)
常见问题1: 不允许所请求的注册表访问权 win7.win8 双击程序文件ZSSQL时候会出现 不允许所请求的注册表访问权 的弹窗异常 解决方法:ZSSQL.exe 右键 属性--兼容性--以管理员身 ...
- H264/AVC视频解码时AVC1和H264的区别
AVC1与H264的区别 http://blog.csdn.net/qiuchangyong/article/details/6660253 H.264 Video Types The followi ...
- C语言字节对齐
转自:http://blog.csdn.net/21aspnet/article/details/6729724 文章最后本人做了一幅图,一看就明白了,这个问题网上讲的不少,但是都没有把问题说透. 一 ...
- php 23种设计模式的趣味解释
http://wenku.baidu.com/link?url=GwvuvSOdJneZQc-DSKoGmPcxTtzn3cdtIp3fRaCNbkg1zJDZZZTx2NwEK5IsqU996fG3 ...
- pb 插入控件是出问题
http://m.blog.csdn.net/blog/jqz1225/34417493 是的http://blog.163.com/wufeng_1213/blog/static/167783313 ...