日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch索引自动删除

發布時間:2023/12/2 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch索引自动删除 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

腳本分2部分,1部分查找符合條件的索引名,2腳本調用1腳本,進行刪除操作

腳本

查找符合條件的,默認大于30天

# coding:utf-8__author__ = 'Jipu FANG'from elasticsearch import Elasticsearch import re import time import datetimenow = time.localtime() data1 = datetime.datetime(now[0], now[1], now[2])es=Elasticsearch("http://192.168.30.135:9200")res = es.cat.indices()l = res.strip().split() def dindex(day=30):index = []for i in l:if re.search('\d+\.\d+\.\d+$', i):itime = time.strptime(re.findall('\d+\.\d+\.\d+$', i)[0], "%Y.%m.%d")data2 = datetime.datetime(itime[0], itime[1], itime[2])d = (data1-data2).daysif int(d) > int(day):index.append(i)return indexif __name__ == '__main__':print dindex()

對符合條件的索引,進行刪除操作

# coding:utf-8__author__ = 'Jipu FANG'import requests import json import time from multiprocessing.dummy import Pool as ThreadPool import re import indexs''' delect index url:"http://192.168.30.135:9200/app-2017.05.16" headers:'Content-Type: application/json' data:{"query": {"match_all":{}}}' select log curl: "http://192.168.30.135:9200/_search" headers:'Content-Type: application/json' data:{"query": {"match": {"message": {"query": "ERROR|77" }}}' '''# request API class ES_API:def __init__(self, url, data, headers):self.url=urlself.data=dataself.headers=headersdef delete(self):r = requests.delete(url=self.url, data=json.dumps(self.data), headers=self.headers)v=r.textprint(v)def post(self):r = requests.post(url=self.url, data=json.dumps(self.data), headers=self.headers)v=r.textprint(v)# 刪除索引,day保留多少天 def delete_index(day):for i in indexs.dindex(day):url = r"http://192.168.30.135:9200/%s" %(i)headers = {'Content-Type':'application/json'}data = {"query": {"match_all":{}}}C=ES_API(url, data, headers)C.delete()time.sleep(3)return "Delete indexs OK!"# 關閉索引,day保留多少天,當索引處于關閉狀態,資源占用比較少 def close_index(day):for i in indexs.dindex(day):url = r"http://192.168.30.135:9200/%s/_close?pretty" %(i)headers = {'Content-Type':'application/json'}data = {}C=ES_API(url, data, headers)C.post()time.sleep(3)return "index status close ok!"delete_index(30) time.sleep(60) close_index(15)

轉載于:https://www.cnblogs.com/GXLo/p/7405631.html

總結

以上是生活随笔為你收集整理的Elasticsearch索引自动删除的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。