统计手机号耗费的总上行流量、下行流量、总流量(序列化)

————————      ——————————      ——————————       ——————————       ——————————    

1.需求:

统计每一个手机号耗费的总上行流量、下行流量、总流量

2.数据准备:

输入数据的格式:

数据格式:时间戳、电话号码、基站的物理地址、访问网址的ip、网站域名、数据包、接包数、上行/传流量、下行/载流量、响应码

输出数据的格式:

1356·0436666 1116 954 2070
手机号码 上行流量 下行流量 总流量

将此数据新创建成phone_data.txt文档,以便后期实验时使用。

1363157985066  13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com  24 27 2481 24681 200
1363157995052  13826544101 5C-0E-8B-C7-F1-E0:CMCC 120.197.40.4   4 0 264 0 200
1363157991076  13926435656 20-10-7A-28-CC-0A:CMCC 120.196.100.99   2 4 132 1512 200
1363154400022  13926251106 5C-0E-8B-8B-B1-50:CMCC 120.197.40.4   4 0 240 0 200
1363157993044  18211575961 94-71-AC-CD-E6-18:CMCC-EASY 120.196.100.99 iface.qiyi.com 视频网站 15 12 1527 2106 200
1363157995074  84138413 5C-0E-8B-8C-E8-20:7DaysInn 120.197.40.4 122.72.52.12  20 16 4116 1432 200
1363157993055  13560439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99   18 15 1116 954 200
1363157995033  15920133257 5C-0E-8B-C7-BA-20:CMCC 120.197.40.4 sug.so.360.cn 信息安全 20 20 3156 2936 200
1363157983019  13719199419 68-A1-B7-03-07-B1:CMCC-EASY 120.196.100.82   4 0 240 0 200
1363157984041  13660577991 5C-0E-8B-92-5C-20:CMCC-EASY 120.197.40.4 s19.cnzz.com 站点统计 24 9 6960 690 200
1363157973098  15013685858 5C-0E-8B-C7-F7-90:CMCC 120.197.40.4 rank.ie.sogou.com 搜索引擎 28 27 3659 3538 200
1363157986029  15989002119 E8-99-C4-4E-93-E0:CMCC-EASY 120.196.100.99 www.umeng.com 站点统计 3 3 1938 180 200
1363157992093  13560439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99   15 9 918 4938 200
1363157986041  13480253104 5C-0E-8B-C7-FC-80:CMCC-EASY 120.197.40.4   3 3 180 180 200
1363157984040  13602846565 5C-0E-8B-8B-B6-00:CMCC 120.197.40.4 2052.flash2-http.qq.com 综合门户 15 12 1938 2910 200
1363157995093  13922314466 00-FD-07-A2-EC-BA:CMCC 120.196.100.82 img.qfc.cn  12 12 3008 3720 200
1363157982040  13502468823 5C-0A-5B-6A-0B-D4:CMCC-EASY 120.196.100.99 y0.ifengimg.com 综合门户 57 102 7335 110349 200
1363157986072  18320173382 84-25-DB-4F-10-1A:CMCC-EASY 120.196.100.99 input.shouji.sogou.com 搜索引擎 21 18 9531 2412 200
1363157990043  13925057413 00-1F-64-E1-E6-9A:CMCC 120.196.100.55 t3.baidu.com 搜索引擎 69 63 11058 48243 200
1363157988072  13760778710 00-FD-07-A4-7B-08:CMCC 120.196.100.82   2 2 120 120 200
1363157985066  13560436666 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com  24 27 2481 24681 200
1363157993055  13560436666 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99   18 15 1116 954 200

3.分析:

Map阶段:

(1)读取一行数据,切分字段

(2)抽取手机号、上行流量、下行流量

(3)以手机号为key,bean对象为value输出,即context.write(手机号,bean);

Reduce阶段:

(1)累加上行流量和下行流量得到总流量。

(2)实现自定义的bean来封装流量信息,并将bean作为map输出的key来传输

(3)MR程序在处理数据的过程中会对数据排序(map输出的kv对传输到reduce之前,会排序),排序的依据是map输出的key

所以,我们如果要实现自己需要的排序规则,则可以考虑将排序因素放到key中,让key实现接口:WritableComparable。
然后重写key的compareTo方法。

4.代码实现:

(1)FlowBean类:

import lombok.AllArgsConstructor;
import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Author : 若清 and wgh
 * Version : 2020/4/13 & 1.0
 */

@AllArgsConstructor
public class FlowBean implements Writable {
    private long upflow;
    private long downflow;
    private long sumflow;

    public FlowBean(){

    }

    public long getUpflow() {
        return upflow;
    }

    public void setUpflow(long upflow) {
        this.upflow = upflow;
    }

    public long getDownflow() {
        return downflow;
    }

    public void setDownflow(long downflow) {
        this.downflow = downflow;
    }

    public long getSumflow() {
        return sumflow;
    }

    public void setSumflow(long sumflow) {
        this.sumflow = sumflow;
    }



    public FlowBean(long upflow,long downflow){
        this.upflow = upflow;
        this.downflow = downflow;
        this.sumflow = upflow + downflow;
    }

    public void write(DataOutput output) throws IOException {
        output.writeLong(this.upflow);
        output.writeLong(this.downflow);
        output.writeLong(this.sumflow);
    }

    public void readFields(DataInput Input) throws IOException {
        this.upflow = Input.readLong();
        this.downflow = Input.readLong();
        this.sumflow = Input.readLong();
    }

    @Override
    public String toString() {
        return this.upflow + "\t" + this.downflow + "\t" + this.sumflow;
    }


}

(2)MapWritable类:

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

import java.io.IOException;

/**
 * Author : 若清 and wgh
 * Version : 2020/4/13 & 1.0
 */
public class MapWritable extends Mapper<LongWritable, Text,Text,FlowBean> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1.获取数据
        String line = value.toString();

        //2.切分数据
        String[] fields = line.split("\t");

        //3.获取上传流量
        long upflow = Long.parseLong(fields[fields.length - 3]);
        long downflow = Long.parseLong(fields[fields.length - 2]);

        //4.输出
        context.write(new Text(fields[1]),new FlowBean(upflow,downflow));
    }
}

(3)ReduceWritable类:

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

import java.io.IOException;

/**
 * Author : 若清 and wgh
 * Version : 2020/4/13 & 1.0
 */
public class ReduceWritable extends Reducer<Text,FlowBean,Text,FlowBean> {
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //1.定义两个计数器,计算每个用户的上传流量、下载流量
        long sumupflow = 0;
        long sumdownflow = 0;

        //2.累加的号的流量和
        for (FlowBean f: values) {
            sumupflow+=f.getUpflow();
            sumdownflow+=f.getDownflow();
        }

        //3.输出
        context.write(key,new FlowBean(sumupflow,sumdownflow));
    }
}

(4)MainWritable类:

import org.apache.hadoop.conf.Configuration;
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.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Author : 若清 and wgh
 * Version : 2020/4/13 & 1.0
 */
public class MainWritable {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"D:\\input\\plus\\input\\phone_data.txt","D:\\input\\plus\\output\\0819"};

        //1.获取job信息
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2.加载jar包
        job.setJarByClass(MainWritable.class);

        //3.关联map和reduce
        job.setMapperClass(MapWritable.class);
        job.setReducerClass(ReduceWritable.class);

        //4.设置最终输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //5.设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //6.提交job任务
        job.waitForCompletion(true);
    }
}

5.运行结果:

(1)没排序之前的数据:
在这里插入图片描述(2)排序之后的数据:
在这里插入图片描述

由此可见,排序的效果是很明显的。
发布了50 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43520450/article/details/105499616