Mic*_*ael 1 java apache hadoop
我只是在intellj IDE中运行一个简单的hadooop程序。但是当我尝试编译时提到了一个错误
$ 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);
}
}
Run Code Online (Sandbox Code Playgroud)
根本原因:
你已经使用旧的API” FileOutputFormat(mapred)在你的工作这需要JobConf对象作为第一个参数没有Job,但是FileInputFormat方法,你从使用新的API(maprecude)这需要Job对象作为第一个参数。(Job也来自新API,JobConf来自旧API)
解:
在代码中更改此行:
import org.apache.hadoop.mapred.FileOutputFormat;
Run Code Online (Sandbox Code Playgroud)
至
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1298 次 |
| 最近记录: |