在hadoop map reduce上查找百分比

Finding percentage on hadoop map reduce

我正在尝试在 MapReduce 框架上分析航班数据(大约 20 GB)。

我需要找出航班延误的百分比。

如果一个航班最多提前或延迟5分钟起飞,我说的是没有晚点,否则就是晚点

I did this calculation on map method

我确定 class IntSumReducer 的映射 methodreduce 方法(延误和未延误航班的总和)工作正常,但我不知道如何找到延误航班的百分比。

So I think I need to edit reduce method of class Reduce.

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 > 5){
            int actualDepTime = 0;
            int scheduledDepTime = 0;

            try{  
                actualDepTime = (int) Double.parseDouble(columns[4]);
                scheduledDepTime = (int) Double.parseDouble(columns[5]);
            }  
            catch(NumberFormatException nfe){  
                return;  
            }  

            //convert time to minutes
            actualDepTime = ((int) actualDepTime/100) * 60 + actualDepTime%100;
            scheduledDepTime = ((int) scheduledDepTime/100) * 60 + scheduledDepTime%100;

            int diff = actualDepTime - scheduledDepTime;

            //if the differecen is less than 5 minutes
            if(diff <= 5 && diff >= -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() == "NotDelay")
                numOfonTime += val.get();
          }

          persentage = numOfonTime/count;
          result.set(persentage);

          Text sumText = new Text("persentage: ");
          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);
  }
}

错误是没有使用 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);
  }
}