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)
Elasticsearch索引自动删除的更多相关文章
- Elasticsearch索引自动套用模板
公司ELK系统目前的设置是每月自动将日志信息记录至新的索引中,将日志数据按月分索引保存,在扩展的ELK架构中,利Logstash对接rabbitmq,获取日志消息,自动持久化至Elasticsearc ...
- elasticsearch索引自动清理
一 es 基本操作 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v GET /_cat/indices?v DELETE /fi ...
- Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除
下面是实验步骤: > db.things.find(){ "_id" : ObjectId("5652d71a1524dc14663060e8"), &q ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- MongoDB自动删除过期数据--TTL索引
前序: 由于公司业务需求,对于3个月前的过期数据需要进行删除动作,以释放空间和方便维护 本来想的是使用crontab写个脚本定时执行,但是看到Mongo本身就有自动删除过期数据的功能,所以还是用一 ...
- Elasticsearch索引(company)_Centos下CURL增删改
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch索引说明 a. 通过上面几篇博客已经将Elastics ...
- ES3:ElasticSearch 索引
ElasticSearch是文档型数据库,索引(Index)定义了文档的逻辑存储和字段类型,每个索引可以包含多个文档类型,文档类型是文档的集合,文档以索引定义的逻辑存储模型,比如,指定分片和副本的数量 ...
- Elasticsearch索引和文档操作
列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...
- windows系统中 利用kibana创建elasticsearch索引等操作
elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...
随机推荐
- OGNL简介
OGNL 一:OGNL简介 OGNL的全称是Object Graph Navigation Language即对象导航语音.它是一个开源项目,工作在视图层,用来取代页面中的java脚本.简化数据 ...
- 前端chrome浏览器调试
引言 "工欲善其事,必先利其器" 恩,这句话我觉得说的特别有道理,举个例子来说吧,厉害的化妆师都有一套非常专业的刷子,散粉刷负责定妆,眼影刷负责打眼影,各司其职,有了专业的工具才能 ...
- Java集合(5)一 HashMap与HashSet
目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...
- Solr7使用Oracle数据源导入+中文分词
安装目录假设为#solr_home,本文的#solr_home为apps/svr/solr 1. 在#solr_home/server/solr下新建文件夹,假设为mjd 2. 将#solr_home ...
- ettercap的中间人欺骗
环境准备:kali系统 因为kali系统自带ettercap,比较方便, 不需要安装 ifcofing命令查看当前网关 ,当前的IP是: 172.16.42.1 查找局域网所有主机 通过netdisc ...
- http请求HttpClient短信接口
项目中安全设置找回密码的功能,需要通过发送短信验证绑定手机,通过绑定的手机号验证并重新设置密码. 因为项目是通过maven管理的,所以需要在pom.xml文件中引入jar包, maven引入的jar包 ...
- 来腾讯云开发者实验室 学习.NET Core 2.0
腾讯云开发者实验室为开发者提供了一个零门槛的在线实验平台,开发者实验室提供的能力: 零门槛扫码即可免费领取实验机器,支持使用自有机器参与,实验完成后支持保留实验成果: 在线 WEB IDE 支持 sh ...
- 使用.Net Core+EF7 CodeFirst(2)
上一篇的话,说了下怎么使用EF7 实现 CodeFirst去生成数据库, 其实还有好多问题的,这次一点一点的解决吧,都挺简单,不过零零散散的,, 1.读取配置文件,获得链接字符串 2.使用数据库进行增 ...
- java 的equals 与== ,null与isempty的区别
1 . == 是为了判断等号两边 变量 所对应 的 内存中的 值 是否 相等, 只是 值 的比较. 2. 假如 String s1 = new String("abc") ...
- HTML5经常使用知识
今日做项目.涉及到native和H5页面的交互 1.document.readyState document.readyState:推断文档是否载入完毕. firefox不支持. 这个属性是仅仅读的, ...