您的位置:首页 > 娱乐 > 八卦 > 网络专业毕业设计_官方电商平台_江门seo_google chrome 网络浏览器

网络专业毕业设计_官方电商平台_江门seo_google chrome 网络浏览器

2025/3/6 17:43:41 来源:https://blog.csdn.net/pilot_speed/article/details/146004965  浏览:    关键词:网络专业毕业设计_官方电商平台_江门seo_google chrome 网络浏览器
网络专业毕业设计_官方电商平台_江门seo_google chrome 网络浏览器

Hadoop3.x从入门到放弃,第六章:Yarn基本概念与操作

Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,
而MapReduce等运算程序则相当于运行于操作系统之上的应用程序。

一、Yarn基础架构

YARN主要由ResourceManager、NodeManager、ApplicationMaster和Container等组件构成

在这里插入图片描述

二、Yarn工作机制

在这里插入图片描述

(1)MR程序提交到客户端所在的节点。
(2)YarnRunner向ResourceManager申请一个Application。
(3)RM将该应用程序的资源路径返回给YarnRunner。
(4)该程序将运行所需资源提交到HDFS上。
(5)程序资源提交完毕后,申请运行mrAppMaster。
(6)RM将用户的请求初始化成一个Task。
(7)其中一个NodeManager领取到Task任务。
(8)该NodeManager创建容器Container,并产生MRAppmaster。
(9)Container从HDFS上拷贝资源到本地。
(10)MRAppmaster向RM 申请运行MapTask资源。
(11)RM将运行MapTask任务分配给另外两个NodeManager,另两个NodeManager分别领取任务并创建容器。
(12)MR向两个接收到任务的NodeManager发送程序启动脚本,这两个NodeManager分别启动MapTask,MapTask对数据分区排序。
(13)MrAppMaster等待所有MapTask运行完毕后,向RM申请容器,运行ReduceTask。
(14)ReduceTask向MapTask获取相应分区的数据。
(15)程序运行完毕后,MR会向RM申请注销自己。

三、Yarn作业提交过程

在这里插入图片描述

1、作业提交:> Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业> Client向RM申请一个作业id> RM给Client返回该job资源的提交路径和作业id。> Client提交jar包、切片信息和配置文件到指定的资源提交路径。> Client提交完资源后,向RM申请运行MrAppMaster。2、作业初始化:> 当RM收到Client的请求后,将该job添加到容量调度器中。> 某一个空闲的NM领取到该Job。> 该NM创建Container,并产生MRAppmaster。> 下载Client提交的资源到本地。3、任务分配:> MrAppMaster向RM申请运行多个MapTask任务资源。> RM将运行MapTask任务分配给另外两个NodeManager,另两个NodeManager分别领取任务并创建容器。4、任务运行:> MR向两个接收到任务的NodeManager发送程序启动脚本,这两个NodeManager分别启动MapTask,MapTask对数据分区排序。> MrAppMaster等待所有MapTask运行完毕后,向RM申请容器,运行ReduceTask。> ReduceTask向MapTask获取相应分区的数据。> 程序运行完毕后,MR会向RM申请注销自己。5、进度和状态更新:> YARN中的任务将其进度和状态(包括counter)返回给应用管理器, 客户端每秒(通过mapreduce.client.progressmonitor.pollinterval设置)向应用管理器请求进度更新, 展示给用户。6、作业完成:> 除了向应用管理器请求作业进度外, 客户端每5秒都会通过调用waitForCompletion()来检查作业是否完成。时间间隔可以通过mapreduce.client.completion.pollinterval来设置。作业完成之后, 应用管理器和Container会清理工作状态。作业的信息会被作业历史服务器存储以备之后用户核查.

四、Yarn调度器和调度算法

目前,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.class</name>
<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value>
</property>

1、FIFO

FIFO调度器(First In First Out):单队列,根据提交作业的先后顺序,先来先服务。优点:简单易懂;
缺点:不支持多队列,生产环境很少使用;

在这里插入图片描述

2、容量调度器(Capacity Scheduler)

Capacity Scheduler是Yahoo开发的多用户调度器。> 多队列:每个队列可配置一定的资源量,每个队列采用FIFO调度略。> 容量保证:管理员可为每个队列设置资源最低保证和资源使用上限> 灵活性:如果一个队列中的资源有剩余,可以暂时共享给那些需要资源的队列,而一旦该队列有新的应用程序提交,则其他队列借调的资源会归还给该队列。> 多租户:支持多用户共享集群和多应用程序同时运行。为了防止同一个用户的作业独占队列中的资源,该调度器会对同一用户提交的作业所占资源量进行限定。

在这里插入图片描述

【容量调度器分配算法】

在这里插入图片描述

3、公平调度器(Fair Scheduler)

Fair Schedulere是Facebook开发的多用户调度器。· 与容量调度器相同点> 多队列:支持多队列多作业> 容量保证:管理员可为每个队列设置资源最低保证和资源使用上线> 灵活性:如果一个队列中的资源有剩余,可以暂时共享给那些需要资源的队列,而一旦该队列有新的应用程序提交,则其他队列借调的资源会归还给该队列。> 多租户:支持多用户共享集群和多应用程序同时运行;为了防止同一个用户的作业独占队列中的资源,该调度器会对同一用户提交的作业所占资源量进行限定。· 与容量调度器不同点> 核心调度策略不同容量调度器:优先选择资源利用率低的队列公平调度器:优先选择对资源的缺额比例大的> 每个队列可以单独设置资源分配方式容量调度器:FIFO、 DRF公平调度器:FIFO、FAIR、DRF

在这里插入图片描述

【什么是缺额】

在这里插入图片描述

公平调度器设计目标是:在时间尺度上,所有作业获得公平的资源。某一时刻一个作业应获资源和实际获取资源的差距叫“缺额”【调度器会优先为缺额大的作业分配资源】

【公平调度器分配算法】

在这里插入图片描述

在这里插入图片描述

【DRF策略】

DRF(Dominant Resource Farness),我们之前说的资源,都是单一标准,
例如只考虑内存(也是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和内存)的一个不同比例的限制。

五、Yarn常用命令

1、列出所有任务信息 
通过 :http://192.168.73.103:8088/cluster
或
yarn application -list  (默认只展示正在执行的任务)
2、查询具体某个状态的任务
yarn application -list -appStates RUNNING【所有状态:ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED】
3、kill掉正在执行的任务yarn application -kill  任务ID
4、查看日志yarn logs -applicationId 任务ID
5、查看任务的某个容器里的日志yarn logs -applicationId 任务ID  -containerId 容器ID
6、查看尝试运行的任务yarn applicaitionattempt -list 任务ID
7、查看尝试运行任务的状态yarn applicationattempt -status 【applicationattemptId】
8、查看容器信息yarn container -list 【任务ID】【容器只有在任务运行的时候才能看到,因为任务运行完毕后 容器会被回收掉】
9、查看节点状态yarn node -list -all
10、加载队列配置yarn rmadmin -refreshQueues
11、查看队列yarn queue -status <QueueName>yarn queue -status default 【查看默认的配置】
12、设置任务的超时时间yarn application -appId  任务ID -updateLifetime 超时时间超时时间 不能超过 yarn.scheduler.capacity.root.default.maximum-application-lifetime  最大生命周期,否则无效
13、设置任务优先级yarn application -appId 任务ID  -updatePriority 优先级

六、生产环境核心参数配置

1. 调度器选择
这里选用默认调度器(容量调度器)。<property><name>yarn.resourcemanager.scheduler.class</name><value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value>
</property>2. ResourceManager 调度器处理线程数量设置
ResourceManager 处理调度器请求的线程数量,默认 50;如果提交的任务数大于 50,可以增加该值,但是不能超过总线程数量。在当前测试环境中,我们共配置了 3 台服务器,4 核 CPU,共有 12 线程,但时不能直接设置为 12,而应该设置为 8(三分之二),
剩余资源保障其它应用程序的正常运行。<property><name>yarn.resourcemanager.scheduler.client.thread-count</name><value>8</value>
</property>3. 是否启用节点功能的自动检测设置
顾名思义,就是自动检测当前系统配置,自动进行资源配置,默认为 false。在该系统内没有其它应用程序运行的情况下,可以开启该参数。<property><name>yarn.nodemanager.resource.detect-hardware-capabilities</name><value>false</value>
</property>4. 是否将逻辑处理器当作物理核心处理器
指定逻辑处理器(虚拟核数)是否应该被视为独立的核心资源,默认为 false。物理核心处理器可以给集群带来更强的处理性能,而逻辑处理器能够提供更多的并发。<property><name>yarn.nodemanager.resource.count-logical-processors-as-cores</name><value>false</value>
</property>5. 设置物理核心到虚拟核心的转换乘数
默认情况下,乘数为 1.0,这意味着一个物理核心会被视为一个虚拟核心。如果希望将一个物理核心映射为多个虚拟核心,可以将这个值调整为大于 1 的值。可以根据应用程序需求、资源利用率以及性能测试结果来决定是否调整这个乘数。<property><name>yarn.nodemanager.resource.pcores-vcores-multiplier</name><value>1.0</value>
</property>6. 设置 NodeManager 使用的内存量
用于指定每个 NodeManager 节点可以使用的内存量,以兆字节(MB)为单位,默认 8G,修改为单台机器对应的内存量。<property><name>yarn.nodemanager.resource.memory-mb</name><value>4096</value>
</property>7. 设置 NodeManager 节点虚拟核心数量
用于指定每个 NodeManager 节点可以使用的虚拟核心数量(vCores),默认为 8 个,修改为单台机器对应的物理核心数量。<property><name>yarn.nodemanager.resource.cpu-vcores</name><value>4</value>
</property>8. 设置每个容器的最小与最大内存
用于设置每个容器(task或应用程序的运行实例)所能够分配到的最小与最大内存量。最小内存默认为 1024 MB;最大内存默认为 8192 MB,推荐设置为系统最大内存的一半;<!-- 容器最小内存-->
<property><name>yarn.scheduler.minimum-allocation-mb</name><value>1024</value>
</property><!-- 容器最大内存-->
<property><name>yarn.scheduler.maximum-allocation-mb</name><value>2048</value>
</property>9. 设置每个容器的最小与最大虚拟CPU核心
设置每个容器(任务或应用程序的运行实例)所能够分配到的最小与最大 CPU 虚拟核心数(vCores)。最小虚拟核心数默认为 1 个;最大虚拟核心数默认为 4 个,推荐设置为系统最大物理核心的一半;<!-- 容器最小虚拟核心数 -->
<property><name>yarn.scheduler.minimum-allocation-vcores</name><value>1</value>
</property><!-- 容器最大虚拟核心数 -->
<property><name>yarn.scheduler.maximum-allocation-vcores</name><value>2</value>
</property>10. 是否开启虚拟内存检查
用于指定是否启用虚拟内存检查来限制每个容器使用的虚拟内存量。默认为 true,NodeManager 将会在分配容器资源时,考虑每个容器使用的虚拟内存量。如果一个容器的虚拟内存使用量超过了预先配置的限制,NodeManager 会采取措施来处理这种情况,通常是终止或杀死该容器。在这里我调整为 false。<property><name>yarn.nodemanager.vmem-check-enabled</name><value>false</value>
</property>11. 设置虚拟内存与物理内存比例
用于设置虚拟内存与物理内存之间的比率,默认为 2.1 倍。这个参数的目的是限制应用程序可以使用的虚拟内存量,以避免某个应用程序无限制地占用虚拟内存资源,导致其他任务和应用程序受影响。<property><name>yarn.nodemanager.vmem-pmem-ratio</name><value>2.1</value>
</property>

在这里插入图片描述

七、Yarn案例实操

1、核心参数配置

<!-- 选择调度器,默认容量 -->
<property><description>The class to use as the resource scheduler.</description><name>yarn.resourcemanager.scheduler.class</name><value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value>
</property><!-- ResourceManager处理调度器请求的线程数量,默认50;如果提交的任务数大于50,可以增加该值,但是不能超过3台 * 4线程 = 12线程(去除其他应用程序实际不能超过8) -->
<property><description>Number of threads to handle scheduler interface.</description><name>yarn.resourcemanager.scheduler.client.thread-count</name><value>8</value>
</property><!-- 是否让yarn自动检测硬件进行配置,默认是false,如果该节点有很多其他应用程序,建议手动配置。如果该节点没有其他应用程序,可以采用自动 -->
<property><description>Enable auto-detection of node capabilities such asmemory and CPU.</description><name>yarn.nodemanager.resource.detect-hardware-capabilities</name><value>false</value>
</property><!-- 是否将虚拟核数当作CPU核数,默认是false,采用物理CPU核数 -->
<property><description>Flag to determine if logical processors(such ashyperthreads) should be counted as cores. Only applicable on Linuxwhen yarn.nodemanager.resource.cpu-vcores is set to -1 andyarn.nodemanager.resource.detect-hardware-capabilities is true.</description><name>yarn.nodemanager.resource.count-logical-processors-as-cores</name><value>false</value>
</property><!-- 虚拟核数和物理核数乘数,默认是1.0 -->
<property><description>Multiplier to determine how to convert phyiscal cores tovcores. This value is used if yarn.nodemanager.resource.cpu-vcoresis set to -1(which implies auto-calculate vcores) andyarn.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-multiplier</name><value>1.0</value>
</property><!-- NodeManager使用内存数,默认8G,修改为4G内存 -->
<property><description>Amount of physical memory, in MB, that can be allocated for containers. If set to -1 andyarn.nodemanager.resource.detect-hardware-capabilities is true, it isautomatically calculated(in case of Windows and Linux).In other cases, the default is 8192MB.</description><name>yarn.nodemanager.resource.memory-mb</name><value>4096</value>
</property><!-- nodemanager的CPU核数,不按照硬件环境自动设定时默认是8个,修改为4个 -->
<property><description>Number of vcores that can be allocatedfor containers. This is used by the RM scheduler when allocatingresources for containers. This is not used to limit the number ofCPUs used by YARN containers. If it is set to -1 andyarn.nodemanager.resource.detect-hardware-capabilities is true, it isautomatically 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-vcores</name><value>4</value>
</property><!-- 容器最小内存,默认1G -->
<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-mb</name><value>1024</value>
</property><!-- 容器最大内存,默认8G,修改为2G -->
<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-mb</name><value>2048</value>
</property><!-- 容器最小CPU核数,默认1个 -->
<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-vcores</name><value>1</value>
</property><!-- 容器最大CPU核数,默认4个,修改为2个 -->
<property><description>The maximum allocation for every container request at the RM	in terms of virtual CPU cores. Requests higher than this will throw anInvalidResourceRequestException.</description><name>yarn.scheduler.maximum-allocation-vcores</name><value>2</value>
</property><!-- 虚拟内存检查,默认打开,修改为关闭 -->
<property><description>Whether virtual memory limits will be enforced forcontainers.</description><name>yarn.nodemanager.vmem-check-enabled</name><value>false</value>
</property><!-- 虚拟内存和物理内存设置比例,默认2.1 -->
<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-ratio</name><value>2.1</value>
</property>

2、容量调度器多队列案例

容量调度器默认只有一个default调度器企业一般会按照业务模块创建队列:登录注册、购物车、下单 等服务商也会按照 公司进行创建队列:如政府医保局,大数据提供商会给使用到服务的每个公司 提供一个队列,有的跑数据适配、有的跑风控等
好处:
1、防止某个业务出错,将服务资源一次全部耗尽。导致集群瘫痪。
2、可以实现任务的降级使用。如双十一为了保证下单,将下单、购物车等业务资源提高,注册等资源降低。
2.1、需求
##需求:需求1:default队列占总内存的40%,最大资源容量占总资源60%,hive队列占总内存的60%,最大资源容量占总资源80%。需求2:配置队列优先级
2.1.1、设置队列
##配置  etc/hadoop/capacity-scheduler.xml   
【里面已经默认配置了default队列的信息了,如果想增加一个hive队列,则增加如下】<!--指定多队列,增加hive队列--><property><name>yarn.scheduler.capacity.root.queues</name><value>default,hive</value><description>The queues at the this level (root is the root queue).</description></property><!-- 设置队列资源额定容量为40%,默认100% --><property><name>yarn.scheduler.capacity.root.default.capacity</name><value>40</value><description>Default queue target capacity.</description></property><property><name>yarn.scheduler.capacity.root.hive.capacity</name><value>60</value><description>Default queue target capacity.</description></property><!--允许单个用户最多可获取的队列资源的倍数。默认值为1--><property><name>yarn.scheduler.capacity.root.default.user-limit-factor</name><value>1</value><description>Default queue user limit a percentage from 0.0 to 1.0.</description></property><property><name>yarn.scheduler.capacity.root.hive.user-limit-factor</name><value>1</value><description>Default queue user limit a percentage from 0.0 to 1.0.</description></property><!--队列资源最大容量为60%,默认100% --><property><name>yarn.scheduler.capacity.root.default.maximum-capacity</name><value>60</value><description>The maximum capacity of the default queue. </description></property><property><name>yarn.scheduler.capacity.root.hive.maximum-capacity</name><value>80</value><description>The maximum capacity of the default queue. </description></property><!--队列的运行状态:RUNNING/STOPPED--><property><name>yarn.scheduler.capacity.root.default.state</name><value>RUNNING</value><description>The state of the default queue. State can be one of RUNNING or STOPPED.</description></property><property><name>yarn.scheduler.capacity.root.hive.state</name><value>RUNNING</value><description>The state of the default queue. State can be one of RUNNING or STOPPED.</description></property><!-- 哪些用户有权向队列提交作业 --><property><name>yarn.scheduler.capacity.root.default.acl_submit_applications</name><value>*</value><description>The ACL of who can submit jobs to the default queue.</description></property><property><name>yarn.scheduler.capacity.root.hive.acl_submit_applications</name><value>*</value><description>The ACL of who can submit jobs to the default queue.</description></property><!-- 哪些用户有权操作队列,管理员权限(查看/杀死) --><property><name>yarn.scheduler.capacity.root.default.acl_administer_queue</name><value>*</value><description>The ACL of who can administer jobs on the default queue.</description></property><property><name>yarn.scheduler.capacity.root.hive.acl_administer_queue</name><value>*</value><description>The ACL of who can administer jobs on the default queue.</description></property><!-- 哪些用户有权配置提交任务优先级 --><property><name>yarn.scheduler.capacity.root.default.acl_application_max_priority</name><value>*</value><description>The ACL of who can submit applications with configured priority.For e.g, [user={name} group={name} max_priority={priority} default_priority={priority}]</description></property><property><name>yarn.scheduler.capacity.root.hive.acl_application_max_priority</name><value>*</value><description>The ACL of who can submit applications with configured priority.For e.g, [user={name} group={name} max_priority={priority} default_priority={priority}]</description></property><!--任务最大的生命周期--><!-- 如果执行命令 yarn application -appId  任务ID -updateLifetime 超时时间,超市时间超过了该配置,那么 这个命令无效 --><property><name>yarn.scheduler.capacity.root.default.maximum-application-lifetime</name><value>-1</value><description>Maximum lifetime of an application which is submitted to a queuein seconds. Any value less than or equal to zero will be considered asdisabled.This will be a hard time limit for all applications in thisqueue. If positive value is configured then any application submittedto this queue will be killed after exceeds the configured lifetime.User can also specify lifetime per application basis inapplication submission context. But user lifetime will beoverridden if it exceeds queue maximum lifetime. It is point-in-timeconfiguration.Note : Configuring too low value will result in killing applicationsooner. This feature is applicable only for leaf queue.</description></property><property><name>yarn.scheduler.capacity.root.hive.maximum-application-lifetime</name><value>-1</value><description>Maximum lifetime of an application which is submitted to a queuein seconds. Any value less than or equal to zero will be considered asdisabled.This will be a hard time limit for all applications in thisqueue. If positive value is configured then any application submittedto this queue will be killed after exceeds the configured lifetime.User can also specify lifetime per application basis inapplication submission context. But user lifetime will beoverridden if it exceeds queue maximum lifetime. It is point-in-timeconfiguration.Note : Configuring too low value will result in killing applicationsooner. This feature is applicable only for leaf queue.</description></property><!--任务默认的生命周期--><property><name>yarn.scheduler.capacity.root.default.default-application-lifetime</name><value>-1</value><description>Default lifetime of an application which is submitted to a queuein seconds. Any value less than or equal to zero will be considered asdisabled.If the user has not submitted application with lifetime value then thisvalue will be taken. It is point-in-time configuration.Note : Default lifetime can't exceed maximum lifetime. This feature isapplicable only for leaf queue.</description></property><property><name>yarn.scheduler.capacity.root.hive.default-application-lifetime</name><value>-1</value><description>Default lifetime of an application which is submitted to a queuein seconds. Any value less than or equal to zero will be considered asdisabled.If the user has not submitted application with lifetime value then thisvalue will be taken. It is point-in-time configuration.Note : Default lifetime can't exceed maximum lifetime. This feature isapplicable only for leaf queue.</description></property>
重启Yarn或者执行   【 yarn rmadmin -refreshQueues 】  刷新队列,就可以看到两条队列:##指定队列执行任务hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar -D mapreduce.job.queuename=hive /input /output
【-D 表示运行时改变参数值】
或
Configuration conf = new Configuration();
conf.set("mapreduce.job.queuename","hive");
Job job = Job.getInstance(conf);

在这里插入图片描述

2.1.2、设置优先级
容量调度器,支持任务优先级的配置,在资源紧张时,优先级高的任务将优先获取资源。
默认情况,Yarn将所有任务的优先级限制为0,若想使用任务的优先级功能,须开放该限制。
##修改配置 yarn-site.xml<property><name>yarn.cluster.max-application-priority</name><value>5</value>
</property>
##分发并重启
xsync yarn-site.xmlsbin/stop-yarn.sh
sbin/start-yarn.sh
##测试 计算PI执行  下面任务 两次
(2000000表示PI的位数)
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi 5 2000000  执行下面 任务 一次
提高优先级
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi  -D mapreduce.job.priority=5 5 2000000##结果,第一个任务执行完后,就会去执行  优先级高的任务,上面先提交的任务排队等待

3、公平调度器案例

##需求:
创建两个队列,分别是test和atguigu(以用户所属组命名)。期望实现以下效果:若用户提交任务时指定队列,则任务提交到指定队列运行;
若未指定队列,test用户提交的任务到root.group.test队列运行,atguigu提交的任务到root.group.atguigu队列运行(注:group为用户所属组)##解决方案:
公平调度器的配置涉及到两个文件,一个是yarn-site.xml,另一个是公平调度器队列分配文件fair-scheduler.xml(文件名可自定义)##参考文件:
> 配置文件参考资料
https://hadoop.apache.org/docs/r3.1.3/hadoop-yarn/hadoop-yarn-site/FairScheduler.html
> 任务队列放置规则参考资料
https://blog.cloudera.com/untangling-apache-hadoop-yarn-part-4-fair-scheduler-queue-basics/
##yarn-site.xml<!--默认是容量调度器-->
<property><name>yarn.resourcemanager.scheduler.class</name><value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler</value><description>配置使用公平调度器</description>
</property><!--默认是容量调度器,加载的是capacity-scheduler.xml配置文件-->
<property><name>yarn.scheduler.fair.allocation.file</name><value>/opt/module/hadoop-3.1.3/etc/hadoop/fair-scheduler.xml</value><description>指明公平调度器队列分配配置文件</description>
</property><!--公平调度器如果某个队列资源不够,另一个队列资源充足的情况下 会借用另一个队列的资源,这里禁止了-->
<property><name>yarn.scheduler.fair.preemption</name><value>false</value><description>禁止队列间资源抢占</description>
</property>
##fair-scheduler.xml<?xml version="1.0"?>
<allocations><!-- 单个队列中Application Master占用资源的最大比例,取值0-1 ,企业一般配置0.1 (128G内存*01)--><queueMaxAMShareDefault>0.5</queueMaxAMShareDefault><!-- 单个队列最大资源的默认值 test atguigu default --><queueMaxResourcesDefault>4096mb,4vcores</queueMaxResourcesDefault><!-- 增加一个队列test --><queue name="test"><!-- 队列最小资源 --><minResources>2048mb,2vcores</minResources><!-- 队列最大资源 --><maxResources>4096mb,4vcores</maxResources><!-- 队列中最多同时运行的应用数,默认50,根据线程数配置 --><maxRunningApps>4</maxRunningApps><!-- 队列中Application Master占用资源的最大比例 --><maxAMShare>0.5</maxAMShare><!-- 该队列资源权重,默认值为1.0 --><weight>1.0</weight><!-- 队列内部的资源分配策略 --><schedulingPolicy>fair</schedulingPolicy></queue><!-- 增加一个队列atguigu --><queue name="atguigu" type="parent"><!-- 队列最小资源 --><minResources>2048mb,2vcores</minResources><!-- 队列最大资源 --><maxResources>4096mb,4vcores</maxResources><!-- 队列中最多同时运行的应用数,默认50,根据线程数配置 --><maxRunningApps>4</maxRunningApps><!-- 队列中Application Master占用资源的最大比例 --><maxAMShare>0.5</maxAMShare><!-- 该队列资源权重,默认值为1.0 --><weight>1.0</weight><!-- 队列内部的资源分配策略 --><schedulingPolicy>fair</schedulingPolicy></queue><!-- 任务队列分配策略,可配置多层规则,从第一个规则开始匹配,直到匹配成功 --><queuePlacementPolicy><!-- 提交任务时指定队列,如未指定提交队列,则继续匹配下一个规则; false表示:如果指定队列不存在,不允许自动创建--><rule name="specified" create="false"/><!-- 提交到root.group.username队列,若root.group不存在,不允许自动创建;若root.group.user不存在,允许自动创建 --><rule name="nestedUserQueue" create="true"><rule name="primaryGroup" create="false"/></rule><!-- 最后一个规则必须为reject或者default。Reject表示拒绝创建提交失败,default表示把任务提交到default队列 --><rule name="reject" /></queuePlacementPolicy>
</allocations>
##分发配置
xsync yarn-site.xml  fair-scheduler.xml##重启yarn
sbin/stop-yarn.sh
sbin/start-yarn.sh
##测试
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi -Dmapreduce.job.queuename=root.test 1 1hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi 1 1

4、Tool接口案例

public class WordCount implements Tool {private Configuration conf;@Overridepublic int run(String[] args) throws Exception {Job job = Job.getInstance(conf);job.setJarByClass(WordCountDriver.class);job.setMapperClass(WordCountMapper.class);job.setReducerClass(WordCountReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));return job.waitForCompletion(true) ? 0 : 1;}@Overridepublic void setConf(Configuration conf) {this.conf = conf;}@Overridepublic Configuration getConf() {return conf;}public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {private Text outK = new Text();private IntWritable outV = new IntWritable(1);@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String line = value.toString();String[] words = line.split(" ");for (String word : words) {outK.set(word);context.write(outK, outV);}}}public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private IntWritable outV = new IntWritable();@Overrideprotected 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);}}
}
public class WordCountDriver {private static Tool tool;public static void main(String[] args) throws Exception {// 1. 创建配置文件Configuration conf = new Configuration();// 2. 判断是否有tool接口switch (args[0]){case "wordcount":tool = new WordCount();break;default:throw new RuntimeException(" No such tool: "+ args[0] );}// 3. 用Tool执行程序// Arrays.copyOfRange 将老数组的元素放到新数组里面int run = ToolRunner.run(conf, tool, Arrays.copyOfRange(args, 1, args.length));System.exit(run);}
}
##测试:yarn jar YarnDemo.jar com.atguigu.yarn.WordCountDriver wordcount /input /outputyarn jar YarnDemo.jar com.atguigu.yarn.WordCountDriver wordcount -Dmapreduce.job.queuename=root.test /input /output1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com