错误:(63,40)java:不兼容的类型:org.apache.hadoop.mapreduce.Job不能转换为or



我只是在智能IDE中运行一个简单的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);
 }
}

根源:

你在你的工作中使用了旧的API' FileOutputFormat(mapred),它将JobConf对象作为第一个参数,而不是Job,但是你从新的API(maprecude)中使用的FileInputFormat方法将Job对象作为第一个参数。(Job也来自新API, JobConf来自旧API)


解决方案:

在你的代码中修改这一行:

import org.apache.hadoop.mapred.FileOutputFormat;

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

最新更新