1、获取配置文件,并创建job,提交到集群如:
Configuration conf = getConf();
conf.set("fs.defaultFS", "hdfs://192.168.30.150:8020");
String jobName = getClass().getSimpleName();
Job job = Job.getInstance(conf, jobName);
job.setJarByClass(TestMapReduce.class);
job.setMapperClass(TestMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setNumReduceTasks(0);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.waitForCompletion(true);
当执行waitForCompletion(true)之后hadoop是如何将任务提交到集群上的呢?以下步骤都是在客户端执行:
1、获取资源提交路径
本地路径为:file://....../.staging
yarn环境为:hfds://....../.staging
和JobId拼接成一个Job资源提交路径如:
hfds://....../.staging/jobid
2、调用FileInputFormat.getSplits()获取切片规划List<FileSplit>,并序列化为文件job.split,拷贝到job资源提交路径中去。
3、将job相关参数写成一个文件job.xml,拷贝到job资源提交路径中去。
4、获取job的jar包,拷贝到job资源提交路径中去。就是上面代码中的:
job.setJarByClass(TestMapReduce.class);
/**
* Set the job's jar file by finding an example class location.
*
* @param cls the example class.
*/
public void setJarByClass(Class cls) {
String jar = ClassUtil.findContainingJar(cls);
if (jar != null) {
setJar(jar);
}
}
意思是在class文件的路径下获取job的jar包。
5、接下来就是Yarn或本地程序如何执行程序了。