重要点 :看注释

从access.log中统计数据

  1. 对healthcheck.html的请求不计入统计
  2. 输出请求总量,以及GET和POST分别的总量
  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();
} }
}

Guava实现 过滤文本,排序,转换内容,分组计数转换map 等等的更多相关文章

  1. 在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView

    背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5中创建GridView>中,我们学习了如何在 ASP.NET MVC 中实现 GridView,类似于 ASP.NET web ...

  2. MixItUp:超炫!基于 CSS3 & jQuery 的过滤和排序插件

    MixItUp 是一款轻量,但功能强大的 jQuery 插件,提供了对分类和有序内容的美丽的动画过滤和排序功能.特别适合用于作品集网站,画廊,图片博客以及任何的分类或有序内容. 它是如何工作的? Mi ...

  3. [转]在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView

    本文转自:http://www.cnblogs.com/powertoolsteam/p/MVC5_GridView_2.html 背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5 ...

  4. Oracle Day2 过滤、排序、单行函数

    1.过滤和排序 SQL> --查询10号部门的所有员工信息 SQL> select * from emp ; 未选定行 SQL> ed SP2: 无法创建保存文件 "afi ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-9  关联实体过滤和排序 问题 你有一实体的实例,你想加载应用了过滤和排序的相关 ...

  6. Oracle01——基本查询、过滤和排序、单行函数、多行函数和多表查询

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7272236.html Oracle的集群 Oracle的体系结构 SQL> --当 ...

  7. 使用 awk 过滤文本或文件中的字符串

    当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干 ...

  8. [Linux] day07——查看及过滤文本

    查看及过滤文本 =====================================cat      concatenate         -n 添加行号------------------- ...

  9. django-rest-framework 基础四 过滤、排序、分页、异常处理

    django-rest-framework 基础四 过滤.排序.分页.异常处理 目录 django-rest-framework 基础四 过滤.排序.分页.异常处理 1. 过滤 1.1 内置过滤类 1 ...

随机推荐

  1. VNC Viewer连接打开remote display的VMware虚拟机出现闪退

    只需修改vnc option里面Advanced-->expert-->ColourLevel的值为“rgb222” or “full”即可. 说明:rgb111--8 colours,r ...

  2. MapReduce案例:统计共同好友+订单表多表合并+求每个订单中最贵的商品

    案例三: 统计共同好友 任务需求: 如下的文本, A:B,C,D,F,E,OB:A,C,E,KC:F,A,D,ID:A,E,F,LE:B,C,D,M,LF:A,B,C,D,E,O,MG:A,C,D,E ...

  3. Java消息机制 ActiveMQ入门实例

    转载自:http://www.cnblogs.com/wyh3721/p/5917316.html 1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/  ...

  4. mac下安装、配置redies

    https://blog.csdn.net/qq_21383435/article/details/80676497 可视化客户端安装(Mac): ruby -e "$(curl -fsSL ...

  5. JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)

    一.函数声明和表达式 函数声明: function test() {}; test();    //运行正常 function test() {}; 函数表达式: var test = functio ...

  6. IIS7 UNC File caching issue

    You have to either choose dir-monitoring and file-change-notification with its drawback of using SMB ...

  7. 黄聪:is_file和file_exists效率比较

    目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...

  8. 【mysql】索引优化记录

    基础知识 Innodb存储引擎 支持行锁 支持事务: Myisam存储引擎 只支持表锁: 不支持事务: 常见索引列表 独立的列 前缀索引(索引选择性) 多列索引(并不是多个单列索引,索引顺序很重要) ...

  9. Python——字符串2.0(实验)(python programming)

    直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改 转载自:https://www.cnblogs.com/wwwwwei/p/104816 ...

  10. android手机截屏、录屏

    1. 手动截屏,通过其他第三方软件发送截图,或者从手机取出截图 2. 使用命令截图,将截图保存到手机,再拉取到电脑 #!/bin/sh #运行 sh screenshot name picName=$ ...