gpt4 book ai didi

java - 错误 :(63, 40) java : incompatible types: org. apache.hadoop.mapreduce.Job 无法转换为 org.apache.hadoop.mapred.JobConf

转载 作者:可可西里 更新时间:2023-11-01 15:01:16 26 4
gpt4 key购买 nike

我只是在 intellj IDE 中运行一个简单的 hadooop 程序。但是当我尝试编译时出现错误

$Error:(63, 40) java: incompatible types: org.apache.hadoop.mapreduce.Job cannot be converted to 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);
}
}

最佳答案

根本原因:

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


解决方案:

更改代码中的这一行:

import org.apache.hadoop.mapred.FileOutputFormat;

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

关于java - 错误 :(63, 40) java : incompatible types: org. apache.hadoop.mapreduce.Job 无法转换为 org.apache.hadoop.mapred.JobConf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com