MapReduce:出租车数据案例

2021-12-06 20:00:45 浏览数 (1)

文章目录
  • 计算出10月1日这天每小时的载客量
  • 计算出10月1日这天超速(超过120)的车辆,超速的次数,超速的详细时间
  • 计算出10月1日这天全天停运的车辆
  • 计算出10月1日这天载客次数超过10次的车辆,载客总次数,载客详细时间。
  • 计算出10月1日这天连续与运行12小时一以上的车辆
  • 计算出10月1日载客次数大于10月2日载客次数的车辆
  • 计算出10月1日上班10月2日没有上班的车辆
  • 计算出连续48小时运营的车辆

部分数据:

链接: https://pan.baidu.com/s/1cFbcj5tz5Gy6AljgpPBTyg

提取码:ccem

计算出10月1日这天每小时的载客量

CarMapper:

代码语言:javascript复制
package com.hadoop.map;

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(",");


            String time=split[3];
            if (time.substring(4,6).equals("10")&&time.substring(6,8).equals("01")){
                //key是车牌号 每小时
                context.write(new Text(time.substring(8,10)),value);
            }
        }



    }
}

CarReduce:

代码语言:javascript复制
package com.hadoop.reduce;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

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

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    /**
     * 载客量
     */
        int CarryingCapacity=0;
        HashMap<String, ArrayList<String>> map = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        for (Text value : values) {
            String[] split = value.toString().split(",");
            if (split[2].equals("1")){
                CarryingCapacity  ;
            }
    
        }
    
        context.write(key,new Text(CarryingCapacity ""));






    }
}

CarDriver:

代码语言:javascript复制
package com.hadoop.driver;

import com.hadoop.map.CarMapper;
import com.hadoop.reduce.CarReduce;
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日这天超速(超过120)的车辆,超速的次数,超速的详细时间

CarMapper:

代码语言:javascript复制
package com.hadoop.Car1;

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 (Integer.parseInt(split[6])>120){
    
            context.write(new Text(split[0]),value);
            }
    
        }



    }
}

CarReduce:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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<Text> list = new ArrayList<>();// [01 01  02  01 03 05 06 ]
        HashSet<Integer> 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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
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:

代码语言:javascript复制
package com.hadoop.Car6;

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 {
            boolean falg1=true;//当falg1为false的时候就证明10月1日没有上班
            boolean falg2=true;//当falg1为false的时候就证明10月2日没有上班
        for (Text value : values) {
            if (value.toString().contains("20121001")){
                String[] split = value.toString().split(",");
                if (split[2].equals("3")){
                    falg1=false;
                }
            }else{
                String[] split = value.toString().split(",");
                if (split[2].equals("3")) {
                    falg2=false;
                }
            }
        }
       if (falg1==true&&falg2==false){
           context.write(key,new Text("十月一日:" falg1 "-----" "十月二日:" falg2));
       }
    
    }
}

CarDriver:

代码语言:javascript复制
package com.hadoop.Car6;

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);
    }
}

计算出连续48小时运营的车辆

CarMapper:

代码语言:javascript复制
package com.hadoop.Taxi08;

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 TaxiMap08  extends Mapper<LongWritable,Text,Text,Text> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        FileSplit fileSplit = (FileSplit) context.getInputSplit();
    
        String name = fileSplit.getPath().getName();
    
        if (name.contains("20121001")){
            if (value.toString().length()>0){
                String[] split = value.toString().split(",");
                context.write(new Text(split[0]), value);
    
            }
        }else{
            if (value.toString().length()>0){
                String[] split = value.toString().split(",");
                context.write(new Text(split[0]), value);
    
            }
        }


    }
}

CarReduce:

代码语言:javascript复制
package com.hadoop.Taxi08;

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

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


public class TaxiReduce08 extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        HashSet<Integer> One = new HashSet<>();
        HashSet<Integer> Two = new HashSet<>();
        StringBuffer sbOne = new StringBuffer();
        StringBuffer sbTwo = new StringBuffer();

        for (Text value : values) {
            if (value.toString().contains("20121001")){
                String[] split = value.toString().split(",");
                One.add(Integer.parseInt(split[3].substring(8,10 ))); //key ----> value:01 02 03 04 05 06
    
            }else{
                String[] split = value.toString().split(",");
                Two.add(Integer.parseInt(split[3].substring(8,10 ))); //key ----> value:01 02 03 04 05 06
            }
        }
    
        for (int i = 0; i < 24; i  ) {
            if (One.contains(i)){
                sbOne.append("1");
    
            }else{
                sbOne.append("0");
            }
    
            if (Two.contains(i)){
                sbTwo.append("1");
            }else{
                sbTwo.append("0");
            }
        }
    
        if (!sbOne.toString().contains("0")&&!sbTwo.toString().contains("0")){
         context.write(key, new Text(sbOne "---------" sbTwo "---------" "此车连续运行48小时"));
        }


    }
}

CarDriver:

代码语言:javascript复制
package com.hadoop.Taxi08;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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 TaxiDriver08 extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        Job job=Job.getInstance(configuration, "TaxiDriver08");

        job.setJarByClass(TaxiDriver08.class);
    
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path("D:\出租车数据\ALLtxt"));
    
        job.setMapperClass(TaxiMap08.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
    
        job.setReducerClass(TaxiReduce08.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
    
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, new Path("data/08_output"));
        return job.waitForCompletion(true)?0:1;
    }
    
    public static void main(String[] args) throws Exception {
        int run = ToolRunner.run(new TaxiDriver08(), args);
        System.out.println(run);
    }
}

GPS数据格式说明 数据格式 数据以ASCII文本表示,以逗号为分隔符,以回车换行符(0x0D 0x0A)结尾。

数据项及顺序:

车机标识,触发事件,运营状态,GPS时间,GPS经度,GPS纬度,GPS速度,GPS方向,GPS状态

车机标识:6个字符 触发事件:0=变空车,1=变载客,2=设防,3=撤防,4=其它 运营状态:0=空车,1=载客,2=驻车,3=停运,4=其它 GPS时间:格式yyyymmddhhnnss,北京时间 GPS经度:格式ddd.ddddddd,以度为单位。 GPS纬度:格式dd.ddddddd,以度为单位。 GPS速度:格式ddd,取值000-255内整数,以公里/小时为单位。 GPS方位:格式ddd,取值000-360内整数,以度为单位。 GPS状态:0=无效,1=有效 结束串:回车符 换行符

数据示例: 123456,0,0,20110414160613,116.4078674,40.2220650,21,274,1<0x0D0x0A>

数据发送方式: 每次200条以上的数据压缩打包后UDP发送。采用ZLIB125压缩库最大压缩率压缩。 接收应答:每接收1包或多包回复1个4字节整数,这个整数可以是收到的包数累计或字节数累计,该回复仅用于判断链路是否正常,不用于重发判断。

0 人点赞