elasticsearch数据过期删除处理
一、概述
使用elasticsearch收集日志进行处理,时间久了,很老的数据就没用了或者用途不是很大,这个时候就要对过期数据进行清理.这里介绍两种方式清理这种过期的数据。
1、curator
关于版本:
安装:
https://www.elastic.co/guide/en/elasticsearch/client/curator/current/installation.html
我使用的是ubuntu系统,所以参考的是https://www.elastic.co/guide/en/elasticsearch/client/curator/current/apt-repository.html
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - vim /etc/apt/sources.list.d/curator.list
deb [arch=amd64] https://packages.elastic.co/curator/5/debian stable main sudo apt-get update && sudo apt-get install elasticsearch-curator
我使用的是elasticsearch-6.5.1,所以安装的是curator5.
安装完成后会生成两个命令:curator、curator_cli,这里我们只先用到curator。
需要创建配置文件:有两个文件一个是config、一个是action
mkdir {/etc/curator,/data/curator}
config:
# cat config_file.yml
client:
hosts:
- 127.0.0.1
port:
url_prefix:
use_ssl: False
certficate:
client_cert:
client_key:
ssl_no_validate: False
http_auth:
timeout:
master_only: true
logging:
loglevel: INFO
logfile: "/data/curator/action.log"
logformat: default
action:
# cat action_file.yml
---
actions:
:
action: delete_indices
description: >-
Delete indices older than days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: regex
value: '^apm-6.5.1-transaction-|^apm-6.5.1-span-'
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count:
exclude: :
action: delete_indices
description: >-
Delete indices older than days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: loadbalance-api-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y-%m-%d'
unit: days
unit_count:
exclude:
---
actions:
:
action: delete_indices
description: >-
Delete indices older than days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: regex
value: 'fluentd-k8s-(2019.02.11|2019.02.12)$'
exclude: true
- filtertype: pattern
kind: prefix
value: fluentd-k8s-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count:
exclude:
可以设置多个action,每个都以不同的数字分割,使用不同的清理策略,具体可以参考https://www.elastic.co/guide/en/elasticsearch/client/curator/5.6/actions.html
注意自己的index的格式,比如我这里的时间格式有两种:
注意匹配,否则那个action就返回空列表,从而不会删除。
这个历史数据重要的会先落地到hdfs,然后在删除。这个日期根据自己服务器的磁盘和日志的重要性自己规划。重要的比如双11的数据不想删除,想留下来可以写到exclude里面,
或者做一个snapshot备份。接下来设置一个定时任务去删除就好了。
crontab -e
* * */ * * curator --config /etc/curator/config_file.yml /etc/curator/action_file.yml
2、使用脚本删除
# cat es-dele-indices.sh
#!/bin/bash
#delete elasticsearch indices
searchIndex=fluentd-k8s
elastic_url=127.0.0.1
elastic_port= date2stamp(){
date --utc --date "$1" +%s
} dateDiff(){
case $ in
-s) sec=; shift;;
-m) sec=; shift;;
-h) sec=; shift;;
-d) sec=; shift;;
*) sec=; shift;;
esac
dte1=$(date2stamp $)
dte2=$(date2stamp $)
diffSec=$((dte2-dte1))
if ((diffSec < )); then abs=-; else abs=; fi
echo $((diffSec/sec*abs))
} for index in $(curl -s "${elastic_url}:${elastic_port}/_cat/indices?v" | grep -E " ${searchIndex}-20[0-9][0-9]\.[0-1][0-9]\.[0-3][0-9]" | awk '{ print $3 }');do
date=$(echo ${index: -}|sed 's/\./-/g')
cond=$(date +%Y-%m-%d)
diff=$(dateDiff -d $date $cond)
echo -n "${index} (${diff})"
if [ $diff -gt ]; then
#echo "/ DELETE"
curl -XDELETE "${elastic_url}:${elastic_port}/${index}?pretty"
else
echo ""
fi
done
elasticsearch数据过期删除处理的更多相关文章
- 18-10-15 服务器删除数据的方法【Elasticsearch 数据删除 (delete_by_query 插件安装使用)】方法二没有成功
rpa 都是5.xx ueba 分为2.0 或者5.0 上海吴工删除数据的方法 在许多项目中,用户提供的数据存储盘大小有限,在运行一段时间后,大小不够就需要删除历史的 Elasticsearch 数 ...
- 关于Redis数据过期策略
1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...
- redis数据过期策略【转】
key的过期时间通常,Redis key被创建时不会自动关联过期时间,key将长久存在,除非通过DEL等命令显示的删除.EXPIRE命令簇可以为指定的key关联一个过期时间,代价是一点额外的内存开销. ...
- Redis数据过期策略
1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...
- Redis数据过期和淘汰策略详解(转)
原文地址:https://yq.aliyun.com/articles/257459# 背景 Redis作为一个高性能的内存NoSQL数据库,其容量受到最大内存限制的限制. 用户在使用Redis时,除 ...
- java操作elasticsearch实现查询删除和查询所有
后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...
- 工作随笔——elasticsearch数据冷热分离、数据冷备
概述: 适合日志类型的数据存储方案.即当日数据写入,历史数据只读. 节省部分硬件成本.热数据采用更好的硬件. 环境: 已有6个ES节点,使用docker-compose方式搭建. es1:master ...
- ElasticSearch 数据增删改实现
前言 本文介绍 ElasticSearch 增加.删除.修改数据的使用示例.通过Restful 接口和 Python 实现.ES最新版本中有Delete By Query 和 Update By Qu ...
- Redis(二十):Redis数据过期和淘汰策略详解(转)
原文地址:https://yq.aliyun.com/articles/257459# 背景 Redis作为一个高性能的内存NoSQL数据库,其容量受到最大内存限制的限制. 用户在使用Redis时,除 ...
随机推荐
- P1495 曹冲养猪(拓展欧几里得)
题目描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把.举个例子,假如有 ...
- Laravel-nestedset that base left and right values tree package
This is a Laravel 4-5 package for working with trees in relational databases. Laravel 5.5, 5.6, 5.7, ...
- Python:每日一题006
题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…… 个人思路及代码: # 方 ...
- tian_lie
后台托管:nohup ./re_start_job.sh kg_fk_etl >>log.log 2>&1 & 查看进程:ps -ef|grep kg_fk_etl ...
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
- ActiveMQ_5死信队列
activemq死信队列 DLQ-死信队列(Dead Letter Queue)用来保存处理失败或者过期的消息. 出现以下情况时,消息会被redelivered: A transacted sessi ...
- 过滤器和拦截器filter和Interceptor的区别
1.创建一个Filter过滤器只需两个步骤 创建Filter处理类 web.xml文件中配置Filter 2.Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的 ...
- windows server防火墙添加例外的步骤
Windows Server 2012 防火墙如何添加端口例外的方法 在Windows Server 2012系统中,如果用户想在防火墙中开通一个端口,您可以按以下步骤执行: 1. 首先点击桌面左 ...
- 谈谈最近的想法和 Thoughtworks 的 Offer
最近笔者一直没有记录博客,原因是因为卷入了面试,离职,谈判,思考等一系列事件中.不过可以先说明一下的是, 笔者最后还是拒绝了 Thoughtworks 的 Offer,继续留在目前的公司. 去年毕业后 ...
- nginx 502错误 upstream sent too big header while reading response header from upstream
原本的设置是 proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; 在这种配置下,使用fiddler进行抓包分 ...