本文主要是通過Elastic官網提供的三個資料集,導入到Elasticsearch5.6.1,并通過Kibana5.6.1展示資料和查詢資料。
關于Elasticsearch5.6.1的環境搭建,請參考我另一篇博文。
ElasticSearch5.6.1環境搭建與運作
http://blog.csdn.net/deliciousion/article/details/78055724
有關ElasticSearch的可視化工具Kibana介紹與安裝,請參閱我的另一篇博文。
Kibana5.6.1的環境塔建與運作
http://blog.csdn.net/deliciousion/article/details/78057459
下面三個位址可以分别擷取到三個資料集:accounts.json、shakespeare.json、logs.jsonl(logs.jsonl.gz解壓得到)
accounts.json
https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json
shakespeare.json
https://download.elastic.co/demos/kibana/gettingstarted/shakespeare.json
logs.jsonl
https://download.elastic.co/demos/kibana/gettingstarted/logs.jsonl.gz
這三個檔案中的資料實際上都是JSON格式的資料,也是Elasticsearch完美支援的資料格式。
下面accounts.json裡内容的冰山一角,這個就是标準是JSON格式資料。
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"[email protected]","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
本文主要是從下面幾個方面進行示範。
1.三個資料集的導入。
2.檢視資料集。
1.三個資料集的導入。
導入accounts.json
下面是accounts.json每條資料的資料結構
{
"account_number": INT,
"balance": INT,
"firstname": "String",
"lastname": "String",
"age": INT,
"gender": "M or F",
"address": "String",
"employer": "String",
"email": "String",
"city": "String",
"state": "String"
}
利用curl的POST請求方法加上--data-binary選項,表示帶二進制資料檔案的方式,這個選項後面跟的是 @+檔案名,該檔案就放在指令運作的目前目錄。我們還可以觀察到請求URL中的後面部分bank/account/_bulk
bank是索引名稱,account是類型名,_bulk表示批量導入。
genfumihiros-MacBook-Air:data_set smallruan$ pwd
/Users/smallruan/ELK/data_set
genfumihiros-MacBook-Air:data_set smallruan$ ls
accounts.json logs.jsonl shakespeare.json
genfumihiros-MacBook-Air:data_set smallruan$ curl -X POST http://127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @accounts.json
運作批量導入的指令後,傳回下面資訊。
took表示導入總共用的時間,機關是毫秒,是以本次導入共用時2214毫秒,相當于2秒左右。
errors表示是否有錯誤,false表示沒有錯誤。
items裡面的就是一條一條的資料導入資訊。
對于每一條導入的資料都能清晰的看到具體導入的資訊。
_index導入的索引。
_type導入的類型
_id導入的文檔id
_version版本号,每更新一次,版本号加1
result表明資料操作的結果,這裡的結果為created表示建立。
_shards資料的分片情況
created表明是否為建立資料,true表示這一條資料是建立的。
status整個操作的狀态值,201表示正常。
{
"took" : 2214,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "bank",
"_type" : "account",
"_id" : "6",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "bank",
"_type" : "account",
"_id" : "13",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "bank",
"_type" : "account",
"_id" : "18",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "bank",
"_type" : "account",
"_id" : "20",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
(....内容太多此處省略)
}
導入shakespeare.json
下面是shakespeare.json第條資料的資料結構
{
"line_id": INT,
"play_name": "String",
"speech_number": INT,
"line_number": "String",
"speaker": "String",
"text_entry": "String",
}
導入這個資料之前我們先針對這些資料建一個mapping映射關系,account.json為什麼不建呢,因為讓它用預設的方式去建了,系統會根據資料的格式,自動建映射關系。但這樣不一定會滿足你的需求,比如我們要用int,系統預設可能用long。下面先為shakespeare.json的資料建mapping映射。
curl
curl -XPUT 'http://127.0.0.1:9200/shakespeare?pretty' -H 'Content-Type: application/json' -d'
{
"mappings" : {
"_default_" : {
"properties" : {
"speaker" : {"type": "keyword" },
"play_name" : {"type": "keyword" },
"line_id" : { "type" : "integer" },
"speech_number" : { "type" : "integer" }
}
}
}
}
'
或者在kibana的Dev tools下的Console中輸入
PUT /shakespeare
{
"mappings" : {
"_default_" : {
"properties" : {
"speaker" : {"type": "keyword" },
"play_name" : {"type": "keyword" },
"line_id" : { "type" : "integer" },
"speech_number" : { "type" : "integer" }
}
}
}
}
建構好索引的映射後,我們再導入,使用類似的導入指令。如下
genfumihiros-MacBook-Air:data_set smallruan$ pwd
/Users/smallruan/ELK/data_set
genfumihiros-MacBook-Air:data_set smallruan$ ls
accounts.json logs.jsonl shakespeare.json
genfumihiros-MacBook-Air:data_set smallruan$ curl -H 'Content-Type: application/x-ndjson' -XPOST 'http://127.0.0.1:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json
傳回資訊如下,這個資料集很大,單單是shakespeare.json這個純文字檔案就已經20多兆了,是以我的機器導入要26秒,共導入10多萬條資料。
{
"took" : 26225,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "shakespeare",
"_type" : "act",
"_id" : "0",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "shakespeare",
"_type" : "scene",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "shakespeare",
"_type" : "line",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
(......此處省略十多萬條資料)
{
"index" : {
"_index" : "shakespeare",
"_type" : "line",
"_id" : "111395",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
}
]
}
導入最後一個資料檔案logs.jsonl。同樣也是先建mapping映射,導入資料,因為該資料含有地圖的坐标資料,我們需要用特定的資料格式進行存放。
logs.jsonl中的資料結構如下:
{
"memory": INT,
"geo.coordinates": "geo_point"
"@timestamp": "date"
}
根據日期建立三個mapping映射。也相當于建立了三個索引。
curl
curl -XPUT 'http://127.0.0.1:9200/logstash-2015.05.18?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
'
curl -XPUT 'http://127.0.0.1:9200/logstash-2015.05.19?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
'
curl -XPUT 'http://127.0.0.1:9200/logstash-2015.05.20?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
'
Kibana
PUT /logstash-2015.05.18
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
PUT /logstash-2015.05.19
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
PUT /logstash-2015.05.20
{
"mappings": {
"log": {
"properties": {
"geo": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
}
建立mapping後,導入資料。
genfumihiros-MacBook-Air:data_set smallruan$ pwd
/Users/smallruan/ELK/data_set
genfumihiros-MacBook-Air:data_set smallruan$ ls
accounts.json logs.jsonl shakespeare.json
genfumihiros-MacBook-Air:data_set smallruan$ curl -H 'Content-Type: application/x-ndjson' -XPOST 'http://127.0.0.1:9200/_bulk?pretty' --data-binary @logs.jsonl
傳回資訊如下。導入這個資料用了更多的時間。
{
"took" : 37730,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "logstash-2015.05.18",
"_type" : "log",
"_id" : "AV7Lp_3zSxwd4khFwKvg",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "logstash-2015.05.18",
"_type" : "log",
"_id" : "AV7Lp_3zSxwd4khFwKvh",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
(後面省略)
2.檢視資料集
導入資料後,我們可以通過下面的URL簡單的檢視下剛才添加的索引以及導入資料的情況。
http://127.0.0.1:9200/_cat/indices?v
通過URL可以看到剛剛添加5個索引,通過上面的資訊,我們知道。
account.json檔案導入了1000條資料。
shakespeare.json檔案導入了111396條資料。
log.jsonl檔案分别導入3個索引,4000多條資料。
通過kibana檢視資料
在kibana中檢視資料要定義索引模式,具體操作如下。
通過5601端口進入kibana首頁 http://127.0.0.1:5601,進入左側菜單中的Management管理頁
在Index pattern中輸入bank,再點選Create建立索引模式。
建立索引後,會在Index Patterns清單中有剛剛添加的bank索引,然後右邊會有bank的字段資訊。包括字段名,字段類型等資訊。
以同樣的方式添加shakespeare索引。
最後,以通配符的方式一次添加logstash-2015.05.18、logstash-2015.05.19、logstash-2015.05.20三個索引。
添加完之後,我們從左邊菜單的Discover中檢視資料,如下圖所示,預設為顯示第一個索引的資料,下面看到的是bank索引的資料,左上角可以看到資料的條數,1000hits,表示有1000條資料。
上圖紅框點開後,是個清單,可以檢視其它索引的資料,比如下面是shakespeare索引的資料。
從左上角處可以看到shakespeare索引的文檔數為111396。
通過,索引清單,點選logstash-2015.05* 這個索引,我們會發現沒有資料,再觀察右上角,會發現有個last 15 minutes,原因是我們建立索引模式的時候,是以時間為基礎建立的,這個與bank和shakespeare兩個索引不一樣,這個索引還選了一個時間戳作為參考,讀者可自行回顧。
而我們添加的資料集是2015年5月18-20日的資料,查找15分鐘内的資料,肯定沒有結果,是以我們要調一下時間,把那個時間段的資料顯示出來,具體操作如下。
時間設定好,我們就可以看到2015年5月18日到20日的資料了,在實際應用場景中,我們經常會使用時間戳這種索引模式來檢視日志資訊。
3.資料的查詢與篩選
從新打開bank索引的資料Discover頁面,找到頂部的搜尋欄。在搜尋欄中輸入下面的搜尋語句。然後點選右邊的搜尋按鈕。
account_number:<100 AND balance:>47500
bank索引的資料,對應的是一些銀行帳戶資訊,比如帳号account_number,賬戶餘額balance,姓firstname,名lastname,年齡age,城市city等資訊。
上面的查詢語句表示查詢賬号小于100而已賬戶餘額大于47500的資訊。
傳回結果表示,有5個資訊命中(5 hits)。
這些資料資訊,我們并不是每個字段都需要檢視的,這個時候我們可以對字段進行篩選,這個與資料庫的select意思差不多。具體操作如下。
把滑鼠指向Avaliable Fields可用字段中的任意一個字段,比如下面的account_number字段,該字段會顯示一個add添加按鈕,點選add按鈕,即可篩選。
篩選後,右邊就隻會顯示篩選的列。
同理,我們篩選多個列,比如下面的account_number、address、balance、city、firstname、lastname、gender、age。
在左邊的清單,我們還可以看到左邊的清單,Seleted Fields顯示已經篩選的字段,Avaliabe Fields顯示可篩選但未被篩選的字段。
本次示範,從資料導入到ES,到在kibana中顯示、查詢、并篩選資料,主要了解es與kibana結合可以做什麼,後續還會有kibana可視化圖的示範。也是根據這三個資料集。
本次示範比較複雜,過程很多,如果有什麼疑問,請進行提出,大家一起探讨,也歡迎大家批評指正。
有什麼想法的請留言,大家一起交流。