欢迎光临散文网 会员登陆 & 注册

期末大数据Mapreduce、Hadoop复习

2023-06-08 22:01 作者:是mirror  | 我要投稿

第五题代码理解 有问题 是总数 

不是 每个时间段

修改如下:

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 Fifth {

  public static class TimeRangeUsageMapper extends Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable usageCount = new IntWritable();

    private final static Text timeRange = new Text();


    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

      String[] row = value.toString().split(" ");

      if (!row[0].equals("id")) {

        String datetime = row[1];

        int hour = getHour(datetime);

        int usage = Integer.parseInt(row[10]);

        String[] res = datetime.split(" ");

        if (hour >= 12 && hour <= 15) {

          timeRange.set(res[0]);

          usageCount.set(usage);

          context.write(timeRange, usageCount);

        }

      }

    }


    private int getHour(String datetime) {

      String[] datetimeParts = datetime.split(" ");

      String[] timeParts = datetimeParts[1].split(":");

      return Integer.parseInt(timeParts[0]);

    }

  }


  public static class TimeRangeUsageReducer extends Reducer<Text, IntWritable, Text, IntWritable> {


    private final 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 void main(String[] args) throws Exception {

    Configuration conf = new Configuration();

    Job job = Job.getInstance(conf, "Bike Data Processor");

    job.setJarByClass(Fifth.class);

    job.setMapperClass(TimeRangeUsageMapper.class);

    job.setReducerClass(TimeRangeUsageReducer.class);

    job.setOutputKeyClass(Text.class);

    job.setOutputValueClass(IntWritable.class);

    FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/exam/自己的学号"));

    FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/自己的学号/output"));

    System.exit(job.waitForCompletion(true) ? 0 : 1);

  }

}

期末大数据Mapreduce、Hadoop复习的评论 (共 条)

分享到微博请遵守国家法律