驱动类主要用于关联Mapper 和 Reducer 以及 提交整个程序。
具体代码如下:
//驱动类
public class FlowSumDriver {
public static void main(String[] args){
Configuration conf = new Configuration() ;
// 1. 获取job对象
Job job = Job.getInstance(conf);
// 2. 设置Jar的路径
job.setJarByClass(FlowSumDriver);
// 3. 关联 mapper 和 reducer
job.setMapperClass(FlowCountMapper.class);
job.setReducerClass(FlowCountReducer.class);
// 4. 设置 mapper 输出的key 和 value 类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(FlowBean.class);
// 5. 设置最终输出的key 和 value
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FlowBean.class);
// 6. 设置输入输出路径
FileInputFormat.setInputPaths(job,new Path("e:\Input"));
FileOutputFormat.setOutputPath(job,new Path("e:\output"))
// 7. 提交job
boolean result = job.waitForCompletion(true);
System.exit(result?0:1);
//如果result值为true 则为0,否则为1;
//0时正常退出系统,非0代表异常退出;
}
}
驱动类总分七大步骤:
- 获取job对象
- 设置Jar的路径 (就是当前驱动类)
- 关联 mapper 和 reducer
- 设置 mapper 输出的key 和 value 类型
- 设置最终输出的key 和 value
- 设置输入输出路径(要读取的数据输入路径和输出数据的路径)
- 提交job