Hadoop-MapReduce

Hadoop-MapReduce

1 MapReduce 概述

1.1 MapReduce 定义

MapReduce 是一个分布式运算程序的编程框架,是用户开发“基于 Hadoop 的数据分析 应用”的核心框架。

MapReduce 核心功能是将用户编写的业务逻辑代码自带默认组件整合成一个完整的 分布式运算程序,并发运行在一个 Hadoop 集群上。

1.2 MapReduce 优缺点

1.2.1 优点

1)MapReduce 易于编程

它简单的实现一些接口,就可以完成一个分布式程序,这个分布式程序可以分布到大量 廉价的 PC 机器上运行。也就是说你写一个分布式程序,跟写一个简单的串行程序是一模一 样的。就是因为这个特点使得 MapReduce 编程变得非常流行。

2)良好的扩展性

当你的计算资源不能得到满足的时候,你可以通过简单的增加机器来扩展它的计算能力。

3)高容错性

MapReduce 设计的初衷就是使程序能够部署在廉价的 PC 机器上,这就要求它具有很高 的容错性。比如其中一台机器挂了,它可以把上面的计算任务转移到另外一个节点上运行, 不至于这个任务运行失败,而且这个过程不需要人工参与,而完全是由 Hadoop 内部完成的。

4)适合 PB 级以上海量数据的离线处理

可以实现上千台服务器集群并发工作,提供数据处理能力。

1.2.2 缺点

1)不擅长实时计算

MapReduce 无法像 MySQL 一样,在毫秒或者秒级内返回结果。

2)不擅长流式计算

流式计算的输入数据是动态的,而 MapReduce 的输入数据集是静态的,不能动态变化。 这是因为 MapReduce 自身的设计特点决定了数据源必须是静态的。

3)不擅长 DAG(有向无环图)计算

多个应用程序存在依赖关系,后一个应用程序的输入为前一个的输出。在这种情况下, MapReduce 并不是不能做,而是使用后,每个 MapReduce 作业的输出结果都会写入到磁盘, 会造成大量的磁盘 IO,导致性能非常的低下

1.3 MapReduce 核心思想

在这里插入图片描述
(1)分布式的运算程序往往需要分成至少 2 个阶段。

(2)第一个阶段的 MapTask 并发实例,完全并行运行,互不相干。

(3)第二个阶段的 ReduceTask 并发实例互不相干,但是他们的数据依赖于上一个阶段
的所有 MapTask 并发实例的输出。

(4)MapReduce 编程模型只能包含一个 Map 阶段和一个 Reduce 阶段,如果用户的业
务逻辑非常复杂,那就只能多个 MapReduce 程序,串行运行。

总结:分析 WordCount 数据流走向深入理解 MapReduce 核心思想。

1.4 MapReduce 进程

一个完整的 MapReduce 程序在分布式运行时有三类实例进程:

(1)MrAppMaster:负责整个程序的过程调度及状态协调。

(2)MapTask:负责 Map 阶段的整个数据处理流程。

(3)ReduceTask:负责 Reduce 阶段的整个数据处理流程。

1.5 官方 WordCount 源码

采用反编译工具反编译源码,发现 WordCount 案例有 Map 类、Reduce 类和驱动类。且 数据的类型是 Hadoop 自身封装的序列化类型。

1.6 常用数据序列化类型

Java类型Hadoop Writable 类型
BooleanBooleanWritable
ByteByteWritable
IntIntWritable
FloatFloatWritable
LongLongWritable
DoubleDoubleWritable
StringText
MapMapWritable
ArrayArrayWritable
NullNullWritable

1.7 MapReduce 编程规范

用户编写的程序分成三个部分:Mapper、Reducer 和 Driver。

1.Mapper阶段

(1)用户自定义的Mapper要继承自己的父类

(2)Mapper的输入数据是KV对的形式(KV的类型可自定义)

(3)Mapper中的业务逻辑写在map()方法中

(4)Mapper的输出数据是KV对的形式(KV的类型可自定义)

(5)map()方法(MapTask进程)对每一个<K,V>调用一次

2.Reducer阶段

(1)用户自定义的Reducer要继承自己的父类

(2)Reducer的输入数据类型对应Mapper的输出数据类型,也是KV

(3)Reducer的业务逻辑写在reduce()方法中

(4)ReduceTask进程对每一组相同k的组调用一次reduce()方法

3.Driver阶段

相当于YARN集群的客户端,用于提交我们整个程序到YARN集群,提交的是 封装了MapReduce程序相关运行参数的job对象

1.8.1 本地测试

1)需求

在给定的文本文件中统计输出每一个单词出现的总次数

在这里插入图片描述
2)需求分析

按照 MapReduce 编程规范,分别编写 Mapper,Reducer,Driver。

3)环境准备

(1)创建 maven 工程,MapReduceDemo

(2)在 pom.xml 文件中添加如下依赖

<dependencies>
 	<dependency>
 		<groupId>org.apache.hadoop</groupId>
 		<artifactId>hadoop-client</artifactId>
 		<version>3.1.3</version>
 	</dependency>
 	<dependency>
 		<groupId>junit</groupId>
 		<artifactId>junit</artifactId>
 		<version>4.12</version>
 	</dependency>
 	<dependency>
	 	<groupId>org.slf4j</groupId>
 		<artifactId>slf4j-log4j12</artifactId>
 		<version>1.7.30</version>
 	</dependency>
</dependencies>

(3)在项目的 src/main/resources 目录下,新建一个文件,命名为“log4j.properties”,在 文件中填入。

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

1.8.2 编写测试类

Mapper

public class WorldCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private Text text = new Text();
    private IntWritable intWritable = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取一行
        String line = value.toString();
        //2. 切割
        String[] words = line.split(" ");
        //3. 循环写出
        for (String word : words) {
            text.set(word);
            //写出
            context.write(text, intWritable);
        }
    }
}

Reducer

public class WorldCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
    private IntWritable intWritable=new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //累加
        int sum=0;
        for (IntWritable value : values) {
            sum+=value.get();
        }
        intWritable.set(sum);
        //写出
        context.write(key, intWritable);
    }
}

Driver

public class WorldCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1 获取job
        Configuration configuration=new Configuration();
        Job job = Job.getInstance(configuration);
        //2 设置jar包路径
        job.setJarByClass(WorldCountDriver.class);
        //3 关联mapper和reduce
        job.setMapperClass(WorldCountMapper.class);
        job.setReducerClass(WorldCountReduce.class);
        //4 设置map输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5 设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //6 设置输入路径和输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\hadoop-test\\input"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop-test\\output"));
        //7 提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

1.8.3 提交到集群测试

修改路径参数(改为命令行参数)

FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

打包插件

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打好jar包,在linux上运行jar包(最小包,环境linux上面有)

com.cssl.WorldCountDriver(指定main函数)

hadoop jar MapReduce-Test-1.0-SNAPSHOT.jar com.cssl.WorldCountDriver /qingguo /output

2 Hadoop 序列化

2.1 序列化概述

1)什么是序列化

序列化就是把内存中的对象,转换成字节序列(或其他数据传输协议)以便于存储到磁 盘(持久化)和网络传输。

反序列化就是将收到字节序列(或其他数据传输协议)或者是磁盘的持久化数据,转换成内存中的对象。

2)为什么要序列化

一般来说,“活的”对象只生存在内存里,关机断电就没有了。而且“活的”对象只能 由本地的进程使用,不能被发送到网络上的另外一台计算机。 然而序列化可以存储“活的” 对象,可以将“活的”对象发送到远程计算机。

3)为什么不用 Java 的序列化

Java 的序列化是一个重量级序列化框架(Serializable),一个对象被序列化后,会附带 很多额外的信息(各种校验信息,Header,继承体系等),不便于在网络中高效传输。所以, Hadoop 自己开发了一套序列化机制(Writable)。

4)Hadoop 序列化特点:

(1)紧凑 :高效使用存储空间。

(2)快速:读写数据的额外开销小。

(3)互操作:支持多语言的交互

2.2 自定义 bean 对象实现序列化接口(Writable)

在企业开发中往往常用的基本序列化类型不能满足所有需求,比如在 Hadoop 框架内部 传递一个 bean 对象,那么该对象就需要实现序列化接口。

(1)必须实现 Writable 接口

(2)反序列化时,需要反射调用空参构造函数,所以必须有空参构造

(3)重写序列化方法

(4)重写反序列化方法

(5)注意反序列化的顺序和序列化的顺序完全一致

(6)要想把结果显示在文件中,需要重写 toString(),可用"\t"分开,方便后续用。

(7)如果需要将自定义的 bean 放在 key 中传输,则还需要实现 Comparable 接口,因为MapReduce 框中的 Shuffle 过程要求对 key 必须能排序。

2.3 序列化案例实操

1)需求

统计每一个手机号耗费的总上行流量、总下行流量、总流量

(1)输入数据 phone_data .txt

(2)输入数据格式:
在这里插入图片描述
(3)期望输出数据格式
在这里插入图片描述
2)需求分析
在这里插入图片描述
3)编写 MapReduce 程序

编写流量统计的 Bean 对象

public class FlowBean implements Writable {
    private long upFlow;
    private long downFlow;
    private long sumFlow;

    ...省略get and set

    public FlowBean() {
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sumFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.upFlow = dataInput.readLong();
        this.downFlow = dataInput.readLong();
        this.sumFlow = dataInput.readLong();
    }

    @Override
    public String toString() {
        return upFlow +"\t" + downFlow +"\t" + sumFlow;
    }
}

编写 Mapper 类

public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
    private  Text outK=new Text();
    private  FlowBean outV=new FlowBean();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取一行
        String line = value.toString();
        //2. 切割
        String[] split = line.split("\t");
        //3. 抓取想要的数据    手机号 上行流量 下行流量
        String phone=split[1];
        String up=split[split.length-3];
        String down=split[split.length-2];
        //4. 封装数据
        outK.set(phone);
        outV.setUpFlow(Long.parseLong(up));
        outV.setDownFlow(Long.parseLong(down));
        outV.setSumFlow();
        //5. 写出
        context.write(outK, outV);
    }
}

编写Reducer类

public class FlowReducer extends Reducer<Text,FlowBean,Text,FlowBean> {

    private FlowBean outV=new FlowBean();
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //1. 变量集合累加值
        long totalUp=0;
        long totalDown=0;
        for (FlowBean value : values) {
            totalUp+=value.getUpFlow();
            totalDown+=value.getDownFlow();
        }
        //2. 封装outK,outV
        outV.setUpFlow(totalUp);
        outV.setDownFlow(totalDown);
        outV.setSumFlow();

        //3. 写出
        context.write(key, outV);
    }
}

编写Driver类

public class FlowDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //获取job
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        //设置jar
        job.setJarByClass(FlowDriver.class);
        //绑定mapper和reducer
        job.setMapperClass(FlowMapper.class);
        job.setReducerClass(FlowReducer.class);
        //设置mapper输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        //设置最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        //设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\hadoop-test\\input"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop-test\\output"));
        //提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

3 MapReduce 框架原理

在这里插入图片描述

3.1 InputFormat 数据输入

3.1.1 切片与 MapTask 并行度决定机制

1)问题引出

MapTask 的并行度决定 Map 阶段的任务处理并发度,进而影响到整个 Job 的处理速度。

思考:1G 的数据,启动 8 个 MapTask,可以提高集群的并发处理能力。那么 1K 的数 据,也启动 8 个 MapTask,会提高集群性能吗?MapTask 并行任务是否越多越好呢?哪些因 素影响了 MapTask 并行度?

2)MapTask 并行度决定机制

数据块:Block 是 HDFS 物理上把数据分成一块一块。数据块是 HDFS 存储数据单位。

数据切片:数据切片只是在逻辑上对输入进行分片,并不会在磁盘上将其切分成片进行 存储。数据切片是 MapReduce 程序计算输入数据的单位,一个切片会对应启动一个 MapTask。
在这里插入图片描述

3.1.2 Job 提交流程源码和切片源码详解

1)Job 提交流程源码详解

waitForCompletion()
submit();
	// 1 建立连接
	connect();
		// 1)创建提交 Job 的代理
		new Cluster(getConfiguration());
			// (1)判断是本地运行环境还是 yarn 集群运行环境
			initialize(jobTrackAddr, conf);
	// 2 提交 job
	submitter.submitJobInternal(Job.this, cluster)
		// 1)创建给集群提交数据的 Stag 路径
		Path jobStagingArea = JobSubmissionFiles.getStagingDir(cluster, conf);
		// 2)获取 jobid ,并创建 Job 路径
		JobID jobId = submitClient.getNewJobID();
		// 3)拷贝 jar 包到集群
		copyAndConfigureFiles(job, submitJobDir);
		rUploader.uploadFiles(job, jobSubmitDir);
		// 4)计算切片,生成切片规划文件
		writeSplits(job, submitJobDir);
		maps = writeNewSplits(job, jobSubmitDir);
		input.getSplits(job);
		// 5)向 Stag 路径写 XML 配置文件
		writeConf(conf, submitJobFile);
		conf.writeXml(out);
		// 6)提交 Job,返回提交状态
		status = submitClient.submitJob(jobId,submitJobDir.toString(),job.getCredentials());

在这里插入图片描述
2)FileInputFormat 切片源码解析(input.getSplits(job))

(1)程序先找到你数据存储的目录。

(2)开始遍历处理(规划切片)目录下的每一个文件

(3)遍历第一个文件ss.txt

  1. 获取文件大小fs.sizeOf(ss.txt)

  2. 计算切片大小 computeSplitSize(Math.max(minSize,Math.min(maxSize,blocksize)))=blocksize=128M

  3. 默认情况下,切片大小=blocksize

  4. 开始切,形成第1个切片:ss.txt—0:128M 第2个切片ss.txt—128:256M 第3个切片ss.txt—256M:300M (每次切片时,都要判断切完剩下的部分是否大于块的1.1倍,不大于1.1倍就划分一块切片

  5. 将切片信息写到一个切片规划文件中

  6. 整个切片的核心过程在getSplit()方法中完成

  7. InputSplit只记录了切片的元数据信息,比如起始位置、长度以及所在的节点列表等。

(4)提交切片规划文件到YARN上,YARN上的MrAppMaster就可以根据切片规划文件计算开启MapTask个数

3.1.3 FileInputFormat切片大小的参数配置

(1)源码中计算切片大小的公式

Math.max(minSize, Math.min(maxSize, blockSize)); 
//默认值为1 
mapreduce.input.fileinputformat.split.minsize=1 
///默认值Long.MAXValue 
mapreduce.input.fileinputformat.split.maxsize= Long.MAXValue 

因此,默认情况下,切片大小=blocksize。

(2)切片大小设置

maxsize(切片最大值):参数如果调得比blockSize小,则会让切片变小,而且就等于配置的这个参数的值。

minsize(切片最小值):参数调的比blockSize大,则可以让切片变得比blockSize还大。

(3)获取切片信息API

// 获取切片的文件名称 
String name = inputSplit.getPath().getName(); 
// 根据文件类型获取切片信息 
FileSplit inputSplit = (FileSplit) context.getInputSplit();

3.1.4 TextInputFormat

1)FileInputFormat 实现类

思考:在运行 MapReduce 程序时,输入的文件格式包括:基于行的日志文件、二进制 格式文件、数据库表等。那么,针对不同的数据类型,MapReduce 是如何读取这些数据的呢?

FileInputFormat 常见的接口实现类包括:TextInputFormatKeyValueTextInputFormatNLineInputFormatCombineTextInputFormat 和自定义 InputFormat 等

2)TextInputFormat

TextInputFormat 是默认的 FileInputFormat 实现类。按行读取每条记录。键是存储该行在整个文件中的起始字节偏移量, LongWritable 类型。值是这行的内容,不包括任何行终止 符(换行符和回车符),Text 类型

3.1.5 CombineTextInputFormat 切片机制

框架默认的 TextInputFormat 切片机制是对任务按文件规划切片,不管文件多小,都会 是一个单独的切片,都会交给一个 MapTask,这样如果有大量小文件,就会产生大量的 MapTask,处理效率极其低下。

1)应用场景:

CombineTextInputFormat 用于小文件过多的场景,它可以将多个小文件从逻辑上规划到 一个切片中,这样,多个小文件就可以交给一个 MapTask 处理。

2)虚拟存储切片最大值设置

CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);// 4m

注意:虚拟存储切片最大值设置最好根据实际的小文件大小情况来设置具体的值。

3)切片机制

生成切片过程包括:虚拟存储过程和切片过程二部分。

在这里插入图片描述

3.1.6 CombineTextInputFormat 案例实操

1)需求

将输入的大量小文件合并成一个切片统一处理。

(1)输入数据

准备 4 个小文件

(2)期望

期望一个切片处理 4 个文件

2)实现过程

(1)不做任何处理,运行 1.8 节的 WordCount 案例程序,观察切片个数为 4。

number of splits:4 

(2)在 WordcountDriver 中增加如下代码,运行程序,并观察运行的切片个数为 3。

驱动类中添加代码如下:

// 如果不设置 InputFormat,它默认用的是 TextInputFormat.class
job.setInputFormatClass(CombineTextInputFormat.class);
//虚拟存储切片最大值设置 4m
CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);

运行如果为 3 个切片。

number of splits:3

(3)将虚拟存储切片最大值设置 20m继续测试,切片结果为1

3.2 MapReduce 工作流程

在这里插入图片描述
在这里插入图片描述
上面的流程是整个 MapReduce 最全工作流程,但是 Shuffle 过程只是从第 7 步开始到第 16 步结束,具体 Shuffle 过程详解,如下:

(1)MapTask 收集我们的 map()方法输出的 kv 对,放到内存缓冲区中

(2)从内存缓冲区不断溢出本地磁盘文件,可能会溢出多个文件

(3)多个溢出文件会被合并成大的溢出文件

(4)在溢出过程及合并的过程中,都要调用 Partitioner 进行分区和针对 key 进行排序

(5)ReduceTask 根据自己的分区号,去各个 MapTask 机器上取相应的结果分区数据

(6)ReduceTask 会抓取到同一个分区的来自不同 MapTask 的结果文件,ReduceTask 会将这些文件再进行合并(归并排序)

(7)合并成大文件后,Shuffle 的过程也就结束了,后面进入 ReduceTask 的逻辑运算过 程(从文件中取出一个一个的键值对 Group,调用用户自定义的 reduce()方法)

注意:

(1)Shuffle 中的缓冲区大小会影响到 MapReduce 程序的执行效率,原则上说,缓冲区 越大,磁盘 io 的次数越少,执行速度就越快。

(2)缓冲区的大小可以通过参数调整,参数:mapreduce.task.io.sort.mb 默认 100M。

3.3 Shuffle 机制

3.3.1 Shuffle 机制

Map 方法之后,Reduce 方法之前的数据处理过程称之为 Shuffle。
在这里插入图片描述

3.3.2 Partition 分区

1、问题引出

要求将统计结果按照条件输出到不同文件中(分区)。比如:将统计结果按照手机 归属地不同省份输出到不同文件中(分区)

2、默认Partitioner分区

public class HashPartitioner<K, V> extends Partitioner<K, V> {
	public int getPartition(K key, V value, int numReduceTasks) {
		return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
	}
}

默认分区是根据key的hashCode对ReduceTasks个数取模得到的。用户没法控制哪个 key存储到哪个分区。

3、自定义Partitioner步骤

(1)自定义类继承Partitioner,重写getPartition()方法

public class CustomPartitioner extends Partitioner<Text, FlowBean> {
	@Override
	public int getPartition(Text key, FlowBean value, int numPartitions) {
		
		...控制分区代码逻辑
		
		return partition;
	}
}

(2)在Job驱动中,设置自定义Partitioner

job.setPartitionerClass(CustomPartitioner.class);

(3)自定义Partition后,要根据自定义Partitioner的逻辑设置相应数量的ReduceTask

job.setNumReduceTasks(5);

3.3.3 Partition 分区案例实操

1)需求

将统计结果按照手机归属地不同省份输出到不同文件中(分区)

(1)输入数据

phone_data .txt
(2)期望输出数据

手机号 136、137、138、139 开头都分别放到一个独立的 4 个文件中,其他开头的放到 一个文件中。

2)在案例 2.3 的基础上,增加一个分区类

public class ProvincePartitioner extends Partitioner<Text, FlowBean> {

    @Override
    public int getPartition(Text text, FlowBean flowBean, int numPartitions) {
        String phone = text.toString();
        String prefix = phone.substring(0, 3);

        int partition;
        if("136".equals(prefix)){
            partition=0;
        }else if("137".equals(prefix)){
            partition=1;
        }else if("138".equals(prefix)){
            partition=2;
        }else if("139".equals(prefix)){
            partition=3;
        }else{
            partition=4;
        }
        return partition;
    }
}

3)在驱动函数中指定自定义数据分区设置和 ReduceTask个数

//设置自定义PartitionerClass
job.setPartitionerClass(ProvincePartitioner.class);
//设置ReduceTasks个数
job.setNumReduceTasks(5);

4)分区总结

(1)ReduceTask > Partition ,则会多产生几个空的输出文件part-r-000xx;

(2)1 < ReduceTask < Partition,则有一部分分区数据无处安放,会Exception;

(3)ReduceTask = 1,则不管MapTask端输出多少个分区文件,最终结果都交给这一个 ReduceTask,最终也就只会产生一个结果文件 part-r-00000;

(4)分区号必须从零开始,逐一累加。

3.3.4 WritableComparable 排序

排序是MapReduce框架中最重要的操作之一。

MapTask和ReduceTask均会对数据按照key进行排序。该操作属于 Hadoop的默认行为。任何应用程序中的数据均会被排序,而不管逻辑上是 否需要

默认排序是按照字典顺序排序,且实现该排序的方法是快速排序

对于MapTask,它会将处理的结果暂时放到环形缓冲区中,当环形缓冲区使 用率达到一定阈值后,再对缓冲区中的数据进行一次快速排序,并将这些有序数 据溢写到磁盘上,而当数据处理完毕后,它会对磁盘上所有文件进行归并排序

对于ReduceTask,它从每个MapTask上远程拷贝相应的数据文件,如果文件大 小超过一定阈值,则溢写磁盘上,否则存储在内存中。如果磁盘上文件数目达到 一定阈值,则进行一次归并排序以生成一个更大文件;如果内存中文件大小或者 数目超过一定阈值,则进行一次合并后将数据溢写到磁盘上。当所有数据拷贝完 毕后,ReduceTask统一对内存和磁盘上的所有数据进行一次归并排序

(1)部分排序 MapReduce根据输入记录的键对数据集排序。保证输出的每个文件内部有序。

(2)全排序 最终输出结果只有一个文件,且文件内部有序。实现方式是只设置一个ReduceTask。但该方法在 处理大型文件时效率极低,因为一台机器处理所有文件,完全丧失了MapReduce所提供的并行架构。

(3)辅助排序:(GroupingComparator分组) 在Reduce端对key进行分组。应用于:在接收的key为bean对象时,想让一个或几个字段相同(全部 字段比较不相同)的key进入到同一个reduce方法时,可以采用分组排序。

(4)二次排序 在自定义排序过程中,如果compareTo中的判断条件为两个即为二次排序。

3.3.5 WritableComparable 排序案例实操(全排序)

1)需求

根据案例 2.3 序列化案例产生的结果再次对总流量进行倒序排序。

2)需求分析

在这里插入图片描述
把2.3案例的结果作为输入文件

3)修改案例2.3FlowBean,实现Comparable接口

public class FlowBean implements WritableComparable<FlowBean> {
    ```省略代码```
    
    @Override
    public int compareTo( FlowBean o) {
        //总流量的倒序排序
        if(this.sumFlow>o.getSumFlow()){
            return -1;
        }else if(this.sumFlow<o.getSumFlow()){
            return 1;
        }else{
            if(this.upFlow>o.getUpFlow()){
                return 1;
            }else if(this.upFlow<o.getUpFlow()){
                return -1;
            }else{
                return 0;
            }
        }
    }
}  

4)修改mapper的输出类型,K为FlowBean,V为Text

5)修改Reducer的输入类型,K为FlowBean,V为Text

@Override
protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    for (Text value : values) {
        context.write(value, key);
    }
}

6)修改Driver

//设置mapper输出类型
job.setMapOutputKeyClass(FlowBean.class);
job.setMapOutputValueClass(Text.class);

3.3.6 WritableComparable 排序案例实操(区内排序)

就是分区+排序(基于2.3案例)
而全排序意思就是只有一个结果文件

3.3.7 Combiner 合并

(1)Combiner是MR程序中Mapper和Reducer之外的一种组件。

(2)Combiner组件的父类就是Reducer。

(3)Combiner和Reducer的区别在于运行的位置

Combiner是在每一个MapTask所在的节点运行;

Reducer是接收全局所有Mapper的输出结果;

(4)Combiner的意义就是对每一个MapTask的输出进行局部汇总,以减小网络传输量。

(5)Combiner能够应用的前提是不能影响最终的业务逻辑,而且,Combiner的输出kv 应该跟Reducer的输入kv类型要对应起来。

(6)自定义 Combiner 实现步骤

(a)自定义一个 Combiner 继承 Reducer,重写 Reduce 方法

public class WordCountCombiner extends Reducer<Text, IntWritable,Text, IntWritable> {
    private IntWritable outV=new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        outV.set(sum);
        context.write(key, outV);
    }
}

(b)在 Job 驱动类中设置:

job.setCombinerClass(WordCountCombiner.class);

总结: 基本和写reducer没什么区别,注意业务逻辑

3.4 OutputFormat 数据输出

3.4.1 OutputFormat 接口实现类

OutputFormat是MapReduce输出的基类,所有实现MapReduce输出都实现了 OutputFormat 接口。下面我们介绍几种常见的OutputFormat实现类。

1.OutputFormat实现类

2.默认输出格式TextOutputFormat

3.自定义OutputFormat

3.1 应用场景: 例如:输出数据到MySQL/HBase/Elasticsearch等存储框架中。

3.2 自定义OutputFormat步骤

➢ 自定义一个类继承FileOutputFormat。

➢ 改写RecordWriter,具体改写输出数据的方法write()。

3.4.2 自定义 OutputFormat 案例实操

在这里插入图片描述
(1)编写 Mapper 类

public class OutPutFormatMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        context.write(value, NullWritable.get());
    }
}

(2)编写 Reducer 类

public class OutPutFormatReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        for (NullWritable value : values) {
            context.write(key, NullWritable.get());
        }
    }
}

(3)编写 OutputFormat 类

public class LogOutputFormat extends FileOutputFormat<Text , NullWritable> {
    @Override
    public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
        return new LogRecordWriter(job);
    }
}

(4)编写 RecordWriter 类

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private FSDataOutputStream atguigu;
    private FSDataOutputStream other;

    public LogRecordWriter(TaskAttemptContext job) {
        //创建两条流
        try {
            FileSystem fs = FileSystem.get(job.getConfiguration());
            atguigu = fs.create(new Path("D:\\hadoop-test\\output\\atguigu.log"));
            other = fs.create(new Path("D:\\hadoop-test\\output\\other.log"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String outK = key.toString();
        if(outK.contains("atguigu")){
            atguigu.writeBytes(outK+"\n");
        }else{
            other.writeBytes(outK+"\n");
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStreams(atguigu,other);
    }
}

(5)编写 Driver 类

public class OutPutFormatDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        
        ...核心代码...
            
        //设置自定义的 outputformat
        job.setOutputFormatClass(LogOutputFormat.class);
        //设置输入输出目录
        FileInputFormat.setInputPaths(job, new Path("D:\\hadoop-test\\input"));
        //虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
        //而 fileoutputformat 要输出一个_SUCCESS 文件,所以在这还得指定一个输出目录
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop-test\\output"));
    }
}

3.5 MapReduce 内核源码解析

3.5.1 MapTask 工作机制

在这里插入图片描述

3.5.2 ReduceTask 工作机制

在这里插入图片描述

3.5.3 ReduceTask 并行度决定机制

1)设置 ReduceTask 并行度(个数)

ReduceTask 的并行度同样影响整个 Job 的执行并发度和执行效率,但与 MapTask 的并 发数由切片数决定不同,ReduceTask 数量的决定是可以直接手动设置:

2)实验:测试 ReduceTask 多少合适

(1)实验环境:1 个 Master 节点,16 个 Slave 节点:CPU:8GHZ,内存: 2G

(2)实验结论:
在这里插入图片描述
3)注意事项

(1)ReduceTask=0,表示没有Reduce阶段,输出文件个数和Map个数一致。

(2)ReduceTask默认值就是1,所以输出文件个数为一个。

(3)如果数据分布不均匀,就有可能在Reduce阶段产生数据倾斜

(4)ReduceTask数量并不是任意设置,还要考虑业务逻辑需求,有些情况下,需要计算全 局汇总结果,就只能有1个ReduceTask。

(5)具体多少个ReduceTask,需要根据集群性能而定。

(6)如果分区数不是1,但是ReduceTask为1,是否执行分区过程。答案是:不执行分区过 程。因为在MapTask的源码中,执行分区的前提是先判断ReduceNum个数是否大于1。不大于1 肯定不执行。

3.5.4 MapTask & ReduceTask 源码解析

1)MapTask 源码解析流程

=================== MapTask ===================
context.write(k, NullWritable.get()); //自定义的 map 方法的写出,进入
	output.write(key, value);
		//MapTask727 行,收集方法,进入两次
		collector.collect(key, value,partitioner.getPartition(key, value,partitions));
			HashPartitioner(); //默认分区器
		collect() //MapTask1082 行 map 端所有的 kv 全部写出后会走下面的 close 方法
			close() //MapTask732 行
				collector.flush() // 溢出刷写方法,MapTask735 行,提前打个断点,进入
					sortAndSpill() //溢写排序,MapTask1505 行,进入
						sorter.sort() QuickSort //溢写排序方法,MapTask1625 行,进入
					mergeParts(); //合并文件,MapTask1527 行,进入
				collector.close(); //MapTask739 行,收集器关闭,即将进入 ReduceTask

2)ReduceTask 源码解析流程

=================== ReduceTask ===================
if (isMapOrReduce()) //reduceTask324 行,提前打断点
initialize() // reduceTask333 行,进入
init(shuffleContext); // reduceTask375 行,走到这需要先给下面的打断点
	totalMaps = job.getNumMapTasks(); // ShuffleSchedulerImpl 第 120 行,提前打断点
	merger = createMergeManager(context); //合并方法,Shuffle 第 80 行
		// MergeManagerImpl 第 232 235 行,提前打断点
		this.inMemoryMerger = createInMemoryMerger(); //内存合并
		this.onDiskMerger = new OnDiskMerger(this); //磁盘合并
rIter = shuffleConsumerPlugin.run();
	eventFetcher.start(); //开始抓取数据,Shuffle 第 107 行,提前打断点
	eventFetcher.shutDown(); //抓取结束,Shuffle 第 141 行,提前打断点
	copyPhase.complete(); //copy 阶段完成,Shuffle 第 151 行
	taskStatus.setPhase(TaskStatus.Phase.SORT); //开始排序阶段,Shuffle 第 152 行
sortPhase.complete(); //排序阶段完成,即将进入 reduce 阶段 reduceTask382 行
reduce(); //reduce 阶段调用的就是我们自定义的 reduce 方法,会被调用多次
cleanup(context); //reduce 完成之前,会最后调用一次 Reducer 里面的 cleanup 方法

3.6 Join 应用

3.6.1 Reduce Join

Map 端的主要工作:为来自不同表或文件的 key/value 对,打标签以区别不同来源的记录。然后用连接字段作为 key,其余部分和新加的标志作为 value,最后进行输出。

Reduce 端的主要工作:在 Reduce 端以连接字段作为 key 的分组已经完成,我们只需要 在每一个分组当中将那些来源于不同文件的记录(在 Map 阶段已经打标志)分开,最后进 行合并就 ok 了。

3.6.2 Reduce Join 案例实操

1)需求分析

通过将关联条件作为 Map 输出的 key,将两表满足 Join 条件的数据并携带数据所来源 的文件信息,发往同一个 ReduceTask,在 Reduce 中进行数据的串联。
在这里插入图片描述
2)代码实现

(1)创建商品和订单合并后的 TableBean 类

public class TableBean implements Writable {

    private String id;      //订单id
    private String pid;     //商品id
    private int amount;     //商品数量
    private String pname;   //商品名称
    private String flag;    //标记是哪张表

    ...省略get and set
    
    @Override
    public String toString() {
        return id + "\t" + pname + "\t" + amount;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(id);
        dataOutput.writeUTF(pid);
        dataOutput.writeInt(amount);
        dataOutput.writeUTF(pname);
        dataOutput.writeUTF(flag);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.id = dataInput.readUTF();
        this.pid = dataInput.readUTF();
        this.amount = dataInput.readInt();
        this.pname = dataInput.readUTF();
        this.flag = dataInput.readUTF();
    }
}

(2)编写 TableMapper 类

public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {

    private String fileName;
    private Text outK=new Text();
    private TableBean outV=new TableBean();
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //初始化,有两个文件
        FileSplit split = (FileSplit) context.getInputSplit();
        //获取文件名
        fileName = split.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取一行
        String line = value.toString();
        //2. 判断是哪个文件的
        if(fileName.contains("order")){//处理的是订单表
            String[] split = line.split("\t");
            outK.set(split[1]);
            outV.setId(split[0]);
            outV.setPid(split[1]);
            outV.setAmount(Integer.parseInt(split[2]));
            outV.setPname("");
            outV.setFlag("order");
        }else{//处理的是商品表
            String[] split = line.split("\t");
            outK.set(split[0]);
            outV.setId("");
            outV.setPid(split[0]);
            outV.setAmount(0);
            outV.setPname(split[1]);
            outV.setFlag("pd");
        }
        context.write(outK, outV);
    }
}

(3)编写 TableReducer 类

public class TableReducer extends Reducer<Text,TableBean,TableBean, NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
        List<TableBean> orderBeans = new ArrayList<>();
        TableBean pdBean = new TableBean();

        for (TableBean value : values) {
            if("order".equals(value.getFlag())){//订单表
                TableBean temp = new TableBean();
                try {
                    BeanUtils.copyProperties(temp, value);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
                orderBeans.add(temp);
            }else{//商品表
                try {
                    BeanUtils.copyProperties(pdBean, value);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        //循环遍历
        for (TableBean orderBean : orderBeans) {
            orderBean.setPname(pdBean.getPname());
            context.write(orderBean, NullWritable.get());
        }
    }
}

(4)编写 TableDriver 类

...代码省略

3)总结

缺点:这种方式中,合并的操作是在 Reduce 阶段完成,Reduce 端的处理压力太大,Map 节点的运算负载则很低,资源利用率不高,且在 Reduce 阶段极易产生数据倾斜。

解决方案:Map 端实现数据合并

3.6.3 Map Join

1)使用场景 Map Join 适用于一张表十分小、一张表很大的场景。

2)优点 思考:在 Reduce 端处理过多的表,非常容易产生数据倾斜。怎么办? 在 Map 端缓存多张表,提前处理业务逻辑,这样增加 Map 端业务,减少 Reduce 端数 据的压力,尽可能的减少数据倾斜。

3)具体办法:采用 DistributedCache

(1)在 Mapper 的 setup 阶段,将文件读取到缓存集合中。

(2)在 Driver 驱动类中加载缓存。

//添加缓存文件
job.addCacheFile(new URI("file:///D:/hadoop-test/pd/pd.txt"));
//关闭reducer tasks
job.setNumReduceTasks(0);

3.6.4 Map Join 案例实操

在这里插入图片描述
2)代码实现

(1)创建商品和订单合并后的 TableBean 类

public class TableBean implements Writable {

    private String id;      //订单id
    private String pid;     //商品id
    private int amount;     //商品数量
    private String pname;   //商品名称
    private String flag;    //标记是哪张表

	...省略 get and set

    @Override
    public String toString() {
        return id + "\t" + pname + "\t" + amount;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(id);
        dataOutput.writeUTF(pid);
        dataOutput.writeInt(amount);
        dataOutput.writeUTF(pname);
        dataOutput.writeUTF(flag);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.id = dataInput.readUTF();
        this.pid = dataInput.readUTF();
        this.amount = dataInput.readInt();
        this.pname = dataInput.readUTF();
        this.flag = dataInput.readUTF();
    }
}

(2)编写 TableMapper 类

public class TableMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

    private Map<String, String> map = new HashMap<>();
    private Text outK = new Text();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //获取缓存文件并把文件内容封装到集合
        URI[] cacheFiles = context.getCacheFiles();
        FileSystem fs = FileSystem.get(context.getConfiguration());
        FSDataInputStream fis = fs.open(new Path(cacheFiles[0]));
        //从流中读取数据
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
        String line;
        while (StringUtils.isNotEmpty(line = bufferedReader.readLine())) {
            //切割
            String[] split = line.split("\t");
            map.put(split[0], split[1]);
        }
        //关流
        IOUtils.closeStream(bufferedReader);
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //处理order
        String line = value.toString();
        String[] split = line.split("\t");
        outK.set(split[0] + "\t" + map.get(split[1]) + "\t" + split[2]);
        context.write(outK, NullWritable.get());
    }
}

(3)编写 TableDriver 类

public class TableDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
		
		...核心代码...
		
        //添加缓存文件
        job.addCacheFile(new URI("file:///D:/hadoop-test/pd/pd.txt"));
        //关闭reducer tasks
        job.setNumReduceTasks(0);
    }
}

3.7 数据清洗(ETL)

“ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取 (Extract)、转换(Transform)、加载(Load)至目的端的过程。ETL 一词较常用在数据仓 库,但其对象并不限于数据仓库

在运行核心业务 MapReduce 程序之前,往往要先对数据进行清洗,清理掉不符合用户 要求的数据。清理的过程往往只需要运行 Mapper 程序,不需要运行 Reduce 程序

1)需求

去除日志中字段个数小于等于 11 的日志。

(1)输入数据 web.log

(2)期望输出数据 每行字段长度都大于 11。

2)需求分析

需要在 Map 阶段对输入的数据根据规则进行过滤清洗。

3)实现代码

(1)编写 WebLogMapper 类

public class WebLogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        boolean result=parseLog(line,context);

        if(!result){
            return;
        }
        context.write(value, NullWritable.get());
    }

    private boolean parseLog(String line, Context context) {
        String[] split = line.split(" ");
        return split.length > 11;
    }
}

(2)编写 WebLogDriver 类

//不需要reducer
job.setNumReduceTasks(0);

3.8 MapReduce 开发总结

1)输入数据接口:InputFormat

(1)默认使用的实现类是:TextInputFormat

(2)TextInputFormat 的功能逻辑是:一次读一行文本,然后将该行的起始偏移量作为 key,行内容作为 value 返回。

(3)CombineTextInputFormat 可以把多个小文件合并成一个切片处理,提高处理效率。

2)逻辑处理接口:

Mapper 用户根据业务需求实现其中三个方法:map() setup() cleanup ()

3)Partitioner 分区

(1)有默认实现 HashPartitioner,逻辑是根据 key 的哈希值和 numReduces 来返回一个 分区号;key.hashCode()&Integer.MAXVALUE % numReduces

(2)如果业务上有特别的需求,可以自定义分区。

4)Comparable 排序

(1)当我们用自定义的对象作为 key 来输出时,就必须要实现 WritableComparable 接 口,重写其中的 compareTo()方法。

(2)部分排序:对最终输出的每一个文件进行内部排序。

(3)全排序:对所有数据进行排序,通常只有一个 Reduce。

(4)二次排序:排序的条件有两个。

5)Combiner 合并

Combiner 合并可以提高程序执行效率,减少 IO 传输。但是使用时必须不能影响原有的 业务处理结果。

6)逻辑处理接口:Reducer

用户根据业务需求实现其中三个方法:reduce() setup() cleanup ()

7)输出数据接口:OutputFormat

(1)默认实现类是 TextOutputFormat,功能逻辑是:将每一个 KV 对,向目标文本文件 输出一行。

(2)用户还可以自定义 OutputFormat。

4 Hadoop 数据压缩

4.1 概述

1)压缩的好处和坏处

压缩的优点:以减少磁盘 IO、减少磁盘存储空间。

压缩的缺点:增加 CPU 开销。

2)压缩原则

(1)运算密集型的 Job,少用压缩

(2)IO 密集型的 Job,多用压缩

4.2 MR 支持的压缩编码

1)压缩算法对比介绍

压缩格式Hadoop 自带?算法文件扩展名是否可切片换成压缩格式后,原来的程序是否需要修改
DEFLATEDEFLATE.deflate不需要
GzipDEFLATE.gz不需要
bzip2bzip2.bz2不需要
LZOLZO.lzo需要建索引,还需要指定输入格式
SnappySnappy.snappy不需要

2)压缩性能的比较

压缩算法原始文件大小压缩文件大小压缩速度解压速度
gzip8.3GB1.8GB17.5MB/s58MB/s
bzip28.3GB1.1GB2.4MB/s9.5MB/s
LZO8.3GB2.9GB49.3MB/s74.6MB/s
Snappy250MB/s500MB/s

4.3 压缩方式选择

压缩方式选择时重点考虑:压缩/解压缩速度、压缩率(压缩后存储大小)、压缩后是否 可以支持切片。

4.3.1 Gzip 压缩

优点:压缩率比较高;

缺点:不支持 Split;压缩/解压速度一般;

4.3.2 Bzip2 压缩

优点:压缩率高;支持 Split;

缺点:压缩/解压速度慢。

4.3.3 Lzo 压缩

优点:压缩/解压速度比较快;支持 Split;

缺点:压缩率一般;想支持切片需要额外创建索引。

4.3.4 Snappy 压缩

优点:压缩和解压缩速度快;

缺点:不支持 Split;压缩率一般;

4.3.5 压缩位置选择

压缩可以在 MapReduce 作用的任意阶段启用。
在这里插入图片描述

4.4 压缩参数配置

1)为了支持多种压缩/解压缩算法,Hadoop 引入了编码/解码器

压缩格式对应的编码/解码器
DEFLATEorg.apache.hadoop.io.compress.DefaultCodec
gziporg.apache.hadoop.io.compress.GzipCodec
bzip2org.apache.hadoop.io.compress.BZip2Codec
LZOcom.hadoop.compression.lzo.LzopCodec
Snappyorg.apache.hadoop.io.compress.SnappyCodec

2)要在 Hadoop 中启用压缩,可以配置如下参数

参数默认值阶段建议
io.compression.codecs
(在 core-site.xml 中配置)
无,这个需要在命令行输入 hadoop checknative查看输入压缩Hadoop 使用文件扩展名判断是否支持某种 编解码器
mapreduce.map.output.compress
(在 mapred-site.xml 中 配置)
falsemapper 输出这个参数设为 true 启用压缩
mapreduce.map.output.compress.codec
(在 mapred-site.xml 中配置)
org.apache.hadoop.io.compress.DefaultCodecmapper 输出企业多使用 LZO 或 Snappy 编解码器在此 阶段压缩数据
mapreduce.output.fileoutputformat.compress
(在 mapred-site.xml 中配置)
falsereducer 输出这个参数设为 true 启 用压缩
mapreduce.output.fileoutputformat.compress.codec
(在 mapred-site.xml 中配置)
org.apache.hadoop.io.compress.DefaultCodecreducer 输出使用标准工具或者编 解码器,如 gzip 和 bzip2

4.5 压缩实操案例

4.5.1 Map 输出端采用压缩

即使你的 MapReduce 的输入输出文件都是未压缩的文件,你仍然可以对 Map 任务的中 间结果输出做压缩,因为它要写在硬盘并且通过网络传输到 Reduce 节点,对其压缩可以提 高很多性能,这些工作只要设置两个属性即可,我们来看下代码怎么设置。

1)开启map端输出压缩

// 开启 map 端输出压缩
configuration.setBoolean("mapreduce.map.output.compress", true);
// 设置 map 端输出压缩方式
configuration.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);

2)开启reducer端输出压缩

// 设置 reduce 端输出压缩开启
FileOutputFormat.setCompressOutput(job, true);
// 设置压缩的方式
//FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
//FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
FileOutputFormat.setOutputCompressorClass(job, DefaultCodec.class);

总结

文章主要内容来自尚硅谷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值