目标:
ElasticSearch数据迁移失败的问题解决 以及基本操作命令
问题描述:
# 迁移数据
POST _reindex
{
"source": {
"index": "product"
},
"dest": {
"index": "mall_product"
}
}
运行出错,出错如下:
"failures": [
{
"index": "mall_product",
"type": "_doc",
"id": "1",
"cause": {
"type": "cluster_block_exception",
"reason": "index [mall_product] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"
},
"status": 403
},
原因分析:
通常我们在Elasticsearch中增加或更新数据时会出现错误:“index [mall_product] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];”,说名这个索引是只读状态。
此时我们需要对该索引的设置进行修改.
解决方案:
先关闭该索引:
POST mall_product/_close
在获取该索引的设置
GET mall_product/_settings
结果如下:
{
"mall_product" : {
"settings" : {
"index" : {
"number_of_shards" : "1",
"blocks" : {
"read_only_allow_delete" : "true"
},
"provided_name" : "mall_product",
"creation_date" : "1631332367473",
"number_of_replicas" : "1",
"uuid" : "QK16bdu4TeugW5Ryxif_RA",
"version" : {
"created" : "7040299"
}
}
}
}
}
此时我们需要修改 “read_only_allow_delete” 为false.
PUT mall_product/_settings?pretty
{
"index":{
"blocks.read_only_allow_delete":"false"
}
}
修改成功:
{
"acknowledged" : true
}
重新打开mall_product索引
POST mall_product/_open
运行结果显示成功:
{
"acknowledged" : true,
"shards_acknowledged" : true
}
最终进行数据迁移:
# 迁移数据
POST _reindex
{
"source": {
"index": "product"
},
"dest": {
"index": "mall_product"
}
}
成功:
{
"took" : 18,
"timed_out" : false,
"total" : 8,
"updated" : 0,
"created" : 8,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
备注
如果上面的方法执行完之后,过几分钟又会出现同样的错误,并且查看索引信息,read_only_allow_delete依然为true
这时就是因为你的磁盘空间不足导致的,查看官方文档,可以看到当磁盘的使用率超过95%时,Elasticsearch为了防止节点耗尽磁盘空间,自动将索引设置为只读模式。
解决方法:
1、最简单也是最直接的是清理磁盘空间
2、更改elasticsearch.yml配置文件,在config/elasticsearch.yml中增加下面这句话
cluster.routing.allocation.disk.watermark.flood_stage: 99%