错误:(63,40)java:不兼容的类型:org.apache.hadoop.mapreduce.job无法转换为org.apache.hadoop.mapred.jobconf

8ehkhllq  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(1156)

我只是在intelljide上运行一个简单的hadoop程序。但是当我试图编译时,有一个错误被提到了
$error:(63,40)java:不兼容类型:org.apache.hadoop.mapreduce.job无法转换为org.apache.hadoop.mapred.jobconf
下面是我为这个小程序编写的代码:

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.mapred.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordCount {

public static class TokenizerMapper
        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 {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}

public static class IntSumReducer
        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(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.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);
 }
}
dm7nw8vv

dm7nw8vv1#

根本原因:
您在作业中使用了旧api的fileoutputformat(mapred),这需要 JobConf 对象作为第一个参数不是 Job ,但是 FileInputFormat 从新api(maprecure)使用的方法 Job 对象作为第一个参数( Job 也来自新的api, JobConf 来自旧api)
解决方案:
更改代码中的此行:

import org.apache.hadoop.mapred.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

相关问题