执行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#
代码如下:
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#