发现elasticsearch集群的状态是red,unassign的分片数很多,看了下都是些旧的日期的索引(应该是定时任务删除失败导致的)。

curl -XGET ip:port/_cat/shards | grep UNASSIGNED

数量有几百个,写个脚本处理下,先恢复成green。red状态好像会影响索引创建和数据迁移

先把需要删除的索引导出到文件

curl -XGET ip:port/_cat/shards | grep UNASSIGNED  needDelIndex.txt

确认下要删除的索引列表。没问题就执行下面删除shell(es的ip和端口需要修改下)

#!/bin/bash
echo "$1"
esUrl=${esip}:${esport}
indexfile=needDelIndex.txt
#cp -f /dev/null ${indexfile}
#curl -XGET ip:port/_cat/shards | grep UNASSIGNED  needDelIndex.txt
if [ ! -f ./${indexfile} ]; then
  echo $indexfile not exists
  exit 0
fi
logfile=esindex_del.`date +"%m-%d"`.log
cp -f /dev/null ${logfile}
lastIndexName="test"
for item in `cat ${indexfile} | awk '{print $1}'`
do
  if [ "$item" = "error" ]
  then
    continue
  fi
  if [ "$item" != "$lastIndexName" ]
  then
    curl -XDELETE ${esUrl}/${item}  ${logfile}
    echo ---------${item} `date`  ${logfile}
    sleep 5
  fi
  lastIndexName=${item}
done

因为我们的索引是按天创建的,索引名前缀是yyyy-MM-dd, 保留一段时间后需要批量删除。shell的第一个参数为yyyy-MM-dd,将删除该天及以前的旧索引

#!/bin/bash
esUrl=${esip}:${esport}
echo "$1"
if [ $# -ge 1 ]
then
  deleteDate=$1
else
  echo "please inpust detete esindex's date(yyyy-MM-dd)"
  exit 0
fi
indexfile=esindex.info
cp -f /dev/null ${indexfile}
curl '${esUrl}/_cat/indices'  ${indexfile}
logfile=esindex_del.`date +"%m-%d"`.out
cp -f /dev/null ${logfile}
for item in `cat ${indexfile} | awk '{print $3}'`
do
  if [ "$item" = "error" ]
  then
    continue
  fi
  parameter=${esUrl}/${item}
  indexdate=${item:0:10}
  if [ "$indexdate" = "$deleteDate" ]
  then
    curl -XDELETE ${parameter}  ${logfile}
    echo ---------${item}  ${logfile}
    sleep 5
  elif [[ "$indexdate" < "$deleteDate" ]]
  then
    curl -XDELETE ${parameter}  ${logfile}
    echo ---------${item}  ${logfile}
    sleep 5
  fi
done

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

标签:
批量删除es索引,shell脚本批量删除es索引的方法

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com

评论“shell脚本批量删除es索引的方法”

暂无“shell脚本批量删除es索引的方法”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。