重要点 :看注释

access.log中统计数据

  1. healthcheck.html的请求不计入统计
  2.  输出请求总量,以及GETPOST分别的总量
  3.  输出请求最频繁的10个接口及其次数,按次数降序
  4.  输出每个小时有多少分钟请求数超过400次, 比如12点有30分钟每分钟超过了400次,11点有35分钟每分钟超过了400
public class stream {
    public static void main(String[] args) {
        //获取资源文件夹路径
        String path = stream.class.getResource("/").getPath() + "access.log";
        File file = new File(path);
        try {
            List<String> lines = Files.readLines(file, Charsets.UTF_8);
            Collection<String> collection = Collections2.filter(lines, x -> !x.contains("healthcheck.html"));
            System.out.println("请求总量:" + collection.stream().count());
            //lambda 如果需要多个语句用大括号
            System.out.println("GET请求量:" + collection.stream().filter((String x) -> {
                return x.contains("GET");
            }).count());

            System.out.println("POST请求量:" + collection.stream().filter(x -> x.contains("POST")).count());
            //这里用stream.flatMap和map 结果是一样的 因为没有涉及到逗号,俩者都是用来进行文本格式转换
            // 除此之外 flatMap需要用Stream.of转换一下  map不需要
            // 下面业务就是提取 提交链接
            List<String> newlines = lines.stream().flatMap(line -> Stream.of(line.substring(line.indexOf(" /") + 1))).
                    flatMap(line -> Stream.of(line.substring(0, line.indexOf(" ")))).
                    filter(word -> word.length() > 0).
                    collect(Collectors.toList());


            List<String> finallines = new ArrayList<String>();
            for (String line : newlines) {
                if (line.contains("?")) {
                    finallines.add(line.substring(0, line.indexOf("?")));
                } else {
                    finallines.add(line);
                }
            }

             /*  Collectors.groupingBy(Function.identity(), Collectors.counting())是用来分组的,
             分组函数第一个参数 如下面是代表每一行line内容,后面是计数,因为list可能会有重复内容,按照line内容
             分组并记下重复次数转换到对应Map的key和value  */
            Map<String, Long> map = finallines.stream().
                    collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

             /* 下面是sorted排序 其中 comparingByValue() 是按照map中value升序排序 如果是comparingByKey()
                则按照key升序排序 reversed是反过来 也就是变成降序, skip(1) 是跳过第一条数据,limit(10) 取10条数据
                forEachOrdered 按照顺序输出 */
            Map<String, Long> sortMap = new LinkedHashMap<>();
            map.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed()).skip(1).limit(10).
                    forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));


            for (String key : sortMap.keySet()) {
                System.out.println(key + "  " + sortMap.get(key));
            }


            Table<String, String, Integer> table = HashBasedTable.create();

            Iterator iter = collection.iterator();

            while (iter.hasNext()) {
                String line = (String) iter.next();
                line = line.substring(line.indexOf(":") + 1);
                line = line.substring(0, line.indexOf(" +"));
                String hour = line.substring(0, 2);
                String minute = line.substring(3, 5);

                if (table.contains(hour, minute)) {
                    int count = table.get(hour, minute);
                    table.put(hour, minute, count + 1);
                } else {
                    table.put(hour, minute, 1);
                }

            }

            Iterator celliter = table.cellSet().iterator();
            Map<String, Integer> countMap = new HashMap<>();
            while (celliter.hasNext()) {
                Table.Cell<String, String, Integer> cell = (Table.Cell<String, String, Integer>) celliter.next();
                if (cell.getValue() > 400) {
                    if (countMap.containsKey(cell.getRowKey())) {
                        Integer count = countMap.get(cell.getRowKey()) + 1;
                        countMap.put(cell.getRowKey(), count);
                    } else
                        countMap.put(cell.getRowKey(), 1);
                }
            }

            for (String key : countMap.keySet()) {
                System.out.println(key + "点有" + countMap.get(key) + "分钟每分钟超过了400次");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

posted on 2018-07-13 19:55  菜霸  阅读(901)  评论(0)    收藏  举报