执行MR程序的时候发生异常,怎么解决呢?

执行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);
}
}
#Java#
全部评论
Map阶段处理文件,默认是按行读取的,每一行由<offset, value>组成,所以mapper 输入key的类型应该是Mapper<IntWritable, Text, Text, IntWritable> 。
点赞 回复
分享
发布于 2018-05-10 10:43

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务