前言
Mapreduce 是一个分布式运算程序的编程框架,是用户开发“基于 hadoop的数据分析 应用”的核心框架。Mapreduce 核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序,并发运行在一个 hadoop 集群上。
![5679451-536c2564316b8b13.png](https://i-blog.csdnimg.cn/blog_migrate/b86bedf71992a74822ac42f22e798598.webp?x-image-process=image/format,png)
执行流程图
一.waitForCompletion
在mapreduce程序的job类中,我们通过set Configuration对象,得到相应的job对象,在job对象中指定Mapper类、Reducer类,Job类等属性后,通过waitForCompletion(true)方法提交并等待job执行。传入的boolean类型参数决定是否监控并打印job的执行情况。
public class MyJob {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
if (args.length!=2){
System.out.println("请传入参数");
System.exit(1);
}
String inputPath = args[0];
String outputPath = args[1];
if (inputPath == null || inputPath == "" || outputPath == null || outputPath == ""){
System.out.println("参数有误");
System.exit(1);
}
Configuration conf = new Configuration(true);
Path out = new Path(outputPath);
if (out.getFileSystem(conf).exists(out)){
System.out.println("HDFS输出目录已存在");
System.exit(1);
}
//构建job类
Job job = Job.getInstance(conf);
//设置运行主类
job.setJarByClass(MyJob.class);
//作业名称
job.setJobName("job");
//设置输入输出路径
FileInputFormat.addInputPath(job,new Path(inputPath));
FileOutputFormat.setOutputPath(job,out);
//先是format
//job.setInputFormatClass();
//mapTask
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(TQ.class);
job.setMapOutputValueClass(IntWritable.class);
//然后是排序比较器
job.setSortComparatorClass(TQSortComparator.class);
//partition分组规则
job.setPartitionerClass(TQPartitioner.class);
//分组比较器
job.setGroupingComparatorClass(TQGroupComparator.class);
//reduceTask
job.setReducerClass(MyReduce.class);
//设置ReduceTask数量
job.setNumReduceTasks(2);
//提交任务完成
job.waitForCompletion(true);
}
}
现在我们进入Job类中的waitForCompletion()方法查看,该方法传入一个布尔值参数。方法首先检查Job状态,若处于DEFINE状态则通过submit()方法提交job。而后根据传入的参数决定是否监控并打印job的运行状况。
该方法每隔 1 秒轮询作业的进度,如果进度有所变化,将该进度报告给控制台(console)。当作业成功完成,作业计数器被显示出来。否则,导致作业失败的错误被记录到控制台。
/**
* Submit the job to the cluster and wait for it to finish.
* @param verbose print the progress to the user
* @return true if the job succeeded
* @throws IOException thrown if the communication with the
* <code>JobTracker</code> is lost
*/
public boolean waitForCompletion(boolean verbose
) throws IOException, InterruptedException,
ClassNotFoundException {
//首先检查Job状态,若处于DEFINE状态则通过submit()方法向集群提交job
if (state == JobState.DEFINE) {
submit();
}
//若传入参数为true,则监控并打印job运行情况
i