ZBLOG

Job.getInstance()的用法

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

基本用法

  1. 导入必要的包

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

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

    job.setJarByClass(MyMainClass.class);
    job.setMapperClass(MyMapper.class);
    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); // 根据需要设定输出值类型


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

完整示例

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MyMapReduce {

    public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String[] words = value.toString().split("\\s+");
            for (String w : words) {
                word.set(w);
                context.write(word, one);
            }
        }
    }

    public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        
        job.setJarByClass(MyMapReduce.class);
        job.setMapperClass(MyMapper.class);
        job.setCombinerClass(MyReducer.class); // 可选,使用 Combiner 来优化性能
        job.setReducerClass(MyReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

总结

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

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


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?