我正试图在MapReduce Framework上分析A Flight数据(大约20 GB)。
我需要找到延误航班的百分比。
如果航班起飞时间提前或延迟最多5分钟,我说它没有延迟,否则延迟。
我在地图方法上做了这个计算
我确信地图method
和reduce
方法class IntSumReducer
(延迟和未延迟航班的总和)正常工作,但我不能弄清楚如何找到延误航班的百分比。
所以我认为我需要修改
reduce
的{{1}}方法。
class Reduce
答案 0 :(得分:0)
错误没有使用 equals方法,这非常简单。 我没有关闭这个问题,因为以后代码可能对某人有用。
下面是正确的代码,但有一些小改动:
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
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 FlightAnalyse {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text delayOrNot = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
String[] columns = value.toString().split(",");
if(columns.length > 15){
int delay = 0;
try{
delay = (int) Double.parseDouble(columns[15]); //delay in minutes
}
catch(NumberFormatException nfe){
return;
}
//if the differecen is less than 5 minutes
if(delay <= 5 && delay >= -5)
delayOrNot.set("NotDelay");
else
delayOrNot.set("Delay");
context.write(delayOrNot, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static class Reduce
extends Reducer<Text,IntWritable,Text,FloatWritable> {
private FloatWritable result = new FloatWritable();
Float persentage = 0f;
Float numOfonTime = 0f;
Float count = 0f;
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
for (IntWritable val : values) {
count += val.get();
if(key.toString().equals("NotDelay"))
numOfonTime += val.get();
}
persentage = numOfonTime/count;
result.set(persentage);
Text sumText = new Text("persentage of " + key + ": ");
context.write(sumText, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Flight Analysis");
job.setJarByClass(FlightAnalyse.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}