计算出用户上网流量总流量(上行+下行)最高的网站Top3

版权声明:耕种 by 会种地的DT男 https://blog.csdn.net/weixin_42617530/article/details/81986742

分析用户访问网站日志,计算出上网总流量前3名的网站名及总流量

日志部分如下(电话号,URL,上行量,下行量)

13026230503 http://v.baidu.com/tv 20 5000
13826544101 http://www.weibo.com/?category=7 20 5000
13926435656 http://v.baidu.com/tv 20 5000
13926251106 https://www.jianshu.com/p/bb88f7520b33 70 3000
18211575961 http://weibo.com/?category=1760 10 100
13560439658 http://www.51doit.cn 10 4000
15920133257 https://www.jianshu.com/p/bb88f7520b9e 20 3000
13719199419 http://movie.youku.com 10 6000
13000117991 http://www.51doit.cn 10 2000

分析过程:

  1. 读取日志文件内容
  2. 处理获取日志中的电话号,URL,上行量,下行量数据
  3. 汇总网站名及流量,继续处理
  4. 得出结果
所需要工具:
  1. 读取文件 IO流: BufferedReader() 缓冲字符流,利用其中 readLine() 方法。
  2. 字符串分割:split() 方法 ,返回 String 数组
  3. HashMap 储存筛选出来数据< key, value>。
    ** key: String类型, 储存网站名
    ** value: Integer类型,储存总流量
  4. lambda表达式,利用List集合中排序功能进行网站总流量排名
具体实现代码如下(已封装成方法)
public static void getData(String path) throws Exception {
        /**读取日志文件*/
        BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        Map<String, Integer> map = new HashMap<String,Integer>();

        String line =null;
        while((line=br.readLine())!=null) {

            /**读取每一行内容,然后进行分割 空格*/

            String[] split = line.split("\\s");
            String phone=split[0];
            String url=split[1];
            Integer upLoad=Integer.parseInt(split[2]);
            Integer downLoad=Integer.parseInt(split[3]);

            /**除去不符合规范的网站*/
            String[] split2 = url.split("\\.");
            if(split2.length>=3) {
            String webSite=split2[1];
            Integer totalStream = map.getOrDefault(webSite, 0);
            totalStream+=upLoad+downLoad;
            map.put(webSite, totalStream);
            }
        }
        /**利用ArrayList构造方法把map.entrySet转成List集合top3*/
        List<Map.Entry<String, Integer>> top3=new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        List<Entry<String, Integer>> collect = top3.stream().sorted((o1,o2)->o2.getValue()-o1.getValue()).limit(3).collect(Collectors.toList());
        System.out.println("网站总流量(上行+下行)Top3\t\n");
        for (Entry<String, Integer> entry : collect) {
            System.out.println("网站名:"+entry.getKey()+", 总流量:"+entry.getValue());
        }

彩蛋1

流程图
这里写图片描述

/**
当while循环开始, map中无key: baidu, 此时回馈0.返回值Integer类型'
totalStream+=upLoad+downLoad; 此时计算 baidu的总流量
然后map.put("baidu", 5020);
当下次循环, 读取到第二个 baidu时,5020+=upLoad+downLoad.
*/
String webSite=split2[1];
Integer totalStream =map.getOrDefault(webSite, 0);
totalStream+=upLoad+downLoad;
map.put(webSite, totalStream);

猜你喜欢

转载自blog.csdn.net/weixin_42617530/article/details/81986742