在之前的文章 “使用 Kibana Timelion 进行时间序列分析”,我已经介绍了 Kibana 中的 Timelion 可视化工具。在今天的文章中,我将使用 Timelion 工具来分析地震数据。这里的地图数据来源于 1994 Northrige earthquake。我们可以在地址 www.ncedc.org/anss/catalog-search.html 里下载到数据。
准备数据
数据来源于 ElasticON tour16 demo。我们可以使用如下的命令来进行下载:
git clone https://github.com/tbragin/elasticon_tour16_demo
接着解压数据集 ncedc-earthquakes-dataset.tar.gz:
tar xzf ncedc-earthquakes-dataset.tar.gz
上面的命令解压出来两个文件:blasts.txt 及 earthquakes.txt。
我们查看文件 ncedc-earthquakes-logstash.conf,并修改 Elasticsearch URL 为自己的 Elasticsearch URL:
ncedc-earthquakes-logstash.conf
input {
stdin { type => "earthquake" }
}
filter {
csv {
columns => ["timestamp","latitude","longitude","depth","mag","magType","nst","gap","dmin","rms","source","event_id"]
convert => {"latitude" => "float"}
convert => {"longitude" => "float"}
convert => {"depth" => "float"}
convert => {"mag" => "float"}
convert => {"dmin" => "float"}
convert => {"rms" => "float"}
convert => {"gap" => "float"}
}
mutate {
add_field => ["location", "%{latitude}, %{longitude}"]
remove_field => ["latitude", "longitude"]
}
date {
match => ["timestamp", "yyyy/MM/dd HH:mm:ss.SS", "ISO8601"]
remove_field => ["timestamp"]
}
}
output {
stdout { codec => rubydebug { metadata => true } }
stdout { codec => dots }
elasticsearch {
hosts => ["localhost:9200"]
user => "elastic"
password => "changeme"
index => "ncedc-earthquakes"
template => "ncedc-earthquakes-template.json"
template_name => "ncedc-earthquakes"
template_overwrite => true
}
}
由于我使用本地部署,在上面我使用 Elasticsearch URL 为 localhost:9200。
我们把 earthquakes.txt, ncedc-earthquakes-logstash.conf 及ncedc-earthquakes-template.json 拷贝 logstash 的安装目录下。
$ pwd
/Users/liuxg/elastic5/logstash-7.9.1
liuxg:logstash-7.9.1 liuxg$ ls
CONTRIBUTORS lib
Gemfile logstash-core
Gemfile.lock logstash-core-plugin-api
LICENSE.txt modules
NOTICE.TXT ncedc-earthquakes-logstash.conf
bin ncedc-earthquakes-template.json
config tools
data vendor
earthquakes.txt x-pack
我们需要把上面的 ncedc-earthquakes-template.json 修改为:
ncedc-earthquakes-template.json
{
"template": "ncedc-earthquakes",
"settings": {
"number_of_shards": 1,
"number_of_replicas" : 0,
"index.refresh_interval": "60s"
},
"mappings": {
"dynamic_templates": [
{
"strings": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "text"
}
}
}
],
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
上面的目的就是为了使得我们创建的 location 字段为 geo_point 数据类型。我们通过如下的命令来把数据导入到 Elasticsearch 中:
cat earthquakes.txt| bin/logstash -f ncedc-earthquakes-logstash.conf
我们可以看到数据被导入到 Elasticsearch 中。在实际的操作中,我们至少需要等1分钟的时间才能在 Kibana 中查看到数据,这是因为我们在 ncedc-earthquakes-template.json 文件中配置 index.refresh_interval 为 60s。
我们在 Kibana 中通过如下的命令来检查最新生产的索引:
GET _cat/indices
我们可以是如下的方式来获取数据的数目:
GET ncedc-earthquakes/_count
{
"count" : 2394927,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
我们接下来创建一个叫做 ncedc-earthquakes 的索引模式。
我们发现这个索引的其中一个数据:
{
"message" : "2009/05/13 22:52:58.10,37.8677,-122.2295,11.00,1.35,Md,24,45,1,0.06,NC,51221532",
"gap" : 45.0,
"host" : "liuxg",
"source" : "NC",
"@timestamp" : "2009-05-13T14:52:58.100Z",
"type" : "earthquake",
"magType" : "Md",
"location" : "37.8677, -122.2295",
"dmin" : 1.0,
"depth" : 11.0,
"@version" : "1",
"mag" : 1.35,
"nst" : "24",
"event_id" : "51221532",
"rms" : 0.06
}
我们可以看到其中有几个重要的字段:@timestamp,depth,mag,location。这几个字段分别表示时间及地震的深度,强度以及位置。我们将使用这些信息来做分析。
使用 Timelion 做数据分析
接下来,我们使用 Timelion 对数据进行可视化。打开 Kibana:
在上面的右边,我们可以看到一个区域叫做 Timelion expression。我们把如下的一个表达式拷贝进去:
.es(index=ncedc-earthquakes,metric=avg:depth).points(fill=5).color(#8cd98c).label('Depth - raw').yaxis(label="Depth"),.es(index=ncedc-earthquakes,metric=avg:depth).lines().movingaverage(52).label('Depth - moving avg').yaxis(label="Depth").color(#257425 ), .es(index=ncedc-earthquakes,metric=avg:mag).points(fill=5).label('Magnitude - raw').yaxis(2, label="Magnitude").color(#ff6666), .es(index=ncedc-earthquakes,metric=avg:mag).lines().movingaverage(52).label('Magnitude - moving avg').yaxis(2, label="Magnitude").color(#b30000 )
那么在过去20年的数据就是这样的:
在上面显示了 Depth 的原始数据已经 Depth - moving avg。同样的我们也展示了 Magnitude 的原始数据已经 Magnitude - moving avg。
同样地,我们把如下的表达是拷贝到 Timelion expression 中:
.es(index=ncedc-earthquakes,metric=avg:mag).points(fill=5).label('raw').yaxis(2, label="Magnitude").color(#ffa3a3), .es(index=ncedc-earthquakes,metric=avg:mag).lines().movingaverage(52).label('5 year moving avg').yaxis(2, label="Magnitude").color(#b30000 ), .es(index=ncedc-earthquakes,metric=avg:mag).lines().movingaverage(10).label('1 year moving avg').yaxis(2, label="Magnitude").color(#ff3333)
如果我们把如下的表达式拷贝到右边的 Timelion expression 中:
.es(index=ncedc-earthquakes,metric=avg:depth).points(fill=5).color(#a3e0a3).label('raw').yaxis(label="Depth"),.es(index=ncedc-earthquakes,metric=avg:depth).lines().movingaverage(52).label('5 year moving avg').yaxis(label="Depth").color(#257425 ), .es(index=ncedc-earthquakes,metric=avg:depth).lines().movingaverage(10).label('1 year moving avg').yaxis(label="Depth").color(#00b359)
那么我们可以看到:
一步一步建立可视化
在上面我们展示了如果使用 Timelion 来对数据可视化。对于一些开发者来说,可能觉得太炫,太晕。在下面,我们来一步一步地展示一个可视化的建立。
打开 Timelion 可视化:
当我们输入 .es(*) 时,它显示的是所有索引的数据展示。如上所示,它显示的是所有文档在当前 time picker 中数据数量。
我们接下来想对地震数据索引 ncedc-earthquakes 来进行展示,我们同时想知道 mag 的最大值的指标统计:
.es(index=ncedc-earthquakes,metric=max:mag)
上面展示了地震强度的最大值的时间系列。
我们接着想知道强度一年内的 moving average。为此,我们添加另外一个时间系列:
.es(index=ncedc-earthquakes,metric=max:mag),.es(index=ncedc-earthquakes,metric=max:mag).mvavg(10)
接下来,我希望原始的 max 指标数据用点来表示,以示和 mvavg 的区别:
.es(index=ncedc-earthquakes,metric=max:mag).points(),.es(index=ncedc-earthquakes,metric=max:mag).mvavg(10)
也许你对上面的颜色并不满意,我们尝试修改上面的点击线的颜色:
.es(index=ncedc-earthquakes,metric=max:mag).points().color(blue),.es(index=ncedc-earthquakes,metric=max:mag).mvavg(10).color(orange)
我们接下来点击 Save 按钮把当前的 Timelion 时序图保存为一个可视图
我们保存上面的视图为 Eearthquake - Demo。这个时态可以在一下的 Dashboard 中被使用,并成为 Dashboard 的一部分。
创建 Maps 可视化
在地震的数据中,我们也同时注意到有一个叫做 location 的字段。我们可以利用这个字段来展示所有的地址数据所在的位置:
从上面,我们可以看到世界各地的地图的数据显示在地图上。我们把这个视图保存为 Eearthquake - Maps。
创建 Eearthquake Dashboard
我们可以根据上面的 Timelion 已经 Maps 地图两个可视化图,创建一个叫做 Eearquake 的 Dashboard:
我们点击上面的两个可视化图,把它们添加到 Dashboard 中:
点击上面的 Save 链接,并保存为 Eearthquake:
这样我们的 Eearthquake Dashboard 就基本已经创建成功了。
接下来,我们最感兴趣的还是在美国加州地区的数据,从上面的图中我们可以看出来那里的数据是最多的。把地图的 zoom 调整到哦合适的级别,并把时间调整到地震发生的那个时间段。我们可以从网址 https://en.wikipedia.org/wiki/1994_Northridge_earthquake 找到当时发生地震时的时间是 January 17, 1994, at 4:30:55 a.m。我们把数据调整到这个时间段:
在 Maps 中,我们可以直接通过在地图上选择一个范围来过滤数据。上面的 Timelion 中显示的还是整个在那个时间段的世界各地的数据的总和。我们可以通过如下的方式来过滤数据,并只显示在加州的数据:
在地图上使用矩形来显示想要范围里的数据:
从上面,我们可以定义我们想要的区域进行过滤:
从上面的图中,我们可以看到一个区域定义的过滤器已经生成。在上面的 Timelion 中的数据就是只显示在上面矩形显示框里的数据。从上面的图中,我们可以清楚地看到在1994年1月17日,有一个清晰的强度的变化,表明一个地震正在发生。