ZBLOG

Job.getInstance()的用法

Job.getInstance() 通常与 Hadoop 的 MapReduce 相关,用于获取 Job 类的实例。具体来说,Hadoop 提供了一个 Job 类来设置和配置作业(即 MapReduce 作业)。以下是其基本用法:

基本用法

  1. 导入必要的包

    1. import org.apache.hadoop.conf.Configuration;
    2. import org.apache.hadoop.mapreduce.Job;
  2. 创建 Job 实例

    1. Configuration conf = new Configuration();
    2. Job job = Job.getInstance(conf, "My Job Name");
  3. 设置 Mapper 和 Reducer

    1. job.setJarByClass(MyMainClass.class);
    2. job.setMapperClass(MyMapper.class);
    3. job.setReducerClass(MyReducer.class);
  4. 设置输入和输出格式: “`java FileInputFormat.addInputPath(job, new Path(“input_path”)); FileOutputFormat.setOutputPath(job, new Path(“output_path”));

job.setOutputKeyClass(Text.class); // 根据需要设定输出键类型 job.setOutputValueClass(IntWritable.class); // 根据需要设定输出值类型

  1. 5. **提交作业**:
  2. ```java
  3. System.exit(job.waitForCompletion(true) ? 0 : 1);

完整示例

下面是一个完整的 MapReduce 作业示例:

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Job;
  6. import org.apache.hadoop.mapreduce.Mapper;
  7. import org.apache.hadoop.mapreduce.Reducer;
  8. import java.io.IOException;
  9. public class MyMapReduce {
  10. public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
  11. private final static IntWritable one = new IntWritable(1);
  12. private Text word = new Text();
  13. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  14. String[] words = value.toString().split("\\s+");
  15. for (String w : words) {
  16. word.set(w);
  17. context.write(word, one);
  18. }
  19. }
  20. }
  21. public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  22. private IntWritable result = new IntWritable();
  23. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  24. int sum = 0;
  25. for (IntWritable val : values) {
  26. sum += val.get();
  27. }
  28. result.set(sum);
  29. context.write(key, result);
  30. }
  31. }
  32. public static void main(String[] args) throws Exception {
  33. Configuration conf = new Configuration();
  34. Job job = Job.getInstance(conf, "word count");
  35. job.setJarByClass(MyMapReduce.class);
  36. job.setMapperClass(MyMapper.class);
  37. job.setCombinerClass(MyReducer.class); // 可选,使用 Combiner 来优化性能
  38. job.setReducerClass(MyReducer.class);
  39. job.setOutputKeyClass(Text.class);
  40. job.setOutputValueClass(IntWritable.class);
  41. FileInputFormat.addInputPath(job, new Path(args[0]));
  42. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  43. System.exit(job.waitForCompletion(true) ? 0 : 1);
  44. }
  45. }

总结

  • Job.getInstance(Configuration conf) 方法用于创建一个新的 Job 实例,配置并准备执行 MapReduce 作业。
  • 设置 Mapper、Reducer、输入输出路径及格式后,可以提交作业。

确保在运行之前已经正确配置了 Hadoop 环境,并根据自己的需求进行调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=22942

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?