一、概述

使用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数据过期删除处理的更多相关文章

  1. 18-10-15 服务器删除数据的方法【Elasticsearch 数据删除 (delete_by_query 插件安装使用)】方法二没有成功

    rpa 都是5.xx  ueba 分为2.0 或者5.0 上海吴工删除数据的方法 在许多项目中,用户提供的数据存储盘大小有限,在运行一段时间后,大小不够就需要删除历史的 Elasticsearch 数 ...

  2. 关于Redis数据过期策略

    1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...

  3. redis数据过期策略【转】

    key的过期时间通常,Redis key被创建时不会自动关联过期时间,key将长久存在,除非通过DEL等命令显示的删除.EXPIRE命令簇可以为指定的key关联一个过期时间,代价是一点额外的内存开销. ...

  4. Redis数据过期策略

    1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...

  5. Redis数据过期和淘汰策略详解(转)

    原文地址:https://yq.aliyun.com/articles/257459# 背景 Redis作为一个高性能的内存NoSQL数据库,其容量受到最大内存限制的限制. 用户在使用Redis时,除 ...

  6. java操作elasticsearch实现查询删除和查询所有

    后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...

  7. 工作随笔——elasticsearch数据冷热分离、数据冷备

    概述: 适合日志类型的数据存储方案.即当日数据写入,历史数据只读. 节省部分硬件成本.热数据采用更好的硬件. 环境: 已有6个ES节点,使用docker-compose方式搭建. es1:master ...

  8. ElasticSearch 数据增删改实现

    前言 本文介绍 ElasticSearch 增加.删除.修改数据的使用示例.通过Restful 接口和 Python 实现.ES最新版本中有Delete By Query 和 Update By Qu ...

  9. Redis(二十):Redis数据过期和淘汰策略详解(转)

    原文地址:https://yq.aliyun.com/articles/257459# 背景 Redis作为一个高性能的内存NoSQL数据库,其容量受到最大内存限制的限制. 用户在使用Redis时,除 ...

随机推荐

  1. ionic3搭建笔记及编译成apk

    一.安装node.js 二.安装Ionic2 npm install -g ionic (安装最新版本) ionic -v //查看版本号(是否安装成功) npm uninstall -g ionic ...

  2. 对于PHP面试知识点的小结

    基础篇 了解大部分数组处理函数 字符串处理函数(区别 mb_ 系列函数) & 引用,结合案例分析 == 与 === 区别 isset 与 empty 区别 全部魔术函数理解 static.$t ...

  3. Spring 添加属性集中常见方法

    //创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...

  4. 使用GetAdaptersInfo时,网卡类型的值为71

    使用GetAdaptersInfo时,网卡类型的值为71,代表无线网卡.

  5. kei下无法跳转到函数的定义处

    1 勾选“option for target”----“output”----"Browse information" 2 重新编译整个工程, 执行上面两个步骤就可以跳转了.

  6. Maths | 为什么点积等价于投影后的乘积

    目录 1. 复习点积 2. 点积的对称性 3. 矩阵与变换的关系 4. 一维矩阵也是一种线性变换 5. 最终解释:为什么是投影 先上结论: \(\boldsymbol v\)和\(\boldsymbo ...

  7. 18.数组(一)之认识java数组

    数组是一个简单的复合数据类型,它是一组有序数据的集合,它当中的每一个数据都具有相同的数据类型,我们通过数组名再加上一个不会越界的下标值来唯一确定数组中的元素. 还有就是,数组是一个特殊的对象. 不管在 ...

  8. js- DOM事件之按钮绑定函数注意事项

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  9. Java设计模式----中介者模式

    说到中介大家都不会陌生,买房子租房子有中介,出国留学有中介,买卖二手车还是有中介.那么中介到底是个什么角色呢?实际上,中介就是让买卖双方不必面对面直接交流,由他/她来完成买卖双方的交易,达到解耦买卖人 ...

  10. typescript handbook 学习笔记1

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...