白话Elasticsearch73_ES生产集群中的索引管理01
文章目錄
- 概述
- 官方指導
- 1、創建索引
- (1)創建索引的語法
- (2)索引創建返回消息的解釋
- 2、刪除索引
- 3、查詢索引設置信息
- 4、 Index 是否存在
- 5、打開/關閉索引
- 5、壓縮索引
- 6、rollover index
概述
繼續跟中華石杉老師學習ES,第73篇
課程地址: https://www.roncoo.com/view/55
官方指導
Index APIs: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html
1、創建索引
(1)創建索引的語法
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
用settings給這個索引在創建時可以添加一些設置,還有可以初始化一些type的mapping
curl -XPUT 'http://elasticsearch02:9200/twitter?pretty' -d ' {"settings" : {"index" : {"number_of_shards" : 3, "number_of_replicas" : 2 }},"mappings" : {"type1" : {"properties" : {"field1" : { "type" : "text" }}}} }'(2)索引創建返回消息的解釋
默認情況下,索引創建命令會在每個primary shard的副本開始進行復制以后,或者是請求超時以后,返回一個響應消息. 類似如下:
{"acknowledged": true,"shards_acknowledged": true }其中
- acknowledged表明了這個索引有沒有創建成功,
- shards_acknowledged表明了每個primary shard有沒有足夠數量的replica開始進行復制了。
有可能這兩個參數會為false,但是索引依然可以創建成功。因為這些參數僅僅是表明在請求超時之前,那兩個行為有沒有成功,也有可能請求超時了,在超時前都沒成功,但是超時后在es server端還是都執行了。
-
如果acknoledged是false,那么就可能是超時了,此時接受到響應消息的時候,cluster state都還沒變更,沒有加入新創建的index,但是也許之后還是會創建這個index。
-
如果shards_acknowledged是false,那么可能在primary shard進行副本copy之前,就timeout了,但是此時也許index創建成功了,而且cluster state已經加入了新創建的index。
2、刪除索引
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
curl -XDELETE 'http://elasticsearch02:9200/twitter?pretty'3、查詢索引設置信息
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html
curl -XGET 'http://elasticsearch02:9200/twitter?pretty'4、 Index 是否存在
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
5、打開/關閉索引
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-close.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
curl -XPOST 'http://elasticsearch02:9200/twitter/_close?pretty' curl -XPOST 'http://elasticsearch02:9200/twitter/_open?pretty'curl -XPUT 'http://elasticsearch02:9200/twitter/type1/1?pretty' -d ' {"field1": "1" }'如果關閉了一個索引之后,那么這個索引是不會帶來任何的性能開銷了,只要保留這個索引的元數據即可,然后對這個索引的讀寫操作都不會成功。
一個關閉的索引可以接著再打開,打開以后會進行shard recovery過程。
比如說你在做一些運維操作的時候,現在你要對某一個索引做一些配置,運維操作,修改一些設置,關閉索引,不允許寫入,成功以后再打開索引
5、壓縮索引
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html
shrink命令可以將一個已有的索引壓縮成一個新的索引,同時primary shard會更少。因為以前提到過,primary shard因為涉及到document的hash路由問題,所以是不允許修改的。
但是如果要減少index的primary shard,可以用shrink命令來壓縮index。但是壓縮后的shard數量必須可以被原來的shard數量整除。
舉例來說,一個有8個primary shard的index可以被壓縮成4個,2個,或者1個primary shard的index。
壓縮索引,是這樣啊,如果你的索引中本來比如是要保留7天的數據,那么給了10個shard,但是現在需求變了,這個索引只要保留3天的數據就可以了,那么數據量變小了,就不需要10個shard了,就可以做shrink操作,5個shard。
shrink命令的工作流程如下:
- (1)首先,它會創建一個跟source index的定義一樣的target
index,但是唯一的變化就是primary shard變成了指定的數量 - (2)接著它會將source index的segment file直接用hard-link的方式連接到target index的segment file,如果操作系統不支持hard-link,那么就會將source index的segment file都拷貝到target index的data dir中,會很耗時。如果用hard-link會很快
- (3)最后,會將target index進行shard recovery恢復
如果要shrink index,那么這個index必須先被標記為read only,而且這個index的每個shard的某一個copy,可以是primary或者是replica,都必須被復制到一個節點上去。默認情況下,index的每個shard有可能在不同機器上的,比如說,index有5個shard,shard0和shard1在機器1上,shard2、shard3在機器2上,shard4在機器3上。現在還得把shard0,shard1,shard2,shard3,shard4全部拷貝到一個同一個機器上去,但是可以是shard0的replica shard。而且每個primary shard都必須存在。可以通過下面的命令來完成。其中index.routing.allocation.require._name必須是某個node的名稱,這個都是可以自己設置的。
curl -XPUT 'http://elasticsearch02:9200/twitter/_settings?pretty' -d ' {"settings": {"index.routing.allocation.require._name": "node-elasticsearch-02", "index.blocks.write": true } }'這個命令會花費一點時間將source index每個shard的一個copy都復制到指定的node上去,可以通過GET _cat/recovery?v命令來追蹤這個過程的進度。
等上面的shard copy relocate過程結束之后,就可以shrink一個index,用下面的命令即可:POST my_source_index/_shrink/my_target_index。如果target index被添加進了cluster state之后,這個命令就會立即返回,不是等待shrink過程完成之后才返回的。當然還可以用下面的命令來shrink的時候修改target index的設置,在settings里就可以設置target index的primary shard的數量。
curl -XPOST 'http://elasticsearch02:9200/twitter/_shrink/twitter_shrinked?pretty' -d ' {"settings": {"index.number_of_replicas": 1,"index.number_of_shards": 1, "index.codec": "best_compression" } }'當然也是需要監控整個shrink的過程的,用GET _cat/recovery?v即可。
6、rollover index
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html
rollover命令可以將一個alias重置到一個新的索引上去,如果已經存在的index被認為太大或者數據太舊了。這個命令可以接收一個alias名稱,還有一系列的condition。如果索引滿足了condition,那么就會創建一個新的index,同時alias會指向那個新的index。
比如下面的命令。舉例來說,有一個logs-0000001索引,給了一個別名是logs_write,接著發起了一個rollover的命令,如果logs_write別名之前指向的那個index,也就是logs-0000001,創建了超過7天,或者里面的document已經超過了1000個了,然后就會創建一個logs-000002的索引,同時logs_write別名會指向新的索引。
這個命令其實是很有用的,特別是針對這種用戶訪問行為日志的數據,或者是一些聯機事務系統的數據的進入,你可以寫一個shell腳本,每天0:00的時候就執行以下rollover命令,此時就判斷,如果說之前的索引已經存在了超過1天了,那么此時就創建一個新的索引出來,同時將別名指向新的索引。自動去滾動創建新的索引,保持每個索引就只有一個小時,一天,七天,三天,一周,一個月。
類似用es來做日志平臺,就可能分布式電商平臺,可能訂單系統的日志,單獨的一個索引,要求的是保留最近3天的日志就可以了。交易系統的日志,是單獨的一個索引,要求的是保留最近30天的日志。
curl -XPUT 'http://elasticsearch02:9200/logs-000001?pretty' -d ' {"aliases": {"logs_write": {}} }'Add > 1000 documents to logs-000001
curl -XPUT 'http://elasticsearch02:9200/logs-000001/data/1?pretty' -d ' {"userid": 1,"page": 1 }' curl -XPUT 'http://elasticsearch02:9200/logs-000001/data/2?pretty' -d ' {"userid": 2,"page": 2 }' curl -XPUT 'http://elasticsearch02:9200/logs-000001/data/3?pretty' -d ' {"userid": 3,"page": 3 }'curl -XPOST 'http://elasticsearch02:9200/logs_write/_rollover?pretty' -d ' {"conditions": {"max_age": "1d","max_docs": 3} }'{"acknowledged": true,"shards_acknowledged": true,"old_index": "logs-000001","new_index": "logs-000002","rolled_over": true, "dry_run": false, "conditions": { "[max_age: 7d]": false,"[max_docs: 1000]": true} }這個過程常見于網站用戶行為日志數據,比如按天來自動切分索引,寫個腳本定時去執行rollover,就會自動不斷創建新的索引,但是別名永遠是一個,對于外部的使用者來說,用的都是最新數據的索引。
舉一個簡單的例子,這塊是怎么玩兒的,比如說用es做網站的實時用戶行為分析,要求的是一個索引只要保留當日的數據就可以了,那么就可以用這個rollover的策略,確保每個索引都是包含當日的最新數據的。老的數據,就變成別的索引了,此時可以寫一個shell腳本,刪除舊的數據,這樣的話,es里就保留當前最新的數據就可以了。也可以根據你的需求,就保留最近7天的數據,但是最新一天的數據在一個索引中,供分析查詢使用。
默認情況下,如果已經存在的那個索引是用-符號加上一個數字結尾的,比如說logs-000001,那么新索引的名稱就會是自動給那個數字加1,比如logs-000002,自動就是給一個6位的數字,而且會自動補零。但是我們也可以自己指定要的新的索引名稱,比如下面這樣:
POST /my_alias/_rollover/my_new_index_name {"conditions": {"max_age": "7d","max_docs": 1000} }可以將rollover命令和date日期結合起來使用,比如下面的例子,先創建了一個logs-2016.10.31-1格式的索引。接著每次如果成功rollover了,那么如果是在當天rollover了多次,那就是當天的日期,末尾的數字遞增。如果是隔天才rollover,會自動變更日期,同時維護末尾的數字序號。
PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E {"aliases": {"logs_write": {}} }PUT logs_write/log/1 {"message": "a dummy log" }POST logs_write/_refreshWait for a day to pass
POST /logs_write/_rollover {"conditions": {"max_docs": "1"} }當然,還可以在rollover的時候,給新的index進行新的設置:
POST /logs_write/_rollover {"conditions" : {"max_age": "7d","max_docs": 1000},"settings": {"index.number_of_shards": 2} }總結
以上是生活随笔為你收集整理的白话Elasticsearch73_ES生产集群中的索引管理01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch72_利用
- 下一篇: 白话Elasticsearch73_ES