BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #59014同步于 2018/5/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

MR程序发生异常Type mismatch in key from map

Albert115
2018/5/10镜像同步3 回复
执行MR程序的时候发生异常:java.lang.Exception: java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.LongWritable 代码如下: package test; import java.io.IOException; 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.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class maxScoreOfEachSubject { // Mapper模块 public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> { public void Map(Object key, Text value, Context context) throws IOException, InterruptedException { String lines = value.toString(); String array[] = lines.split("\\s"); String keyOutput = array[0]; int valueOutput = Integer.parseInt(array[1]); context.write(new Text(keyOutput), new IntWritable(valueOutput)); } } // Reducer模块 public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable sortMax = new IntWritable(); public void Reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int score = 0; for (IntWritable value : values) { score = Math.max(score, value.get()); } sortMax.set(score); context.write(key, sortMax); } } // Driver模块 public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "max score of each subject"); job.setJarByClass(maxScoreOfEachSubject.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); for (int i = 0; i < args.length - 1; ++i) { FileInputFormat.addInputPath(job, new Path(args[i])); } FileOutputFormat.setOutputPath(job, new Path(args[args.length - 1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
superccf机器人#1 · 2018/5/10
在的map(), reduce()方法上面加上@Override
superccf机器人#2 · 2018/5/10
还有你的map reduce方法应该用小写字母开头的吧
Albert115机器人#3 · 2018/5/10
【 在 superccf 的大作中提到: 】 : 还有你的map reduce方法应该用小写字母开头的吧 用小写字母开头,就解决异常了,但是又出现新的问题了,输出的结果字符串为乱码,应该怎么办?