Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序。
YARN主要由ResourceManager、NodeManager、ApplicationMaster和Container等组件构成。
(1)作业提交
(2)作业初始化
(3)任务分配
(4)任务运行
(5)进度和状态更新
(6)作业完成
目前,Hadoop作业调度器主要有三种:FIFO、容量(Capacity Scheduler)和公平(Fair Scheduler)。
Apache Hadoop3.1.3默认的资源调度器是Capacity Scheduler。
CDH框架默认调度器是Fair Scheduler。
详见yarn-default.xml
文件
<property>
<description>The class to use as the resource scheduler.description>
<name>yarn.resourcemanager.scheduler.classname>
<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulervalue>
property>
FIFO调度器(First In First Out):单队列,根据提交作业的先后顺序,先来先服务。
Capacity Scheduler是Yahoo开发的多用户调度器。
Fair Schedulere是Facebook开发的多用户调度器。
公平调度器—缺额
有3种资源分配方式:FIFO策略、 Fair 策略、 DRF策略。
(1)、FIFO策略
公平调度器每个队列资源分配策略如果选择FIFO的话,此时公平调度器相当于上面讲过的容量调度器。
(2)、Fair 策略
Fair 策略(默认)是一种基于最大最小公平算法实现的资源多路复用方式,默认情况下,每个队列内部采用该方式分配资源。这意味着,如果一个队列中有两个应用程序同时运行,则每个应用程序可得到1/2的资源;如果三个应用程序同时运行,则每个应用程序可得到1/3的资源。
(2)作业资源分配
需求:有一条队列总资源12个, 有4个job,对资源的需求分别是:
job1->1, job2->2 , job3->6, job4->5
第一次算: 12 / 4 = 3
job1: 分3 --> 多2个
job2: 分3 --> 多1个
job3: 分3 --> 差3个
job4: 分3 --> 差2个
第二次算: 3 / 2 = 1.5
job1: 分1
job2: 分2
job3: 分3 --> 差3个 --> 分1.5 --> 最终: 4.5
job4: 分3 --> 差2个 --> 分1.5 --> 最终: 4.5
第n次算: 一直算到没有空闲资源
需求:有一条队列总资源16,有4个job
对资源的需求分别是:
job1->4 job2->2 job3->10 job4->4
每个job的权重为:
job1->5 job2->8 job3->1 job4->2
第一次算: 16 / (5+8+1+2) = 1
job1: 分5 --> 多1
job2: 分8 --> 多6
job3: 分1 --> 少9
job4: 分2 --> 少2
第二次算: 7 / (1+2) = 7/3
job1: 分4
job2: 分2
job3: 分1 --> 分7/3(2.33) -->少6.67
job4: 分2 --> 分14/3(4.66) -->多2.66
第三次算:2.66/1=2.66
job1: 分4
job2: 分2
job3: 分3.33 --> 分2.66/1 --> 分6
job4: 分4
第n次算: 一直算到没有空闲资源
(3)、DRF策略
DRF(Dominant Resource Fairness),我们之前说的资源,都是单一标准,例如只考虑内存(也是Yarn默认的情况)。但是很多时候我们资源有很多种,例如内存,CPU,网络带宽等,这样我们很难衡量两个应用应该分配的资源比例。
那么在YARN中,我们用DRF来决定如何调度:假设集群一共有100 CPU和10T 内存,而应用A需要(2 CPU, 300GB),应用B需要(6 CPU,100GB)。则两个应用分别需要A(2%CPU, 3%内存)和B(6%CPU, 1%内存)的资源,这就意味着A是内存主导的, B是CPU主导的,针对这种情况,我们可以选择DRF策略对不同应用进行不同资源(CPU和内存)的一个不同比例的限制。
资源配置:
从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。
1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster。
平均每个节点运行10个 / 3台 ≈ 3个任务(4 3 3)
修改yarn-site.xml
配置参数如下
<property>
<description>The class to use as the resource scheduler.description>
<name>yarn.resourcemanager.scheduler.classname>
<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulervalue>
property>
<property>
<description>Number of threads to handle scheduler interface.description>
<name>yarn.resourcemanager.scheduler.client.thread-countname>
<value>8value>
property>
<property>
<description>Flag to determine if logical processors(such as
hyperthreads) should be counted as cores. Only applicable on Linux
when yarn.nodemanager.resource.cpu-vcores is set to -1 and
yarn.nodemanager.resource.detect-hardware-capabilities is true.
description>
<name>yarn.nodemanager.resource.count-logical-processors-as-coresname>
<value>falsevalue>
property>
<property>
<description>Enable auto-detection of node capabilities such as
memory and CPU.
description>
<name>yarn.nodemanager.resource.detect-hardware-capabilitiesname>
<value>falsevalue>
property>
<property>
<description>Multiplier to determine how to convert phyiscal cores to vcores. This value is used if
yarn.nodemanager.resource.cpu-vcores is set to -1(which implies auto-calculate vcores) and
yarn.nodemanager.resource.detect-hardware-capabilities is set to true. The number of vcores will be calculated as number of CPUs * multiplier.
description>
<name>yarn.nodemanager.resource.pcores-vcores-multipliername>
<value>1.0value>
property>
<property>
<description>Amount of physical memory, in MB, that can be allocated
for containers. If set to -1 and
yarn.nodemanager.resource.detect-hardware-capabilities is true, it is
automatically calculated(in case of Windows and Linux).
In other cases, the default is 8192MB.
description>
<name>yarn.nodemanager.resource.memory-mbname>
<value>4096value>
property>
<property>
<description>Number of vcores that can be allocated
for containers. This is used by the RM scheduler when allocating
resources for containers. This is not used to limit the number of
CPUs used by YARN containers. If it is set to -1 and
yarn.nodemanager.resource.detect-hardware-capabilities is true, it is
automatically determined from the hardware in case of Windows and Linux.
In other cases, number of vcores is 8 by default.description>
<name>yarn.nodemanager.resource.cpu-vcoresname>
<value>4value>
property>
<property>
<description>The minimum allocation for every container request at the RM in MBs. Memory requests lower than this will be set to the value of this property. Additionally, a node manager that is configured to have less memory than this value will be shut down by the resource manager.
description>
<name>yarn.scheduler.minimum-allocation-mbname>
<value>1024value>
property>
<property>
<description>The maximum allocation for every container request at the RM in MBs. Memory requests higher than this will throw an InvalidResourceRequestException.
description>
<name>yarn.scheduler.maximum-allocation-mbname>
<value>2048value>
property>
<property>
<description>The minimum allocation for every container request at the RM in terms of virtual CPU cores. Requests lower than this will be set to the value of this property. Additionally, a node manager that is configured to have fewer virtual cores than this value will be shut down by the resource manager.
description>
<name>yarn.scheduler.minimum-allocation-vcoresname>
<value>1value>
property>
<property>
<description>The maximum allocation for every container request at the RM in terms of virtual CPU cores. Requests higher than this will throw an
InvalidResourceRequestException.description>
<name>yarn.scheduler.maximum-allocation-vcoresname>
<value>2value>
property>
<property>
<description>Whether virtual memory limits will be enforced for
containers.description>
<name>yarn.nodemanager.vmem-check-enabledname>
<value>falsevalue>
property>
<property>
<description>Ratio between virtual memory to physical memory when setting memory limits for containers. Container allocations are expressed in terms of physical memory, and virtual memory usage is allowed to exceed this allocation by this ratio.
description>
<name>yarn.nodemanager.vmem-pmem-rationame>
<value>2.1value>
property>
重启Yarn集群
./sbin/stop-yarn.sh
./sbin/start-yarn.sh
登录页面查看资源修改:http://hadoop102:8088/cluster
关闭虚拟内存检查
配置参数:
default
队列:占总内存的40%,最大资源容量占总资源60%。
hive
队列:占总内存的60%,最大资源容量占总资源80%。
修改capacity-scheduler.xml
配置
<property>
<name>yarn.scheduler.capacity.root.queuesname>
<value>default,hivevalue>
<description>
The queues at the this level (root is the root queue).
description>
property>
<property>
<name>yarn.scheduler.capacity.root.default.capacityname>
<value>40value>
property>
<property>
<name>yarn.scheduler.capacity.root.default.maximum-capacityname>
<value>60value>
property>
添加capacity-scheduler.xml
配置
<property>
<name>yarn.scheduler.capacity.root.hive.capacityname>
<value>60value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.user-limit-factorname>
<value>1value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.maximum-capacityname>
<value>80value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.statename>
<value>RUNNINGvalue>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.acl_submit_applicationsname>
<value>*value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.acl_administer_queuename>
<value>*value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.acl_application_max_priorityname>
<value>*value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.maximum-application-lifetimename>
<value>-1value>
property>
<property>
<name>yarn.scheduler.capacity.root.hive.default-application-lifetimename>
<value>-1value>
property>
分发修改后配置文件,或者修改ResourceManger
所在节点配置
重启Yarn集群或者刷新配置
yarn rmadmin -refreshQueues
登录页面查看队列更新:http://hadoop102:8088/cluster/scheduler
package com.example.demo.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.output.FileOutputFormat;
public class WCDriver2 {
public static void main(String[] args) throws Exception {
System.out.println(args[0]);
System.out.println(args[1]);
//1.创建Job实例
Configuration conf = new Configuration();//可以设置参数
conf.set("mapreduce.job.queuename", "hive");
Job job = Job.getInstance(conf);
//2.给Job赋值
//2.1关联本程序的jar---如果是本地运行不用设置。如果是在集群上运行(打jar包放在集群上)一定要设置
job.setJarByClass(WCDriver2.class);
//2.2设置Mapper和Reducer类
job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class);
//2.3设置Mapper输出的Key,value的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//2.4设置最终输出的key,value的类型(在这是Reducer输出的key,value的类型)
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//2.5设置输入和输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
//注意:输出的目录必须不存在
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//3.提交Job
boolean b = job.waitForCompletion(true);
System.out.println("=======" + b);
}
}
pom文件打包方式
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoname>
<description>demodescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-clientartifactId>
<version>3.1.3version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.30version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.6.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
登录服务器执行脚本
demo-0.0.1.jar
:运行的jar包,服务器所在全路径com.example.demo.wordcount.WCDriver2
:全类名运行jar包中的哪个类/input
:数据的输入路径(HDFS)/output
:数据的输出路径(HDFS)hadoop jar demo-0.0.1.jar com.example.demo.wordcount.WCDriver2 /input /output