小言_互联网的博客

Elasticsearch:Split index API - 把一个大的索引分拆成更多分片

578人阅读  评论(0)

在我们实际使用 Elasticsearch 时,随着时间的推移,我们会发现有扩容的必要。这个可能由于我们在刚开始创建项目认识不足。我们需要更多的 primary shards,这样可以提高 ingest 的速度。那么我们有什么办法来把之前的一个大的索引变成更多的小的索引呢?这个答案就是 split index API。它的基本用法如下:


  
  1. POST / my- index- 000001/_split/ split- my- index- 000001
  2. {
  3. "settings": {
  4. "index.number_of_shards": 2
  5. }
  6. }

它有两种形式的接口:


  
  1. POST / <index>/_split/ <target-index>
  2. PUT / <index>/_split/ <target-index>

在我们操作之前,我们有如下的前提条件:

我们可以通过如下的方法来把一个索引变为 read-only:


  
  1. PUT /my_source_index/_ settings
  2. {
  3. "settings": {
  4. "index.blocks.write": true
  5. }
  6. }

拆分索引 API 允许你将现有索引拆分为新索引,在该索引中,每个原始主 shard 在新索引中均拆分为两个或多个主 shard。

索引可拆分的次数(以及每个原始碎片可拆分为的碎片数量)由 index.number_of_routing_shards 设置确定。 路由分片的数量指定哈希空间,该空间在内部用于以一致的哈希在各个 shard 之间分发文档。 例如,将 number_of_routing_shards 设置为30(5 x 2 x 3)的5个 shard 索引可以拆分为 2 或 3。换句话说,可以如下拆分:

  • 5→10→30(拆分依次为2和3)
  • 5→15→30(拆分依次为3和2)
  • 5→30(拆分6)

 

动手实践

准备数据

在今天的教程中,我们将使用 Kibana 自带的索引来进行展示。打开 Kibana 界面:

点击 Add data:

这样我们的样本数据就导入进 Elasticsearch 了。通过上面的操作,我们在 Elasticsearch 中将生成一个叫做 kibana_sample_data_flights 的索引。

 

使用 split index API 来拆分索引

按照要求,我们首先把索引变为只读:


  
  1. PUT kibana_sample_data_logs/_ settings
  2. {
  3. "blocks.write": true
  4. }

在 Kibana  中运行上面的指令。我们可以通过如下的指令来进行查询:

GET kibana_sample_data_logs/_settings

上面的指令显示:


  
  1. {
  2. "kibana_sample_data_logs" : {
  3. "settings" : {
  4. "index" : {
  5. "number_of_shards" : "1",
  6. "auto_expand_replicas" : "0-1",
  7. "blocks" : {
  8. "write" : "true"
  9. },
  10. "provided_name" : "kibana_sample_data_logs",
  11. "creation_date" : "1602127165075",
  12. "number_of_replicas" : "0",
  13. "uuid" : "oyJwGhQATvCil2rWAC6nqg",
  14. "version" : {
  15. "created" : "7080099"
  16. }
  17. }
  18. }
  19. }
  20. }

上面显示 blocks.write 为 true,也即我们不可以再向这个索引写入任何的数据了。从上面我们也可以看出来,我们的当前的索引只有一个 primary shard。这个可以从 number_of_shards 为 1 可以看出来。

我们在 Kibana 中打入如下的指令:


  
  1. POST kibana_sample_data_logs/_split/ kibana_sample_data_logs_split
  2. {
  3. "settings": {
  4. "index.number_of_shards": 2
  5. }
  6. }

在上面,我们把 kibana_sample_data_logs 拆分为 2 个㊗️ shards,并且我们的目标索引的名字为:kibana_sample_data_logs_split。上面的命令的返回值为:


  
  1. {
  2. "acknowledged" : true,
  3. "shards_acknowledged" : true,
  4. "index" : "kibana_sample_data_logs_split"
  5. }

我们可以通过如下的命令来进行查看过程:

GET _cat/recovery/kibana_sample_data_logs_split

上面显示的结果为:


  
  1. index shard time type stage source_host source_node target_host target_node repository snapshot files files_recovered files_percent files_total bytes bytes_recovered bytes_percent bytes_total translog_ops translog_ops_recovered translog_ops_percent
  2. kibana_sample_data_logs_split 0 484ms local_shards done n/a n/a 127.0.0.1 node 1 n/a n/a 0 0 100. 0% 15 0 0 100. 0% 11573815 0 0 100. 0%
  3. kibana_sample_data_logs_split 1 503ms local_shards done n/a n/a 127.0.0.1 node 1 n/a n/a 0 0 100. 0% 15 0 0 100. 0% 11573815 0 0 100. 0%

上面显示100%已经完成。

我们可以通过如下的方法来进行比较索引的数量:

GET kibana_sample_data_logs/_count

它显示:


  
  1. {
  2. "count" : 14074,
  3. "_shards" : {
  4. "total" : 1,
  5. "successful" : 1,
  6. "skipped" : 0,
  7. "failed" : 0
  8. }
  9. }

我们用如下的方法来查看 kibana_sample_data_logs_split 的设置:


  
  1. {
  2. "kibana_sample_data_logs_split" : {
  3. "settings" : {
  4. "index" : {
  5. "routing" : {
  6. "allocation" : {
  7. "initial_recovery" : {
  8. "_id" : null
  9. }
  10. }
  11. },
  12. "number_of_shards" : "2",
  13. "routing_partition_size" : "1",
  14. "auto_expand_replicas" : "0-1",
  15. "blocks" : {
  16. "write" : "true"
  17. },
  18. "provided_name" : "kibana_sample_data_logs_split",
  19. "resize" : {
  20. "source" : {
  21. "name" : "kibana_sample_data_logs",
  22. "uuid" : "oyJwGhQATvCil2rWAC6nqg"
  23. }
  24. },
  25. "creation_date" : "1602127847864",
  26. "number_of_replicas" : "0",
  27. "uuid" : "rhs6k0P6QNudSVO1MauQZA",
  28. "version" : {
  29. "created" : "7080099",
  30. "upgraded" : "7080099"
  31. }
  32. }
  33. }
  34. }
  35. }

上面显示 number_of_shards 为2,表示有2个主 shard。我们可以使用如下命令来进行查看文档的数量:

GET kibana_sample_data_logs_split/_count

上面的命令显示:


  
  1. {
  2. "count" : 14074,
  3. "_shards" : {
  4. "total" : 2,
  5. "successful" : 2,
  6. "skipped" : 0,
  7. "failed" : 0
  8. }
  9. }
GET _cat/shards/kibana_sample_data_logs_split?v

上面的命令显示:


  
  1. index shard prirep state docs store ip node
  2. kibana_sample_data_logs_split 1 p STARTED 7076 4. 8mb 127.0.0.1 node 1
  3. kibana_sample_data_logs_split 0 p STARTED 6998 4. 7mb 127.0.0.1 node 1

我们可以看到有两个 primary shard。

如果我们想把上面的拆分的索引重新变成一个大的索引的话,那么请阅读我的另外一篇文章 “通过 shrink API 减少 shard 数量来缩小 Elasticsearch 索引”。


转载:https://blog.csdn.net/UbuntuTouch/article/details/108960950
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场