博客 大数据学习——YARN

大数据学习——YARN

   数栈君   发表于 2024-12-03 17:08  368  0

1 yarn资源调度器

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

1.1 Yarn基础架构

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

(1) ResourceManager

负责处理客户端请求,调度全局资源以及监控NodeManager和ApplicationMaster

(2)NodeManager

每个具体节点的管理者,监控节点内的容器生命周期和资源,配合ApplicationMaster工作

(3)ApplicationMaster

用户提交一个应用程序时启动。ApplicationMaster 负责协调来自 ResourceManager 的资源,负责任务的监控与容错,根据应用的运行状态和资源使用情况动态计算资源需求

(4)Container

YARN 中的资源抽象,封装了某个节点上的多维度资源,如内存、CPU、磁盘、网络等。

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/2377f0111a8249f0784463b0062ce8f8..png

1.2 Yarn工作机制

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/f8b69d03865ea4ac3501daa10e262ea9..png

 1.3HDFS,YARN和MapReduce处理作业提交全流程

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/3bc1762f9c0eaa402b7f43c6a9bf5bdd..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/66b232981efe84be59e5bf854f4036de..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/cca6030eb2630f2c309c8ab08e37aa33..png

(1)作业提交

第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。

第2步:Client向RM申请一个作业id。

第3步:RM给Client返回该job资源的提交路径和作业id。

第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。

第5步:Client提交完资源后,向RM申请运行MrAppMaster。

(2)作业初始化

第6步:当RM收到Client的请求后,将该job添加到容量调度器中。

第7步:某一个空闲的NM领取到该Job。

第8步:该NM创建Container,并产生MRAppmaster。

第9步:下载Client提交的资源到本地。

(3)任务分配

第10步:MrAppMaster向RM申请运行多个MapTask任务资源。

第11步:RM将运行MapTask任务分配给另外两个NodeManager,另两个NodeManager分别领取任务并创建容器。

(4)任务运行

第12步:MR向两个接收到任务的NodeManager发送程序启动脚本,这两个NodeManager分别启动MapTask,MapTask对数据分区排序。

第13步:MrAppMaster等待所有MapTask运行完毕后,向RM申请容器,运行ReduceTask。

第14步:ReduceTask向MapTask获取相应分区的数据。

第15步:程序运行完毕后,MR会向RM申请注销自己。

(5)进度和状态更新

YARN中的任务将其进度和状态(包括counter)返回给应用管理器, 客户端每秒向应用管理器请求进度更新, 展示给用户。

(6)作业完成

除了向应用管理器请求作业进度外, 客户端每5秒都会通过调用waitForCompletion()来检查作业是否完成。作业完成之后, 应用管理器和Container会清理工作状态。作业的信息会被作业历史服务器存储以备之后用户核查。

1.4 Yarn调度器和调度算法
Apache Hadoop3.1.3默认的资源调度器是Capacity Scheduler。在yarn-default.xml中配置。

1.4.1 先进先出调度器(FIFO)
FIFO调度器(First In First Out):单队列,根据提交作业的先后顺序,先来先服务。

优点:简单易懂;

缺点:不支持多队列,生产环境很少使用;

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/bb5dd1bc25f40483fe419ee2af7ceed2..png

1.4.2 容量调度器(Capacity Scheduler)

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/ee7ee5edc319fd80dcbf8e7e7cd1c9c6..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/ea8a0ad44d15d9155618448e0c6e62a2..png
http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/ea8a0ad44d15d9155618448e0c6e62a2..png

1.4.3 公平调度器(Fair Scheduler)

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/8ec033995b62b87a6a65018e569319ce..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/37b0fbd94105a27d75442a05a4cddede..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/7fc0916eddd7313c62b2f032e9047223..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/912311b890ae1ebd071e8132ff32316a..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/c5524ad1b5501bc91d068b0b19c8a516..png

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/f351816678834a41e22d4c69136bcb7e..png

1.5 Yarn常用命令

1.5.1 yarn application查看任务

(1)列出所有Application:yarn application -list

(2)根据Application状态过滤:yarn application -list -appStates (所有状态:ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED)

yarn application -list -appStates FINISHED

(3)Kill掉Application:yarn application -kill

1.5.2 yarn logs查看日志

(1)查询Application日志:yarn logs -applicationId

(2)查询Container日志:yarn logs -applicationId -containerId

1.5.3 yarn applicationattempt查看尝试运行的任务
(1)列出所有Application尝试的列表:yarn applicationattempt -list

(2)打印ApplicationAttemp状态:yarn applicationattempt -status

1.5.4 yarn container查看容器

(1)列出所有Container:yarn container -list

(2)打印Container状态: yarn container -status

1.5.5 yarn node查看节点状态

列出所有节点:yarn node -list -all

1.5.6 yarn rmadmin更新配置

加载队列配置:yarn rmadmin -refreshQueues

1.5.7 yarn queue查看队列

打印队列信息:yarn queue -status

1.6 Yarn生产环境核心参数

http://dtstack-static.oss-cn-hangzhou.aliyuncs.com/2021bbs/files_user1/article/b318324bdd82a0b870fcac9627468fe6..png

2 Yarn的Tool接口案例

2.1实现向集群提交任务时可以动态传参功能:

[atguigu@hadoop102 hadoop-3.1.3]$ hadoop jar wc.jar com.atguigu.mapreduce.wordcount2.WordCountDriver /input /output1

目标效果 [atguigu@hadoop102 hadoop-3.1.3]$ hadoop jar wc.jar com.atguigu.mapreduce.wordcount2.WordCountDriver -Dmapreduce.job.queuename=root.test /input /output1

2.2实现步骤:

(1)新建Maven项目YarnDemo,pom如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ">
4.0.0

com.atguigu.hadoop
yarn_tool_test
1.0-SNAPSHOT



org.apache.hadoop
hadoop-client
3.1.3


(2)创建类WordCount并实现Tool接口:

package com.atguigu.yarn;

import org.apache.hadoop.conf.Configuration;
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 java.io.IOException;
public class WordCount implements Tool {

private Configuration conf;

@Override
public 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;
}

@Override
public void setConf(Configuration conf) {
this.conf = conf;
}

@Override
public Configuration getConf() {
return conf;
}

public static class WordCountMapper extends Mapper{

private Text outK = new Text();
private IntWritable outV = new IntWritable(1);

@Override
protected 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{
private IntWritable outV = new IntWritable();

@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {

int sum = 0;

for (IntWritable value : values) {
sum += value.get();
}
outV.set(sum);

context.write(key, outV);
}
}
},>,>

(3)新建WordCountDriver

package com.atguigu.yarn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.util.Arrays;

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);
}
}

2.3 测试

在HDFS上准备输入文件,假设为/input目录,向集群提交该Jar包

[atguigu@hadoop102 hadoop-3.1.3]$ yarn jar YarnDemo.jar com.atguigu.yarn.WordCountDriver wordcount /input /output

此时提交的3个参数,第一个用于生成特定的Tool,第二个和第三个为输入输出目录。此时如果我们希望加入设置参数,可以在wordcount后面添加参数,例如:

[atguigu@hadoop102 hadoop-3.1.3]$ yarn jar YarnDemo.jar com.atguigu.yarn.WordCountDriver wordcount -Dmapreduce.job.queuename=root.test /input /output1
————————————————

本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!

《数据资产管理白皮书》下载地址:

《行业指标体系白皮书》下载地址:

《数据治理行业实践白皮书》下载地址:

《数栈V6.0产品白皮书》下载地址:

想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:

同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:

0条评论
上一篇:YARN详解
社区公告
  • 大数据领域最专业的产品&技术交流社区,专注于探讨与分享大数据领域有趣又火热的信息,专业又专注的数据人园地

最新活动更多
微信扫码获取数字化转型资料
钉钉扫码加入技术交流群