1.分片设置前检查
[esadmin@oracle1 ~]$ curl 'http://192.168.1.7:9201/_cat/shards?v'
index shard prirep state docs store ip node
mydb1 0 p STARTED 2 13.4kb 192.168.1.7 esdb-node-3
mydb1 0 r STARTED 2 13.4kb 192.168.1.7 esdb-node-1
2.设置分片的个数
--设置10个分片,每个分片有两个副本。
curl -XPUT 192.168.1.7:9201/_template/all_index_shards -H 'Content-Type: application/json' -d '
{
"index_patterns": "*",
"settings": {
"number_of_shards": 10,
"number_of_replicas": "2"
}
}'
3.新创建索引
curl -XPOST http://192.168.1.7:9201/mydb2/_doc/?pretty -H 'Content-Type: application/json' -d '
{
"id" : "1","name" : "xsq001","city" : "深圳","course" : "oracle","age":18,"teacher" : "da哥","pxdate": "20200829"
}'
curl -XPOST http://192.168.1.7:9201/mydb2/_doc/?pretty -H 'Content-Type: application/json' -d '
{
"id" : "2","name" : "xsq002","city" : "深圳","course2" : "oracle2","age":19,"teacher" : "da哥","pxdate": "20200829"
}'
--查询数据
curl -XPOST 'http://192.168.1.7:9202/mydb2/_search?pretty' -H 'Content-Type: application/json'
{
"took" : 100,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "mydb2",
"_type" : "_doc",
"_id" : "7cvIV5ABQJTCooTXArus",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "xsq001",
"city" : "深圳",
"course" : "oracle",
"age" : 18,
"teacher" : "da哥",
"pxdate" : "20200829"
}
},
{
"_index" : "mydb2",
"_type" : "_doc",
"_id" : "7svIV5ABQJTCooTXHbtC",
"_score" : 1.0,
"_source" : {
"id" : "2",
"name" : "xsq002",
"city" : "深圳",
"course2" : "oracle2",
"age" : 19,
"teacher" : "da哥",
"pxdate" : "20200829"
}
}
]
}
}
--查询分片情况
[esadmin@oracle1 ~]$ curl 'http://192.168.1.7:9201/_cat/shards?v'
index shard prirep state docs store ip node
mydb2 1 p STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 1 r STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 1 r STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 7 p STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 7 r STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 7 r STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 4 p STARTED 1 6.9kb 192.168.1.7 esdb-node-3
mydb2 4 r STARTED 1 7kb 192.168.1.7 esdb-node-2
mydb2 4 r STARTED 1 6.9kb 192.168.1.7 esdb-node-1
mydb2 6 r STARTED 1 6.9kb 192.168.1.7 esdb-node-3
mydb2 6 p STARTED 1 6.9kb 192.168.1.7 esdb-node-2
mydb2 6 r STARTED 1 6.9kb 192.168.1.7 esdb-node-1
mydb2 9 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 9 p STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 9 r STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 2 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 2 r STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 2 p STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 8 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 8 r STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 8 p STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 3 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 3 p STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 3 r STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 5 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 5 r STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 5 p STARTED 0 208b 192.168.1.7 esdb-node-1
mydb2 0 r STARTED 0 208b 192.168.1.7 esdb-node-3
mydb2 0 p STARTED 0 208b 192.168.1.7 esdb-node-2
mydb2 0 r STARTED 0 208b 192.168.1.7 esdb-node-1
mydb1 0 p STARTED 2 13.4kb 192.168.1.7 esdb-node-3
mydb1 0 r STARTED 2 13.4kb 192.168.1.7 esdb-node-1
--果然,新生成的索引,每个索引有10[0~9]个分片,每个分片有2个从副本。
4.索引查看
[esadmin@oracle1 indices]$ ll
total 0
drwxrwxr-x 13 esadmin esadmin 110 Jun 27 11:40 eks_TgK5S1-VMF6Xi7IIyw
drwxrwxr-x 4 esadmin esadmin 29 Jun 27 11:24 R8ovegjBS7i44hlbOA-PUQ
--分片查看
[esadmin@oracle1 indices]$ cd eks_TgK5S1-VMF6Xi7IIyw
[esadmin@oracle1 eks_TgK5S1-VMF6Xi7IIyw]$ ll
total 0
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 0
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 1
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 2
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 3
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 4
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 5
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 6
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 7
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 8
drwxrwxr-x 5 esadmin esadmin 49 Jun 27 11:40 9
drwxrwxr-x 2 esadmin esadmin 25 Jun 27 11:40 _state
---每个分片由三个文件
[esadmin@oracle1 eks_TgK5S1-VMF6Xi7IIyw]$ cd 0
[esadmin@oracle1 0]$ ll
total 0
drwxrwxr-x 2 esadmin esadmin 42 Jun 27 11:40 index
drwxrwxr-x 2 esadmin esadmin 53 Jun 27 11:40 _state
drwxrwxr-x 2 esadmin esadmin 49 Jun 27 11:40 translog
5.总结
分片和副本数量的设置必须在集群创建好之后设置。
且已经创建的索引,修改分片和副本数后不会有变化。