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