码迷,mamicode.com
首页 > 编程语言 > 详细

python - 定时清理ES 索引

时间:2018-08-16 17:46:18      阅读:816      评论:0      收藏:0      [点我收藏+]

标签:转化   python3   pytho   odi   utf-8   ==   line   env   open   

只保留三天

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import datetime

# 时间转化为字符串

now_time = datetime.datetime.now().strptime(datetime.datetime.now().strftime("%Y.%m.%d"),"%Y.%m.%d")
os.system("curl -XGET http://127.0.0.1:9200/_cat/indices > date.txt")

with open("date.txt","r") as f:
    for line in f.readlines():
        index = line.strip().split()[2]

        try:
            index_strftime = datetime.datetime.strptime(index.split("-")[-1], "%Y.%m.%d")
            Ca = (now_time - index_strftime)
            if str(Ca).split()[0] == "0:00:00":
                continue
            elif int(str(Ca).split()[0]) >= 3:
                command = "curl -XDELETE http://127.0.0.1:9200/%s" % index
                print(command)
                os.system(command)
            else:
                print(index,"no del")
        except:
            pass

加入到定时任务即可

python - 定时清理ES 索引

标签:转化   python3   pytho   odi   utf-8   ==   line   env   open   

原文地址:https://www.cnblogs.com/heweiblog/p/9488252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!