日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Elasticsearch索引自动删除

發布時間:2023/12/2 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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索引自动删除的全部內容,希望文章能夠幫你解決所遇到的問題。

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