Guava实现 过滤文本,排序,转换内容,分组计数转换map 等等
重要点 :看注释
从access.log中统计数据
- 对healthcheck.html的请求不计入统计
- 输出请求总量,以及GET和POST分别的总量
- 输出请求最频繁的10个接口及其次数,按次数降序
- 输出每个小时有多少分钟请求数超过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 等等的更多相关文章
- 在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView
背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5中创建GridView>中,我们学习了如何在 ASP.NET MVC 中实现 GridView,类似于 ASP.NET web ...
- MixItUp:超炫!基于 CSS3 & jQuery 的过滤和排序插件
MixItUp 是一款轻量,但功能强大的 jQuery 插件,提供了对分类和有序内容的美丽的动画过滤和排序功能.特别适合用于作品集网站,画廊,图片博客以及任何的分类或有序内容. 它是如何工作的? Mi ...
- [转]在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView
本文转自:http://www.cnblogs.com/powertoolsteam/p/MVC5_GridView_2.html 背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5 ...
- Oracle Day2 过滤、排序、单行函数
1.过滤和排序 SQL> --查询10号部门的所有员工信息 SQL> select * from emp ; 未选定行 SQL> ed SP2: 无法创建保存文件 "afi ...
- 《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-9 关联实体过滤和排序 问题 你有一实体的实例,你想加载应用了过滤和排序的相关 ...
- Oracle01——基本查询、过滤和排序、单行函数、多行函数和多表查询
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7272236.html Oracle的集群 Oracle的体系结构 SQL> --当 ...
- 使用 awk 过滤文本或文件中的字符串
当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干 ...
- [Linux] day07——查看及过滤文本
查看及过滤文本 =====================================cat concatenate -n 添加行号------------------- ...
- django-rest-framework 基础四 过滤、排序、分页、异常处理
django-rest-framework 基础四 过滤.排序.分页.异常处理 目录 django-rest-framework 基础四 过滤.排序.分页.异常处理 1. 过滤 1.1 内置过滤类 1 ...
随机推荐
- httpclient中文乱码
https://blog.csdn.net/teamlet/article/details/8605840
- Fixed-point multiplication (C166 A*B/B)
I want to multiply two fixed point numbers. After the multiplication I have to shift the result so t ...
- UML图类,接口之间的关系
UML图类之间的关系(来自大话设计模式中的一张图,代表了类,接口之间的各种关系)
- go thrift报错问题--WriteStructEnd
问题 go thrift开发过程中,多个goroutine共用一个client时,报错: panic: runtime error: index out of range goroutine 24 [ ...
- TCP加速锐速SS(ServerSpeeder)破解版一键安装
速(serverspeeder),是一款TCP加速程序,能够增强VPS/服务器连接的稳定性,且有效的提高服务器的带宽利用率,进而提高访问速度.老左经常看到论坛.群里有用户提到锐速这款软件可以提高VPS ...
- MySQL分析数据运行状态利器【SHOW PROCESSLIST】
这个博文,将只是简单的记录一下,我们的数据库操作和使用中,加索引加不上去,分析的过程,其实比较简单,就是看有没有连接进程还在操作表.有的话,将其停掉(不影响业务的场景下). 今天的主角是: SHOW ...
- Wsus 清理的计划任务
<# Get-ExecutionPolicy 默认为 RemoteSigned 该签名设置 Set-ExecutionPolicy Unrestricted 添加到排除 powershell - ...
- Quartz不用配置文件配置启动
StdSchedulerFactory schedulerFactory = null; try { schedulerFactory = new StdSchedulerFactory(); Pro ...
- 【转】探索 ConcurrentHashMap 高并发性的实现机制
原文链接:https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/ <探索 ConcurrentHashMap ...
- 【剑指offer】规则二维数组查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路: ...