备注:此内容为《Elasticsearch黑鸟教程(入门系列)》之八,修订于2020年12月13日。

1、yellow状态介绍

在Elasticsearch集群中,索引的yellow状态表示所有主分片可用,但不是所有副本分片都可用,最常见的情景是单节点时,由于Elasticsearch默认有1个副本,但主分片和副本不能在同一个节点上,所以副本就是未分配unassigned状态。通过如下命令,我们可以看到所有未分配副本的索引:

  • curl "http://localhost:9200/_cat/shards" | grep UNASSIGNED

结果如下所示,第一列表示索引名,第二列表示分片编号,第三列用于标识主/副本,p是主分片,r是副本。

  • curl "http://localhost:9200/_cat/shards" | grep UNASSIGNED
  • myindex1 0 r UNASSIGNED
  • myindex2 0 r UNASSIGNED

2、分片介绍

在Elasticsearch集群中,为了数据的安全,通常采用多个分片的形式存储。Elasticsearch的分片由于职责不同,又有主分片和副本分片之分。

primary shard:主分片,每个文档都存储在一个分片中,当你存储一个文档的时候,系统会首先存储在主分片中,然后会复制到不同的副本中。默认情况下,一个索引有5个主分片。你可以在事先制定分片的数量,当分片一旦建立,分片的数量则不能修改。

replica shard:副本分片,每一个分片有零个或多个副本。副本主要是主分片的复制,可以增加高可用性,提高性能。

默认情况下,一个主分片有一个副本,但副本的数量可以动态的增加。需要注意的是:副本必须部署在不同的节点上,不能部署在和主分片相同的节点上。

3、分片的设置参数

number_of_shards参数:是指索引要做多少个分片,只能在创建索引时指定,后期无法修改,如下所示:

  • curl -XPUT 'http://localhost:9200/myindex' -H 'Content-Type: application/json' -d '
  • {
  • "settings": {
  • "number_of_shards": 10,
  • "number_of_replicas" : 1
  • }
  • }
  • '

查看一下设置的效果:

  • curl -XGET 'http://localhost:9200/myindex/_settings?pretty'
  • {
  • "myindex" : {
  • "settings" : {
  • "index" : {
  • "routing" : {
  • "allocation" : {
  • "include" : {
  • "_tier_preference" : "data_content"
  • }
  • }
  • },
  • "number_of_shards" : "10",
  • "provided_name" : "myindex",
  • "creation_date" : "1606892563505",
  • "number_of_replicas" : "1",
  • "uuid" : "HbgnTQYWQnucIb2-7RfacA",
  • "version" : {
  • "created" : "7100099"
  • }
  • }
  • }
  • }
  • }

number_of_replicas参数:是指每个分片有多少个副本,后期可以动态修改,如下所示:

  • curl -XPUT 'http://localhost:9200/myindex/_settings' -H 'Content-Type: application/json' -d '
  • {
  • "index" : {
  • "number_of_replicas" : 2
  • }
  • }
  • '

标签: none

添加新评论