zoukankan      html  css  js  c++  java
  • Elasticsearch索引自动删除

    简介

    脚本分2部分,1部分查找符合条件的索引名,2脚本调用1脚本,进行删除操作
    

    脚本

    查找符合条件的,默认大于30天

    # coding:utf-8
    
    __author__ = 'Jipu FANG'
    
    from elasticsearch import  Elasticsearch
    import re
    import time
    import datetime
    
    now = 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).days
                if int(d) > int(day):
                    index.append(i)
        return index
    
    if __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=url
            self.data=data
            self.headers=headers
    
        def delete(self):
            r = requests.delete(url=self.url, data=json.dumps(self.data), headers=self.headers)
            v=r.text
            print(v)
    
        def post(self):
            r = requests.post(url=self.url, data=json.dumps(self.data), headers=self.headers)
            v=r.text
            print(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)
    
  • 相关阅读:
    jQuery学习笔记3--网页字体变大变小
    jQuery学习笔记2--表格内容筛选
    jQuery学习笔记1--表格展开关系
    (转)PhoneGap开发环境搭建
    对HTML+CSS+JavaScript的个人理解
    (转)经典收藏 50个jQuery Mobile开发技巧集萃
    (转)phoneGap-Android开发环境搭建
    (转)面向移动设备的HTML5开发框架
    (转)前端攻略系列(二)
    (转)常见浏览器兼容性问题与解决技巧
  • 原文地址:https://www.cnblogs.com/GXLo/p/7405631.html
Copyright © 2011-2022 走看看