MapReduce 出租车数据案例_mapreduce处理train,离开小厂进大厂的第一周

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

}


CarReduce:



package com.hadoop.Car1;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CarReduce extends Reducer<Text,Text,Text,Text> {

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    StringBuffer sb =new StringBuffer();
    int count=0;
    for (Text value : values) {

           count++;
           String s = value.toString().split(",")[3];
            sb.append(s).append("-");


    }
    context.write(key,new Text("超速的次数:"+count+"超速的时间---->"+sb));





}

}


CarDriver:



package com.hadoop.Car1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CarDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job JB = Job.getInstance(configuration, “CarDriver”);
FileSystem fileSystem = FileSystem.get(configuration);

    JB.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(JB,new Path("E:\\出租车数据\\2012001"));


    JB.setMapperClass(CarMapper.class);
    JB.setMapOutputKeyClass(Text.class);
    JB.setMapOutputValueClass(Text.class);

    JB.setReducerClass(CarReduce.class);
    JB.setOutputKeyClass(Text.class);
    JB.setOutputValueClass(Text.class);
    boolean exists = fileSystem.exists(new Path("E:\\出租车数据\\Out"));
    if (exists){
        fileSystem.delete(new Path("E:\\出租车数据\\Out"),true);
    }
    JB.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(JB,new Path("E:\\出租车数据\\Out"));


    return JB.waitForCompletion(true)?0:1;
}

public static void main(String[] args) throws Exception {
    ToolRunner.run(new CarDriver(),args);
}

}


## 计算出10月1日这天全天停运的车辆


CarMapper:



package com.hadoop.Car2;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CarMapper extends Mapper<LongWritable, Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
if (split[3].substring(4,6).equals(“10”)&&split[3].substring(6,8).equals(“01”)){
context.write(new Text(“”),value);
}

    }



}

}


CarReduce:



package com.hadoop.Car2;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CarReduce extends Reducer<Text,Text,Text,Text> {

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    int count=0;
    for (Text value : values) {
        String[] split = value.toString().split(",");
        if (split[2].equals("3")||split[2]=="3"){
            count++;
        }

    }
    context.write(new Text(""),new Text("这天全天停运的车辆为:"+count+"辆"));





}

}


CarDriver:



package com.hadoop.Car2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CarDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job JB = Job.getInstance(configuration, “CarDriver”);
FileSystem fileSystem = FileSystem.get(configuration);

    JB.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(JB,new Path("E:\\出租车数据\\2012001"));


    JB.setMapperClass(CarMapper.class);
    JB.setMapOutputKeyClass(Text.class);
    JB.setMapOutputValueClass(Text.class);

    JB.setReducerClass(CarReduce.class);
    JB.setOutputKeyClass(Text.class);
    JB.setOutputValueClass(Text.class);
    boolean exists = fileSystem.exists(new Path("E:\\出租车数据\\Out"));
    if (exists){
        fileSystem.delete(new Path("E:\\出租车数据\\Out"),true);
    }
    JB.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(JB,new Path("E:\\出租车数据\\Out"));


    return JB.waitForCompletion(true)?0:1;
}

public static void main(String[] args) throws Exception {
    ToolRunner.run(new CarDriver(),args);
}

}


## 计算出10月1日这天载客次数超过10次的车辆,载客总次数,载客详细时间。


CarMapper:



package com.hadoop.Car3;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CarMapper extends Mapper<LongWritable, Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
if (split[3].substring(4,6).equals(“10”)&&split[3].substring(6,8).equals(“01”)){
context.write(new Text(split[0]),value);
}

    }



}

}


CarReduce:



package com.hadoop.Car3;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CarReduce extends Reducer<Text,Text,Text,Text> {

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    int count=0;
    StringBuffer sb = new StringBuffer();
    for (Text value : values) {
        String[] split = value.toString().split(",");
        if (split[2].equals("1")||split[2]=="1"){
            count++;
            String s=split[3];
            sb.append(s).append("\_");
        }

    }
    if (count>10){

    context.write(new Text(key),new Text("载客总次数:"+count+"------>载客详细时间:"+sb));
    }





}

}


CarDriver:



package com.hadoop.Car3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CarDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job JB = Job.getInstance(configuration, “CarDriver”);
FileSystem fileSystem = FileSystem.get(configuration);

    JB.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(JB,new Path("E:\\出租车数据\\2012001"));


    JB.setMapperClass(CarMapper.class);
    JB.setMapOutputKeyClass(Text.class);
    JB.setMapOutputValueClass(Text.class);

    JB.setReducerClass(CarReduce.class);
    JB.setOutputKeyClass(Text.class);
    JB.setOutputValueClass(Text.class);
    boolean exists = fileSystem.exists(new Path("E:\\出租车数据\\Out"));
    if (exists){
        fileSystem.delete(new Path("E:\\出租车数据\\Out"),true);
    }
    JB.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(JB,new Path("E:\\出租车数据\\Out"));


    return JB.waitForCompletion(true)?0:1;
}

public static void main(String[] args) throws Exception {
    ToolRunner.run(new CarDriver(),args);
}

}


## 计算出10月1日这天连续与运行12小时一以上的车辆


CarMapper:



package com.hadoop.Car4;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CarMapper extends Mapper<LongWritable, Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
if (split[3].substring(4,6).equals(“10”)&&split[3].substring(6,8).equals(“01”)){
context.write(new Text(split[0]),value);
}

    }



}

}


CarReduce:



package com.hadoop.Car4;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.HashSet;

public class CarReduce extends Reducer<Text,Text,Text,Text> {

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
   StringBuffer sb = new StringBuffer();

// ArrayList list = new ArrayList<>();// [01 01 02 01 03 05 06 ]
HashSet list12 = new HashSet<>();
for (Text value : values) {
String[] split = value.toString().split(“,”);
// list.add(new Text(split[3].substring(8,10)));
for (int i = 0; i < 24; i++) {
if (Integer.parseInt(split[3].substring(8,10))==i){
list12.add(Integer.parseInt(split[3].substring(8,10)));
}
}
}

    for (int i = 0; i < 24; i++) {

        if (list12.contains(i)){
            sb.append("1");
        }else{
            sb.append("0");
        }
    }




    if (sb.toString().contains("111111111111")) {
        context.write(key,new Text("----->此车运行了12以上"));
    }


}

}


CarDriver:



package com.hadoop.Car4;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CarDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job JB = Job.getInstance(configuration, “CarDriver”);
FileSystem fileSystem = FileSystem.get(configuration);

    JB.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(JB,new Path("E:\\出租车数据\\2012001"));


    JB.setMapperClass(CarMapper.class);
    JB.setMapOutputKeyClass(Text.class);
    JB.setMapOutputValueClass(Text.class);

    JB.setReducerClass(CarReduce.class);
    JB.setOutputKeyClass(Text.class);
    JB.setOutputValueClass(Text.class);
    boolean exists = fileSystem.exists(new Path("E:\\出租车数据\\Out"));
    if (exists){
        fileSystem.delete(new Path("E:\\出租车数据\\Out"),true);
    }
    JB.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(JB,new Path("E:\\出租车数据\\Out"));


    return JB.waitForCompletion(true)?0:1;
}

public static void main(String[] args) throws Exception {
    ToolRunner.run(new CarDriver(),args);
}

}


## 计算出10月1日载客次数大于10月2日载客次数的车辆


CarMapper:



package com.hadoop.Car5;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class CarMapper extends Mapper<LongWritable, Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
FileSplit sp = (FileSplit) context.getInputSplit();
String name = sp.getPath().getName();
if (name.contains(“20121001”)){
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);
}
}else {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);

        }
    }





}

}


CarReduce:



package com.hadoop.Car5;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CarReduce extends Reducer<Text,Text,Text,Text> {

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    int  One=0;
    int Two=0;
    for (Text value : values) {
        if (value.toString().contains("20121001")){
            String[] split = value.toString().split(",");
            if (split[1].equals("4")&&split[2].equals("1")){
                One++;
            }
        }else{
            String[] split = value.toString().split(",");
            if (split[1].equals("4")&&split[2].equals("1")) {
                Two++;
            }
        }
    }
    if (One>Two){

    context.write(key,new Text("十月一日载客量:"+One+"----大于---"+"十月二日载客量:"+Two));
    }

}

}


CarDriver:



package com.hadoop.Car5;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CarDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job JB = Job.getInstance(configuration, “CarDriver”);
FileSystem fileSystem = FileSystem.get(configuration);

    JB.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(JB,new Path("E:\\出租车数据\\201210011002"));


    JB.setMapperClass(CarMapper.class);
    JB.setMapOutputKeyClass(Text.class);
    JB.setMapOutputValueClass(Text.class);

    JB.setReducerClass(CarReduce.class);
    JB.setOutputKeyClass(Text.class);
    JB.setOutputValueClass(Text.class);
    boolean exists = fileSystem.exists(new Path("E:\\出租车数据\\Out"));
    if (exists){
        fileSystem.delete(new Path("E:\\出租车数据\\Out"),true);
    }
    JB.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(JB,new Path("E:\\出租车数据\\Out"));


    return JB.waitForCompletion(true)?0:1;
}

public static void main(String[] args) throws Exception {
    ToolRunner.run(new CarDriver(),args);
}

}


## 计算出10月1日上班10月2日没有上班的车辆


CarMapper:



package com.hadoop.Car6;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class CarMapper extends Mapper<LongWritable, Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
FileSplit sp = (FileSplit) context.getInputSplit();
String name = sp.getPath().getName();
if (name.contains(“20121001”)){
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);
}
}else {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);

        }
    }





}

}


CarReduce:



package com.hadoop.Car6;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

ing().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);
}
}else {
if (value!=null&&value.toString().length()>0){
String[] split = value.toString().split(“,”);
context.write(new Text(split[0]),value);

        }
    }





}

}


CarReduce:



package com.hadoop.Car6;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)
[外链图片转存中…(img-qls3mLN0-1713309270592)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值