首页 > 编程语言 > 详细

python - 定时清理ES 索引

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

只保留三天

#!/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 索引

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!