我写了一个MapReduce程序来处理文本文件中的数据。但是当我在本地运行它(Linux VM)时,它会发出一个错误,它将地图中的密钥标识为LongWriteable
,而不是Text
类所要求的Mapper
。
日志输出:
2017-05-21 14:06:46,436 INFO [main] org.apache.hadoop.mapred.MapTask: Map output collector class = org.apache.hadoop.mapred.MapTask$MapOutputBuffer
2017-05-21 14:06:46,454 INFO [main] org.apache.hadoop.mapred.MapTask: Starting flush of map output
2017-05-21 14:06:46,468 WARN [main] org.apache.hadoop.mapred.YarnChild: Exception running child : java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.LongWritable
at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1072)
at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:715)
at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112)
at org.apache.hadoop.mapreduce.Mapper.map(Mapper.java:125)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:175)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1807)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:169)
这是我程序的代码。
映射器:
public static class RepaymentMapper extends Mapper<Text, Text, Text, Text> {
public void map(Text key, Text values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
if(values.toString() != null) {
try {
String[] data = values.toString().split(DefaultValues.DATA_SEPARATOR);
String repaymentDate = data[0];
String accountNo = data[1];
double repaymentAmount = Double.parseDouble(data[2]);
double monthInstallment = Double.parseDouble(data[3]);
double unCollectedAmount = 0;
if(repaymentAmount == 0)
unCollectedAmount = monthInstallment;
else if(repaymentAmount<monthInstallment)
unCollectedAmount = monthInstallment - repaymentAmount;
output.collect(new Text(accountNo), new Text(repaymentDate + DefaultValues.DATA_SEPARATOR + String.valueOf(unCollectedAmount)));
} catch(IOException io) {
throw io;
}
}
}
}
减速机:
public static class RepaymentReducer extends Reducer<Text, Text, Text, DoubleWritable> {
public void reduce(Text key, Iterable<Text> values, OutputCollector<Text, DoubleWritable> output, Reporter reporter) throws IOException {
try {
for(Text v : values) {
String[] data = v.toString().split(DefaultValues.DATA_SEPARATOR);
output.collect(key,new DoubleWritable(Double.parseDouble(data[1])));
}
} catch(IOException io) {
throw io;
}
}
}
主:
public static void main(String[] args) {
Configuration conf = new Configuration();
try {
Job job = Job.getInstance(conf, "Loan Repayment Job");
job.setJarByClass(RepaymentAnalyticJob.class);
job.setMapperClass(RepaymentMapper.class);
job.setCombinerClass(RepaymentReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
} catch(Exception e) {
e.printStackTrace();
}
}