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计算了,今天和大家分享下这个 ...
随机推荐
- WPF MultiDataTrigger
huhu <Style x:Key="Cell" TargetType="{x:Type Button}"> <Setter Property ...
- POJ 3191 The Moronic Cowmpouter(进制转换)
题目链接 题意 : 将一个10进制整数转化为-2进制的数. 思路 :如果你将-2进制下的123转化为十进制是1*(-2)^2+2*(-2)^1+3*(-2)^0.所以十进制转化为-2进制就是一个逆过程 ...
- POJ2993——Help Me with the Game(字符串处理+排序)
Help Me with the Game DescriptionYour task is to read a picture of a chessboard position and print i ...
- 哈希值识别工具hash-identifier
Hash Identifier可以用来识别各种类型的哈希值.在kali上使用方法很简单 (1)搜索hash-identifier (2)在HASH后面输入要识别的hash内容 (3)识别成功 wind ...
- Android Spannable
ApiDemo 源码至 com.example.android.apis.text.Link 类. 首先,看一下其运行效果: 要给 TextView 加上效果,方式主要有几种: 第一种,自动应用效果, ...
- gitHub for windows设置网络代理
在公司网络环境下使用gitHub,一直出错,因为公司使用的代理.但是gitHub for windows无法设置代理. 可以通过C:\Users\admin\.gitconfig中,添加代理. 添加: ...
- kali获得已经安装的软件列表
有人说是用dpkg -l ,也有说是用dpkg --get-selections. debian:~# dpkg -l|grep install|wc -l2678debian:~# dpkg --g ...
- [swustoj 1095] 挖金子
挖金子(1095) 题目描述 你在一个N*M的区域中,一开始在(1,1)的位置,每个位置有可能有金子,也有可能不能到达,也有可能有传送门.你只能往右或者下走,不能走出这个区域.当你位于传送门时,传送门 ...
- YQL
YQL,(Yahoo! Query Language)是一种支持对互联网上的数据进行查询.过滤.连接.类似SQL语法的简单语言.用YQL官方的话:有了YQL,开发人员只需要使用一种 简单的查询语言即可 ...
- jwplayer 网页在线播放插件
1.到官网 https://www.jwplayer.com/ 注册,取得key并下载免费版本(免费版只支持mp4格式): 2.编辑如下网页即可在线播放: <!DOCTYPE html> ...