上一章節我們對ES的集群進行了搭建,有興趣的朋友可以參考一下elasticSearch核心概念的介紹(十三):docker搭建ES集群
這里我們來介紹了ES集群索引的分片管理
ES集群索引分片管理
介紹
- 分片(shard):因為ES是個分布式的搜索引擎,所以索引通常都會分解成不同部分,而這些分布在不同節點的資料就是分片,ES自動管理和組織分片,并在必要的時候對分片資料進行再平衡分配,所以用戶基本上不用擔心分片的處理細節,
- 副本(replica):ES默認為一個索引創建一個主分片,并分別為其創建一個副分片,也就是說每個索引都由一個主分片成本,而每個主分片都有相應的一個copy,
- ES7.x之后,如果不指定索引分片,默認會創建一個主分片和一個副分片,而7.x版本之前的比較,如6.x版本,默認是5個主分片
下面我們來看看分片如何使用?
創建索引(不指定分片數量)
curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
創建之后我們在獲取索引來看看分片數量
curl -X GET "172.25.45.150:9200/nba"
回傳結果如關注下面這部分
"settings": {
"index": {
"creation_date": "1646360464892",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "oUWzENVEQVW_hXZlCdfQhg",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到number_of_shards(主分片)和number_of_shards(副分片)的數量都是1,
將上述索引洗掉我們再來看看指定分片數量如何取創建
curl -X DELETE "172.25.45.150:9200/nba"
創建索引(指定分片數量)
- settings
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
- 創建索引
curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
繼續來看看索引的分片
curl -X GET "172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到現在我們創建的索引分片數量已經為我們設定的了
索引分片分配
- 分片分配到哪個節點是由ES自動管理的,如果某個節點掛了,那分配又會重新分配到別的節點上,
- 在單機中,節點沒有副分片,因為只有一個節點沒必要生成分片,一個節點掛了,副分片也會掛掉,完全是單故障,沒有存在的意義,
- 在集群中,同個分片它的主分片和副分片不會在同一個節點上,因為主分片和副分片在同個節點,節點掛了,副分片和主分片一樣是掛了,不要把雞蛋都放在同個籃子里,
- 可以手動移動分片,比如把某個分片移動從節點1移動到節點2.
- 創建索引時指定的主分片數以后是無法修改的,所以主分片數的數量要根據專案決定,如果真的要增加主分片只能重建索引了,副分片數以后是可以修改的,
按照上述我們所創建的分片,有3個主分片,然后每個主分片都1個副分片,那么我們就存在6個分片,
如果裝了kibana的可以通過Monitoring查看分片分布的情況,

- Primary:主分片
- Replica:副分片
- Relocating:分配中
- Initializing:初始化
- Unassigned Primary:未分片的主分片
- Unassigned Replica:未分配的副分片
手動移動分片
將分片2從節點1移動到節點3
curl -X PUT "172.25.45.150:9200/_cluster/reroute" -H 'Content-Type:aplication/json' -d '
{
"commands": [
{
"move": {
"index": "nba",
"shard": 2,
"from_node": "node-1",
"to_node": "node-3"
}
}
]
}
'

修改副分片數量
上面我們提到了主分片的數量在索引創建后是無法修改的,如果想要進行修改就得重新創建一下索引,那么我們可以修改副分片的數量的,
curl -X PUT "http://172.25.45.150:9200/nba/_settings" -H 'Content-Type:application/json' -d '
{
"number_of_replicas":2
}
'
查詢所有
curl -X GET "http://172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到副分片的數量變成了2個,現在每個主分片都存在2個副分片,那么現在我們就存在了9個分片,我們來看一下分片的分布

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/437977.html
標籤:其他
上一篇:人工智能——深度學習
