使用shell腳本怎么批量刪除es索引-創(chuàng)新互聯(lián)

使用shell腳本怎么批量刪除es索引?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)-專業(yè)網站定制、快速模板網站建設、高性價比廣河網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式廣河網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋廣河地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。

發(fā)現(xiàn)elasticsearch集群的狀態(tài)是red,unassign的分片數(shù)很多,看了下都是些舊的日期的索引(應該是定時任務刪除失敗導致的)。

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

數(shù)量有幾百個,寫個腳本處理下,先恢復成green。red狀態(tài)好像會影響索引創(chuàng)建和數(shù)據遷移

先把需要刪除的索引導出到文件

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

確認下要刪除的索引列表。沒問題就執(zhí)行下面刪除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

因為我們的索引是按天創(chuàng)建的,索引名前綴是yyyy-MM-dd, 保留一段時間后需要批量刪除。shell的第一個參數(shù)為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

看完上述內容,你們掌握使用shell腳本怎么批量刪除es索引的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網頁題目:使用shell腳本怎么批量刪除es索引-創(chuàng)新互聯(lián)
文章起源:http://muchs.cn/article32/cocisc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網站、ChatGPT、企業(yè)網站制作微信公眾號、定制開發(fā)微信小程序

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站網頁設計