ELK 使用小技巧(第 3 期)
ELK Tips 主要介紹一些 ELK 使用過程中的小技巧,內容主要來源為 Elastic 中文社群。
一、Logstash
1、Filebeat 設定多個 output
在 6.0 之前,Filebeat 可以設定多個輸出(必須是不同型別的輸出);從 6.0 開始已經禁止多輸出了,只能擁有一個輸出,如果想實現多輸出,可以藉助 logstash 等中間元件進行輸出分發。
二、Elasticsearch
1、ES 使用者佔用的記憶體大於為 ES 設定的 heapsize
ES 是 Java 應用,底層儲存引擎是基於 Lucene 的,heapsize 設定的是 Java 應用的記憶體;而 Lucene 建立倒排索引(Inverted Index)是先在記憶體裡生成,然後定期以段檔案(segment file)的形式刷到磁碟的,因此 Lucene 也會佔用一部分記憶體。
ofollow,noindex">elasticsearch.cn/article/32
2、ES 使用別名插入資料
ES 可以通過索引的方式向索引插入資料,但是同時只能有一個索引可以被寫入,而且需要手動設定,未設定的情況下會報錯: no write index is defined for alias [xxxx] , The write index may be explicitly disabled using is_write_index=false
or the alias points to multiple indices without one being designated as a write index。
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias1", "is_write_index" : true } } ] } 複製程式碼
3、ES 設定 G1 垃圾回收
修改 jvm.options
檔案,將下面幾行:
-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly 複製程式碼
改為
-XX:+UseG1GC -XX:MaxGCPauseMillis=50 複製程式碼
即可。
其中 -XX:MaxGCPauseMillis
是控制預期的最高 GC 時長,預設值為 200ms,如果線上業務特性對於 GC 停頓非常敏感,可以適當設定低一些。但是這個值如果設定過小,可能會帶來比較高的 cpu 消耗。
4、ES 和 Zipkin 整合時設定驗證資訊
java -DKAFKA_ZOOKEEPER=10.14.123.117:2181 -DSTORAGE_TYPE=elasticsearch -DES_HOSTS=http://10.14.125.5:9200 ES_USERNAME=xxx ES_PASSWORD=xxx -jar zipkin.jar 複製程式碼
5、ES 叢集部署報錯
問題 1 報錯資訊如下:
Received message from unsupported version:[2.0.0] minimal compatible version is:[5.6.0] 複製程式碼
經排查是叢集中存在低版本的 ES 例項,將低版本例項移除即可。
問題 2 報錯資訊如下:
with the same id but is a different node instance 複製程式碼
刪除對應節點 elsticsearch 資料夾下的 data 資料夾下的節點資料即可。
6、海量中文分詞外掛
海量分詞是天津海量資訊科技股份有限公司自主研發的中文分詞核心,經測試分詞效果還是不錯的,值得一試。
7、查詢一個索引下的所有 type 名
通過下面的 API,即可獲取全部的 type,下面的例子中 doc 就是 indexName 索引下的一個 type:
GET http://es127.0.0.1:9200/indexName/_mappings ----------------------------------------------- { indexName: - { mappings: - { doc: - { _all: + {... }, dynamic_date_formats: + [... ], dynamic_templates: + [... ], properties: + {... } } } } } 複製程式碼
8、索引模板中根據欄位值設定別名
設定索引模板的時候,別名可以使用 Query 條件來進行匹配。
PUT _template/template_1 { "index_patterns" : ["te*"], "settings" : { "number_of_shards" : 1 }, "aliases" : { "alias2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" }, "{index}-alias" : {} } } 複製程式碼
9、索引模板設定預設時間匹配格式
ES 預設是不會將 yyyy-MM-dd HH:mm:ss 識別為時間的,可以通過在索引模板進行如下設定實現多種時間格式的識別:
"mappings": { "doc": { "dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"], 複製程式碼
10、ES 中 Merge 相關設定
Merge 是非常耗費 CPU 的操作;而且如果不是 SSD 的話,推薦將 index.merge.scheduler.max_thread_count 設定為 1;否則 ES 會啟動 Math.min(3, Runtime.getRuntime().availableProcessors() / 2) 個執行緒進行 Merge 操作;這樣大部分機械硬碟的磁碟 IO 都很難承受,就可能出現阻塞。
"index": { "refresh_interval": "5s", "number_of_shards": "3", "max_result_window": 10000, "translog": { "flush_threshold_size": "500mb", "sync_interval": "30s", "durability": "async" }, "merge": { "scheduler": { "max_merge_count": "100", "max_thread_count": "1" } }, 複製程式碼
11、mapping 中 enabled store index 引數
- enabled:預設是true,只用於 mapping 中的 object 欄位型別;當設定為 false 時,其作用是使 es 不去解析該欄位,並且該欄位不能被查詢和 store,只有在 source 中才能看到,設定 enabled 為 false,可以不設定欄位型別,預設型別為 object;
- store:預設 false,store 引數的功能和 source 有一些相似,我們的資料預設都會在 source 中存在,但我們也可以將資料 store 起來;當我們使用
copy_to
引數時,copy_to
的目標欄位並不會在 source 中儲存,此時 store 就派上用場了; - index:預設是 true,當設定為 false,表明該欄位不能被查詢,如果查詢會報錯。
12、ES 圖片搜尋
- 可以藉助區域性敏感 LSH 或者 pHash 來實現: stackoverflow.com/questions/3…
- Github 也有一個開源專案使用了多種 Hash 演算法藉助 ES 來實現圖片搜尋: github.com/usc-isi-i2/…
13、Term 聚合根據子聚合結果排序
GET /_search { "aggs" : { "genres" : { "terms" : { "field" : "genre", "order" : { "playback_stats.max" : "desc" } }, "aggs" : { "playback_stats" : { "stats" : { "field" : "play_count" } } } } } } 複製程式碼
三、社群文章精選
- ET007 ElasticStack 6.5 介紹
- CentOS 7.4 下安裝 ES 6.5.1 搜尋叢集
- Elastic Stack v6.5 新特性解讀
- Elasticsearch 史上最全最常用工具清單
Any Code,Code Any!
掃碼關注『AnyCode』,程式設計路上,一起前行。
