Yarn调度器
目前,Hadoop 作业调度器主要有三种:FIFO、容量(Capacity Scheduler)和公平(Fair Scheduler)。Apache Hadoop3.1.3 默认的资源调度器是 Capacity Scheduler。 CDH 框架默认调度器是 Fair Scheduler。
1、先进先出调度器(FIFO)
2、容量调度器(Capacity Scheduler)
这里队列资源分配中,3条队列,如果有空闲的NameNode节点,优先给队列A分配资源,让其先运行,然后根据根据作业的优先级(可以设置),没设置优先级根据作业提交时间判断先给哪个作业运行,一个作业可能有多个MapTask和ReduceTask需要分配多个容器,可以根据优先级来判断先给哪个容器分配资源来运行,如果没设置优先级就根据HDFS中的资源和容器的节点距离来判定。
3、公平调度器(Fair Scheduler)
缺额是什么呢?
队列优先级有了,公平调度器队列内部的任务执行顺序是什么呢?
公平调度器队列资源如何分配?队列内部资源如何分配?
队列内部
说了fifo策略和fair策略还有一个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和内存)的一个不同比例的限制。
Yarn 常用命令
1、yarn查看任务(application)
yarn application -list
2、根据 Application 状态过滤:yarn application -list -appStates (所有状态:ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED)
3、Kill 掉 Application
yarn application -kill application_1612577921195_0001(这是Application-Id,可以根据上面语句查询出来)
4、查询任务(Application) 日志:yarn logs -applicationId
5、查询某个任务某个容器( Container) 日志:yarn logs -applicationId -containerId
6、yarn applicationattempt 查看尝试运行的任务(正在运行中的)
yarn applicationattempt -list
7、打印 ApplicationAttemp 状态:yarn applicationattempt -status (上边语句查询出来的运行中任务的id)
8、列出所有 Container:yarn container -list 运行中任务所使用的容器
9、打印 Container 状态:yarn container -status
10、 yarn node 查看节点状态:yarn node -list -all
11、 yarn rmadmin 更新配置:yarn rmadmin -refreshQueues更新队列的配置可以不重启yarn更新配置
12、yarn queue 查看队列:yarn queue -status (队列名称)
yarn详细配置可以查看网上资料
Tool接口
为什么需要这个接口呢?
hadoop jar wc.jar com.atguigu.mapreduce.wordcount2.WordCountDriver -
Dmapreduce.job.queuename=root.test /input /output1
希望指定队列来执行程序,但是报错,程序误以为第一个是路径所以报错。
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<LongWritable, Text, Text, IntWritable>{
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<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);
}
}
}
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);
}
}