当我尝试基于 Hadoop 0.20 API 的 Hadoop in Action 书中的 Map Reduce 编程示例时,我得到了错误
java.io.IOException:映射中的值类型不匹配:预期 org.apache.hadoop.io.IntWritable,收到 org.apache.hadoop.io.Text
但据我检查,我正在正确通过所有内容。如果有人可以帮助我,那将非常有帮助。
这是代码。它与书中的代码相同。
@SuppressWarnings("unused")
public class CountPatents extends Configured implements Tool {
@SuppressWarnings("deprecation")
public static class MapClass extends MapReduceBase implements Mapper<Text, Text, Text, Text> {
public void map(Text key, Text value,OutputCollector<Text, Text> output,Reporter reporter) throws IOException {
output.collect(value, key);
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, IntWritable> {
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int count=0;
while(values.hasNext()){
count=count+1;
values.next();
}
output.collect(key, new IntWritable(count));
}
}
public int run(String[] args) throws Exception {
Configuration conf = getConf();
JobConf job = new JobConf(conf, CountPatents.class);
Path in = new Path(args[0]);
Path out = new Path(args[1]);
FileInputFormat.setInputPaths(job, in);
FileOutputFormat.setOutputPath(job, out);
job.setJobName("MyJob");
job.setMapperClass(MapClass.class);
job.setReducerClass(Reduce.class);
job.setInputFormat(KeyValueTextInputFormat.class);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.set("key.value.separator.in.input.line", ",");
JobClient.runJob(job);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new CountPatents(), args);
System.exit(res);
}
}