Elasticsearch 统计代码例子
aggs
avg 平均数
最近15分钟的平均访问时间,upstream_time_ms是每次访问时间,单位毫秒
{
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
//当然你也可以直接将过滤器写在aggs里面
{
"size": 0,
"aggs": {
"filtered_aggs": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
},
"aggs": {
"execute_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}
cardinality 基数,比如计算uv
你可能注意到了size:0,如果你只需要统计数据,不要数据本身,就设置它,这不是我投机取巧,官方文档也是这么干的。
{
"size": 0,
"aggs": {
"filtered_aggs": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
},
"aggs": {
"ipv": {
"cardinality": {
"field": "ip"
}
}
}
}
}
}
percentiles 基于百分比统计
最近15分钟,99.9的请求的执行时间不超过多少
{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"percentiles": {
"field": "upstream_time_ms",
"percents": [
90,
95,
99.9
]
}
}
}
}
//返回值,0.1%的请求超过了159ms
{
"took": 620,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 679400,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"values": {
"90.0": 24.727003484320534,
"95.0": 72.6200981699678,
"99.9": 159.01065773524886 //99.9的数据落在159以内,是系统计算出来159
}
}
}
}
percentile_ranks 指定一个范围,有多少数据落在这里
{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"percentile_ranks": {
"field": "upstream_time_ms",
"values": [
50,
160
]
}
}
}
}
//返回值
{
"took": 666,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 681014,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"values": {
"50.0": 94.14716385885366,
"160.0": 99.91130872493076 //99.9的数据落在了160以内,这次,160是我指定的,系统计算出99.9
}
}
}
}
统计最近15分钟,不同的链接请求时间大小
{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"terms": {
"field": "uri"
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}
//返回,看起来url1 比 url2慢一点(avg_time),不过url1的请求量比较大 (doc_count)
{
"took": 1655,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 710802,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"doc_count_error_upper_bound": 10,
"sum_other_doc_count": 347175,
"buckets": [
{
"key": "/url1",
"doc_count": 362688,
"avg_time": {
"value": 6.601660380271749
}
},
{
"key": "/url2",
"doc_count": 939,
"avg_time": {
"value": 5.313099041533547
}
}
]
}
}
}
找出url响应最慢的前2名
{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"terms": {
"size": 2,
"field": "uri",
"order": {
"avg_time": "desc"
}
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}
//返回值
{
"took": 1622,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 748712,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"doc_count_error_upper_bound": -1,
"sum_other_doc_count": 748710,
"buckets": [
{
"key": "url_shit",
"doc_count": 123,
"avg_time": {
"value": 8884
}
},
{
"key": "url_shit2",
"doc_count": 456,
"avg_time": {
"value": 8588
}
}
]
}
}
}
value_count 文档数量
相当于
select count(*) from table group by uri,为了达到这个目的,只需要把上文中,avg 换成value_count。不过avg的时候,结果中的doc_count其实达到了同样效果。
怎么取数据画个图?比如:最近2分钟,每20秒的时间窗口中,平均响应时间是多少
{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-2m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"date_histogram": {
"field": "@timestamp",
"interval": "20s"
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}
pv 分时统计图(每小时一统计)
周期大小对性能影响不大
{
"size":0,
"fields":false,
"aggs": {
"execute_time": {
"date_histogram": {
"field": "@timestamp",
"interval": "1h"
}
}
}
}
Elasticsearch 统计代码例子的更多相关文章
- Linux下统计代码行数
使用wc统计代码行数 最近写了一些代码,想统计一下代码的行数,在eclipse中好像没这功能,网上搜了一下才发现原来Linux有一个统计文件行数的命令wc.使用wc可以打印出每个文件和总文件的行数.字 ...
- VS2010统计代码行数 [转]
按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容: ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头 和 /开头 或者 空行 都不计入代 ...
- Eclipse统计代码行数
开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...
- Google Analytics统计代码GA.JS中文教程
2010-12-06 11:07:08| 分类: java编程 | 标签:google analytics ga js 代码 |举报|字号 订阅 Google Analytics ...
- Visual Studio VS2010统计代码行数(转载)
本文转自:http://blog.csdn.net/zhouworld16/article/details/9292851 在网上看到别人用的方法: 按CTRL+SHIFT+F (Find in fi ...
- 30个php操作redis常用方法代码例子
From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...
- 如何给WordPress安装百度统计代码
1.注册并登录百度统计,点击页面顶部的“网站中心”,然后点击右上角“+ 新增网站”,填写网站域名确定后,点击“复制代码”:2.登录 WordPress 后台,点击左侧导航栏“外观”里的“编辑”,然后点 ...
- 在Flash Builder或者Eclipse统计代码行数的方法
在Flash Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件
- Python 统计代码行
正在学习 Python, 做了个统计代码行的功能, 参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能, 只是windows下测试了,而且代码文件编码形式是 utf-8的. 如果使用其它编码形式的 ...
随机推荐
- HDFS的Write过程
hadoop中重要的组成部分HDFS,它所发挥的重要作用是进行文件的后端存储.HDFS针对的是低端的服务器,场景为读操作多.写操作少的情况.在分布式存储情况下,比较容易出现的情况是数据的损害,为了保证 ...
- Token生成(转载)
package main import ( "encoding/base64" "encoding/json" "log" "ne ...
- Difftime
功 能:返回两个time_t型变量之间的时间间隔,即 计算两个时刻之间的时间差. 用 法: double difftime(time_t time2, time_t time1);
- #leetcode刷题之路23-合并K个排序链表
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例:输入:[ 1->4->5, 1->3->4, 2->6]输出: 1->1->2- ...
- 【学时总结】 ◆学时·III◆ 二分图
[学时·III] 二分图 ■基本策略■ 其实本质是图论中的网络流 二分图是两个由多个点组成的集合(上部和下部,且没有重叠),两个集合中的点不与该集合内其他的点连通,但和另一个集合内的点连通.我们称这两 ...
- Google Cloud Platform 续
Google Cloud Platform 创建新实例 地区:australia-southeast1-a 机器类型:1个vCPU n1-standard-1 系统:Ubuntu 16.04 LTS ...
- Ubuntu如何挂载U盘
1. 以root用户登陆 2. 查看当前挂载 fdisk -l 一般情况未挂载的硬盘都在最后,这里是/dev/sdb1 3.新建一个目录来挂载硬盘 挂载到MNT/usb root@h-Default- ...
- 关于alias同时使用多个命令小记。
为了方便自己快速使用命令,接触了一下alias.在使用多个命令时,一般有两种方式.各有不同 根据需求选择. 直接上代码: 1.alias Name='(date;pwd;cmd1;cmd2) 写法简洁 ...
- jquery购物车添加功能
<html> <head> <meta charset="UTF-8"> <title></title> <scr ...
- Redis集群入门
官方文章: https://redis.io/topics/cluster-tutorial#redis-cluster-configuration-parameters 本文永久地址: https: ...