王婆卖瓜,自卖自夸一下,这代码越写越规范了,真香!
用于清理elasticsearch数据,放着自己记录看一下。第一次用yield
''' elasticsearch clean '''
__author__ = 'jiangjw'
import requests
import heapq
import traceback
import datetime
class Esclean(object):
def __init__(self, host, port):
self.host = host
self.port = str(port)
def setting(self):
self.urlName = 'http://' + self.host + ":" + self.port + '/_cat/indices'
self.urlDel = 'http://' + self.host + ':' + self.port + '/'
def getName(self):
try:
dataResponse = requests.get(self.urlName)
dataResponse.raise_for_status()
dataName = dataResponse.text
for line in dataName.splitlines():
name = line.split(' ')[2]
yield name
except:
traceback.print_exc()
def filterName(self):
today = datetime.date.today()
todayNumber = int(str(today).replace('-', ""))
self.filebeat = {}
for name in self.getName():
nameDate = name.replace('.', '-').split('-')[-3:]
nameDate = ''.join(nameDate)
if nameDate.isdigit():
nameDate = int(nameDate)
if nameDate > 99999999:
nameDate = str(nameDate)[-6:]
self.filebeat[nameDate] = name
continue
if nameDate + 2 <= todayNumber:
delName = name
yield delName
def filterBeat(self):
beatName = heapq.nlargest(1, self.filebeat)
for i in self.filebeat:
if i not in beatName:
yield self.filebeat.get(i)
def deleteName(self):
for u in self.filterName():
try:
deleteUrl = self.urlDel + u
response = requests.delete(url=deleteUrl)
response.raise_for_status()
except:
traceback.print_exc()
for f in self.filterBeat():
try:
deleteUrl = self.urlDel + f
response = requests.delete(url=deleteUrl)
response.raise_for_status()
except:
traceback.print_exc()
def main():
start = datetime.datetime.now()
host = '192.168.110.174'
port = 9200
es = Esclean(host=host, port=port)
es.setting()
es.deleteName()
end = datetime.datetime.now()
print("{}运行完成,脚本共耗时:{}s".format(start, (end - start).seconds))
if __name__ == '__main__':
main()