主从结构

  主节点:JobTracker(一个)

  从节点:TaskTrackers(多个)

JobTracker:

  接收客户提交的计算任务

  把计算任务分配给TaskTrackers执行

  监控TaskTracker执行情况

TaskTrackers:

  执行JobTracker分配的计算任务


MapReduce计算模型

  在Hadoop中,每个MapReduce任务都被初始化为一个Job,每个Job分为两个阶段:Map、Reduce。这两个阶段分别用两个函数表示 :Map、Reduce

  Map函数接收一个<key,value>形式的输入,产生同样形式的中间输出。Hadoop将所有相同key的value集合到一起传递给Reduce函数

  Reduce函数接收一个<key,(list of value)>形式的的呼入,然后对value集合进行处理输出结果。Reduce的输出也是<key,value>的形式

练习:

输入文本

姓名 分数

多个文本,内容行如上述,统计每个人的平均分

Map

 package org.zln.scorecount;

 import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException;
import java.util.StringTokenizer; /**
* Created by sherry on 15-7-12.
*/
public class ScoreMap extends Mapper<LongWritable,Text,Text,IntWritable> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();//将纯文本的数据转化为string
StringTokenizer tokenizer = new StringTokenizer(line,"\n");//切割
while (tokenizer.hasMoreTokens()){
StringTokenizer tokenizerLine = new StringTokenizer(tokenizer.nextToken());
String strName = tokenizerLine.nextToken();//姓名
String strScore = tokenizerLine.nextToken();//成绩 Text name = new Text(strName);
int scoreInt = Integer.parseInt(strScore);
context.write(name,new IntWritable(scoreInt));//输出姓名:成绩 }
}
}

Reduce

 package org.zln.scorecount;

 import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException;
import java.util.Iterator; /**
* Created by sherry on 15-7-12.
*/
public class ScoreReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int count = 0;
Iterator<IntWritable> intWritableIterator = values.iterator();
while (intWritableIterator.hasNext()){
sum += intWritableIterator.next().get();//总分
count++;//平均分
}
int avg = sum/count;
context.write(key,new IntWritable(avg));
}
}

Main

 package org.zln.scorecount;

 import org.apache.hadoop.conf.Configured;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* Created by sherry on 15-7-12.
*/
public class ScoreMain extends Configured implements Tool{
public int run(String[] args) throws Exception {
Job job = new Job(getConf());
job.setJarByClass(ScoreMain.class);
job.setJobName("ScoreCount"); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setMapperClass(ScoreMap.class);
job.setReducerClass(ScoreReduce.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean success = job.waitForCompletion(true);
return success?0:1;
} //统计平均分
public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new ScoreMain(), args);
System.exit(ret);
}
}


我们的Map与Reduce都继承了父类,并复写了map或reduce方法

父类中 还有 三个方法未作处理

setup:启动map/reduce后首先调用

cleanup:最后调用

run:每次调用的时候都会执行

MapReduce架构的更多相关文章

  1. HBase、HDFS和MapReduce架构异同简解

    HBase.HDFS和MapReduce架构异同 .. HBase(公司架构模型) HDFS2.0(公司架构模型) MR2.0(公司架构模型) MR1.0(公司架构模型) 中央 HMaster Nam ...

  2. MapReduce架构与执行流程

    一.MapReduce是用于解决什么问题的? 每一种技术的出现都是用来解决实际问题的,否则必将是昙花一现,那么MapReduce是用来解决什么实际的业务呢? 首先来看一下MapReduce官方定义: ...

  3. 2本Hadoop技术内幕电子书百度网盘下载:深入理解MapReduce架构设计与实现原理、深入解析Hadoop Common和HDFS架构设计与实现原理

    这是我收集的两本关于Hadoop的书,高清PDF版,在此和大家分享: 1.<Hadoop技术内幕:深入理解MapReduce架构设计与实现原理>董西成 著  机械工业出版社2013年5月出 ...

  4. MapReduce架构与生命周期

    MapReduce架构与生命周期 概述:MapReduce是hadoop的核心组件之一,可以通过MapReduce很容易在hadoop平台上进行分布式的计算编程.本文组织结果如下:首先对MapRedu ...

  5. MapReduce架构设计

    MapReduce采用Master/Slave的架构,其架构图如下: 它主要有以下4个部分组成: 1)Client 2)JobTracker JobTracke负责资源监控和作业调度.JobTrack ...

  6. 第二代map-reduce架构YARN解析

    需求 我们在考虑hadoop map-reduce框架的时候,最重要需包括: 1. reliability 可靠性,主要是jobtracker,resource manager可靠性 2. avail ...

  7. MapReduce架构和算法(2)

    一个.combiner计划 每map它可能会产生大量的输出,combiner的作用是map输出端先做合并.reducer的数据量. combiner最基本是实现本地key的归并,combiner具有类 ...

  8. 【转】五分钟读懂大数据核心MapReduce架构及原理

    什么是MapReduce Hadoop中的MapReduce是一个简单的软件框架,基于它写出的应用程序可以运行在由上千个商用机器组成的大型集群上,并以一种可靠容错式并行处理TB级数据 MapReduc ...

  9. 初步掌握MapReduce的架构及原理

    目录 1.MapReduce定义 2.MapReduce来源 3.MapReduce特点 4.MapReduce实例 5.MapReduce编程模型 6.MapReduce 内部逻辑 7.MapRed ...

随机推荐

  1. java 基础词汇 必须 第九天

    Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ...

  2. jquery 操作css 选择器

    .addClass() 为每个匹配的元素添加指定的样式类名 .addClass(className) className 为每个匹配元素所有增加的一个或多个样式名 .addClass(function ...

  3. react中密码自动填充及解决火狐浏览器,360浏览器记住密码后,密码框自动填充终极解决方案

    先直接上核心代码如下: 在火狐浏览器,360浏览器,初次加载,bug长这样: 如果你想通过生命周期componentDidMounted等生命周期进行置空操作都是不行的,这可能是浏览器自带的特性记住密 ...

  4. MySQL5.6基于MHA方式高可用搭建

    master 10.205.22.185 #MHA node slave1 10.205.22.186  #MHA node+MHA manager slave2 10.205.22.187  #MH ...

  5. 【Ecshop】商品数据采集扩展

    一个自用的Ecshop商品数据采集程序 ->到此下载

  6. C语言结构体指针成员强制类型转换

    #include <stdio.h> #include <stdlib.h> typedef struct ListElmt_ { void *data; struct Lis ...

  7. Python的输入和输出问题详解

    输出用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') pr ...

  8. 第三章 文件 I/O

    3.1 引言 先说明可用的文件 I/O 函数:open.read.write.close,然后说明不同缓冲区长度对read和write函数的影响. 本章所说的函数经常被称为不带缓冲的 I/O (unb ...

  9. POJ:3262-Protecting the Flowers

    Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8606 Accepted: 347 ...

  10. Idea搭建spring framework源码环境

    spring的源码目前放在github上,https://github.com/spring-projects/spring-framework 一.安装Git 二.安装Gradle gradle为解 ...