MapReduce

一种分布式计算模型,解决海量数据的计算问题,MapReduce将计算过程抽象成两个函数

Map(映射):对一些独立元素(拆分后的小块)组成的列表的每一个元素进行指定的操作,可以高度并行。

Reduce(化简):对一个列表的元素进行合并

input -> map -> reduce -> output
数据流通格式<kay,value> eg:
原始数据 -> map input map map output(reduce input) shuffle reduce reduce output
example example -> <0,example example> -> <example,1> <example,1> -> <example,list(1,1,1)> -> <example,3>
helo wrold example -> <16,helo wrold example> -> <hello,1> <wrold,1> <example,1> -> <hello,list(1)> <...> -> <hello,1> <wrold,1>

MapReduce底层执行流程

一.Input

InputFormat

读取数据
转换成<key,value>

FileInputFormat

TextInputFormat 文本初始化,一行变成一个KY对,用偏移量作为Key、

二.Map

ModuleMapper类继承Mapper类

执行map(KEYIN,VALUEIN,KETOUT,VALUEOUT),
默认情况下
KEYIN:LongWritable
KEYVALUE:TEXT

三.shuffle(洗牌)

map,output<key,value>

a)先存在内存中
b)合并combiner[可选] -> <hadoop,1> <hadoop,1> =>> <hadoop,2>
c)spill,溢写到磁盘中,存储成很多小文件,过程如下
1.分区Partition(数量跟Reduce数量一致)
2.在分区内进行排序sort
d)合并,Merge ->大文件(Map Task任务运行的机器的本地磁盘中)
e)排序sort
f)压缩[可选]

四.reduce

reduce Task会到Map Task运行的机器上COPY要处理的数据

a)合并merge
b)排序
c)分组Group(相同的key的value放在一起)

ModuleReduceper类继承Reduce类

执行reduce(KEYIN,VALUEIN,KETOUT,VALUEOUT)
map的输出类型就是reduce的输入类型,中间的shuffle只是进行合并分组排序,不会改变数据类型

五.output

OutputFormat

写数据

FileOutputFormat

TextInputFormat 每个KeyValue对输出一行,key和value之间使用分隔符\t,默认调用key和value的toString方法

代码如下:

package com.cenzhongman.mapreduce;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.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.Tool;
import org.apache.hadoop.util.ToolRunner; //继承Configured类,从而继承了该类的getConf();line 81
//实现Tool方法,实现run方法 line79
//通过Toolrunner工具类的run方法实现,setConf(),达到conf传递的效果
public class WordCount extends Configured implements Tool {
// 1.Map class
public static class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private Text mapOutputKey = new Text();
private final static IntWritable mapOutputValue = new IntWritable(1); @Override
public void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// line value
String lineValue = value.toString(); // split
// String[] strs = lineValue.split(" ");
StringTokenizer stringTokennizer = new StringTokenizer(lineValue); // iterator
while (stringTokennizer.hasMoreTokens()) {
// get word value
String wordValue = stringTokennizer.nextToken(); // set value
mapOutputKey.set(wordValue); // output
context.write(mapOutputKey, mapOutputValue);
}
} @Override
public void cleanup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// nothing
// 在执行map之前会执行该函数,可用于JDBC等
// Reduce同理,不再重复
} @Override
public void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// nothing
// 在执行map之后会执行该函数,可用于JDBC断开等
} } // 2.Reduce class
public static class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable reduceOutputValue = new IntWritable(); @Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
// sum tmp
int sum = 0;
// iterator
for (IntWritable value : values) {
// total
sum += value.get();
}
// set value
reduceOutputValue.set(sum); // output
context.write(key, reduceOutputValue); } } // 3.driver
public int run(String[] args) throws Exception {
// 1.get configuration
Configuration conf = getConf(); // 2.create Job
Job job = Job.getInstance(conf, this.getClass().getSimpleName());
// run jar
job.setJarByClass(this.getClass()); // 3.set job
// input -> map -> reduce -> output
// 3.1 input from type
Path inPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inPath); // 3.2 map
job.setMapperClass(WordcountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); //****************shuffle配置***********************
//1)Partition分区
// job.setPartitionerClass(cls);
//2)sort排序
// job.setSortComparatorClass(cls);
//combiner[可选]Map中的合并
// job.setCombinerClass(cls);
//Group分组
// job.setGroupingComparatorClass(cls);
//压缩设置在配置文件中设置,也可以在conf对象中设置 //****************shuffle配置*********************** // 3.3 reduce
job.setReducerClass(WordcountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); // 3.4 output
Path outPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outPath); // 4 submit job
boolean isSuccess = job.waitForCompletion(true); //set reduce number[可选,优化方式之一,默认值为1]配置文件mapreduce.job.reduces
job.setNumReduceTasks(2); return isSuccess ? 0 : 1;
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
//set compress设置压缩方式,可以从官方文件和源码中得到-----可选,优化方式之一
conf.set("mapreduce.map.output.compress", "true");
conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); // int status = new WordCount().run(args);
int status = ToolRunner.run(conf, new WordCount(), args);
System.exit(status);
}
}

MapReduce执行流程及程序编写的更多相关文章

  1. 016_笼统概述MapReduce执行流程结合wordcount程序

    数据传输<key,value>     File-->  <key,value>  -->map(key,value)  --> mapResult<k ...

  2. mapreduce执行流程

    角色描述:JobClient:执行任务的客户端JobTracker:任务调度器TaskTracker:任务跟踪器Task:具体的任务(Map OR Reduce) 从生命周期的角度来看,mapredu ...

  3. 2.25-2.26 MapReduce执行流程Shuffle讲解

    原文链接:https://langyu.iteye.com/blog/992916 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是 ...

  4. 003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程

    003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程 Java程序长啥样? 首先编写一个Java程序 记事本编写程序 打开记事本 1.wi ...

  5. 一个 Spark 应用程序的完整执行流程

    一个 Spark 应用程序的完整执行流程 1.编写 Spark Application 应用程序 2.打 jar 包,通过 spark-submit 提交执行 3.SparkSubmit 提交执行 4 ...

  6. MapReduce架构与执行流程

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

  7. [Hadoop]浅谈MapReduce原理及执行流程

    MapReduce MapReduce原理非常重要,hive与spark都是基于MR原理 MapReduce采用多进程,方便对每个任务资源控制和调配,但是进程消耗更多的启动时间,因此MR时效性不高.适 ...

  8. Mybatis入门程序编写

    执行原理 入门程序编写 1.pom.xml 文件 <dependencies> <dependency> <groupId>mysql</groupId> ...

  9. MapReduce作业的执行流程

    MapReduce任务执行总流程 一个MapReduce作业的执行流程是:代码编写 -> 作业配置 -> 作业提交 -> Map任务的分配和执行 -> 处理中间结果 -> ...

随机推荐

  1. ionic2 自定义cordova插件开发以及使用 (Android)

    如何写一个cordova 用于ionic2项目中呢,在搜索了一番之后,千篇一律,我都怀疑那些文章是不是全部都是复制来复制去的,而且都不是很详细.我自己也捣鼓了一下午,踩了很多坑.所以特此写这下这篇,记 ...

  2. Java阶段性测试--第四五六大题参考代码

    第四题:.此题要求用IO流完成 使用File类在D盘下创建目录myFiles, 并在myFiles目录下创建三个文件分别为:info1.txt, info2.txt, info3.txt . 代码: ...

  3. java中的各种流(老师的有道云笔记)

    内存操作流-字节 之前的文件操作流是以文件的输入输出为主的,当输出的位置变成了内存,那么就称为内存操作流.此时得使用内存流完成内存的输入和输出操作. 如果程序运行过程中要产生一些临时文件,可采用虚拟文 ...

  4. PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化)/约束类型/魔术方法小结

      前  言  OOP  学习了好久的PHP,今天来总结一下PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化). 1  PHP中的抽象 ...

  5. 【转载】SQL Server行转列,列转行

    行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER 2005 新增的运算符PIVOT来实现.用传统的方法,比较好理解.层次清 ...

  6. 对jsp的初步了解及规范问题(二)

    前言 今天的例子是用jsp制作简单的“艾宾浩斯记忆曲线的学习计划表”. 重点不是算法,重点是学习jsp中的一个重要的思想,作为展现层,jsp中不应该出现业务逻辑代码. 当中<%%>代码也会 ...

  7. vue-schart : vue.js 的图表组件

    介绍 vue-schart 是使用vue.js封装了sChart.js图表库的一个小组件.支持vue.js 1.x & 2.x 仓库地址:https://github.com/lin-xin/ ...

  8. 多重bash登入的history写入问题

    问题:如果一个用户同时开好几个 bash 接口, 这时~/.bash_history中会写入哪个bash的历史命令记录? 答:所有的bash 都有自己的 HISTSIZE 笔记录在内存中,因为等到注销 ...

  9. C#基础知识-XML介绍及基本操作(十)

    在讲了一系列的基础文档之后,现在开始讲一些实例.对于一些数据不是很大的程序,或者只是一些配置文件,需要本地存储的,完全可以使用XML代替数据库,因为只是去操作单个文件会比操作数据库要简单很多,在程序中 ...

  10. java源码学习(二)Integer

    Integer类包含了一个原始基本类型int.Integer属性中就一个属性,它的类型就是int. 此外,这个类还提供了几个把int转成String和把String转成int的方法,同样也提供了其它跟 ...