基于 Elasticsearch + kibana 實(shí)現(xiàn) IP 地址分布地圖可視化
1、需求
有一批特定用途(文末揭曉)的 IP 地址。 想通過(guò)地圖形式可視化展示 IP 地址對(duì)應(yīng)的經(jīng)緯度坐標(biāo)的分布。
2、方案探討
第一步:IP 地址轉(zhuǎn)經(jīng)緯度坐標(biāo)。
第二步:經(jīng)緯度坐標(biāo)借助可視化工具(如:echarts)渲染展示。
Elasticsearch 支持 Geo-point、Geo-shape 數(shù)據(jù)類(lèi)型。 Kibana 支持 Coordinate Map(坐標(biāo)圖)、Region Map(區(qū)域地圖)可視化地圖展示。

3、GeoIp processor 介紹
4、導(dǎo)入一條數(shù)據(jù)實(shí)戰(zhàn)一把
4.1 步驟 1:創(chuàng)建預(yù)處理管道
PUT?_ingest/pipeline/geoip_pipeline
{
??"description"?:?"Add?geoip?info",
??"processors"?:?[
????{
??????"geoip"?:?{
????????"field"?:?"ip"
??????}
????}
??]
}
4.2 步驟 2:創(chuàng)建索引
DELETE?niu_20210215
PUT?niu_20210215
{
??"settings":?{
????"index.default_pipeline":?"geoip_pipeline",
????"number_of_shards":?1,
????"number_of_replicas":?0
??},
??"mappings":?{
????"properties":?{
??????"geoip":?{
????????"properties":?{
??????????"location":?{
????????????"type":?"geo_point"
??????????}
????????}
??????},
??????"ip":{
????????"type":"keyword"
??????}
????}
??}
}
靈活:用戶只關(guān)心 bulk 批量寫(xiě)入數(shù)據(jù)。 零寫(xiě)入代碼修改:甚至寫(xiě)入數(shù)據(jù)的代碼一行都不需要改就可以。
4.3 步驟 3:寫(xiě)入一條數(shù)據(jù)
PUT?niu_20210215/_doc/1
{
??"ip":?"8.8.8.8"
}
{
??"niu_20210215"?:?{
????"mappings"?:?{
??????"properties"?:?{
????????"geoip"?:?{
??????????"properties"?:?{
????????????"city_name"?:?{
??????????????"type"?:?"text",
??????????????"fields"?:?{
????????????????"keyword"?:?{
??????????????????"type"?:?"keyword",
??????????????????"ignore_above"?:?256
????????????????}
??????????????}
????????????},
????????????"continent_name"?:?{
??????????????"type"?:?"text",
??????????????"fields"?:?{
????????????????"keyword"?:?{
??????????????????"type"?:?"keyword",
??????????????????"ignore_above"?:?256
????????????????}
??????????????}
????????????},
????????????"country_iso_code"?:?{
??????????????"type"?:?"text",
??????????????"fields"?:?{
????????????????"keyword"?:?{
??????????????????"type"?:?"keyword",
??????????????????"ignore_above"?:?256
????????????????}
??????????????}
????????????},
????????????"location"?:?{
??????????????"type"?:?"geo_point"
????????????},
????????????"region_iso_code"?:?{
??????????????"type"?:?"text",
??????????????"fields"?:?{
????????????????"keyword"?:?{
??????????????????"type"?:?"keyword",
??????????????????"ignore_above"?:?256
????????????????}
??????????????}
????????????},
????????????"region_name"?:?{
??????????????"type"?:?"text",
??????????????"fields"?:?{
????????????????"keyword"?:?{
??????????????????"type"?:?"keyword",
??????????????????"ignore_above"?:?256
????????????????}
??????????????}
????????????}
??????????}
????????},
????????"ip"?:?{
??????????"type"?:?"keyword"
????????}
??????}
????}
??}
}
?"_source"?:?{
??"geoip"?:?{
????"continent_name"?:?"North?America",
????"country_iso_code"?:?"US",
????"location"?:?{
??????"lon"?:?-97.822,
??????"lat"?:?37.751
????}
??},
??"ip"?:?"8.8.8.8"
}
geoip.city_name:城市 geoip.continent_name:大陸名稱(chēng) geoip.country_iso_code:國(guó)家編碼 geoip.location:經(jīng)緯度坐標(biāo),必須是:geo_point 類(lèi)型 geoip.region_iso_code:地域編碼 geoip.region_name:地域名稱(chēng)
比如:所有的默認(rèn)字符串類(lèi)型改成:keyword 類(lèi)型。
4.4 步驟 4:kibana 可視化展示
4.4.1 創(chuàng)建關(guān)聯(lián)索引模板

4.4.2 ?創(chuàng)建坐標(biāo)圖

4.4.3 ?可視化基礎(chǔ)設(shè)置,執(zhí)行后,就能看到可視化結(jié)果。

5、批量導(dǎo)入數(shù)據(jù)后可視化展示
因?yàn)?span style="color: rgb(1, 1, 1);">全局設(shè)置了 default_pipeline,寫(xiě)入數(shù)據(jù)不需要做任何特殊處理了。


6、小結(jié)
基礎(chǔ)方案大家都能想到,有沒(méi)有更簡(jiǎn)單的、更快捷的方式呢?是需要我們考慮的。 本文拋磚引玉,Kibana 新版本的可視化功能更強(qiáng)大,需要學(xué)習(xí)的點(diǎn)還有很多......
參考
評(píng)論
圖片
表情
