es 按月建索引,定时删除3个月前索引

1.建模板

#log_template为模板名称可更改
PUT /_template/log_template?pretty
{
#以log开头的索引都会使用此模板创建
  "template": "log*",

  "settings": {
#设置es分片数量,可不设
    "number_of_shards": 10

  },

  "mappings": {
#设置模板中属性,可不设置
      "properties": {

        "name": {

          "type": "text",

          "index": "true"

        }
      }
  },
#索引别名,查询时可使用别名查询索引
  "aliases": {"log":{}}

}

2.创建索引时携带年月(如果按日删除需携带年月日) 

例: log-202208

3.根据别名查询数据

#log为别名
GET /log/_search

4.定时删除过期数据,本文使用shell脚本

#!/bin/bash
  
function delete_indices() {
#comp_date=`date -d "7 day ago" +"%Y-%m-%d"`  7天前
    comp_date=`date -d "3 month ago" +"%Y-%m-%d"`
    date1="$1"
    date2="$comp_date"

    t1=`date -d "$date1" +%s`
    t2=`date -d "$date2" +%s`
    if [ $t1 -le $t2 ]; then
        echo "$1时间早于$comp_date,进行索引删除"
        #转换一下格式,将类似2022-10-01格式转化为20221001
        curl -XDELETE http://192.168.3.111:9200/*$2
    fi
}
#如果按日删除 需将grep '[[:digit:]]\{6\}' 改为grep '[[:digit:]]\{8\}'
curl -XGET http://192.168.3.111:9200/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | grep '[[:digit:]]\{6\}' | sort | uniq  | while read line
do
 #如果按日删除 需将p="${line:0:4}-${line:4:2}-01" 改为 p="${line:0:4}-${line:4:2}-${line:6}"
    p="${line:0:4}-${line:4:2}-01"
    o="${line}"
    #调用索引删除函数
    delete_indices $p $o
done

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于Elasticsearch定时清理过期索引,您可以使用Elasticsearch的Curator插件来实现。Curator是一个用于管理Elasticsearch索引的工具,可以方便地执行索引删除、备份等操作。 以下是一些步骤可以用来设置定时清理过期索引的任务: 1. 安装Curator:您可以通过pip命令安装Curator: ``` pip install elasticsearch-curator ``` 2. 创建配置文件:创建一个YAML格式的配置文件,用于定义索引清理任务。例如,创建一个名为`curator_config.yml`的文件,并添加以下内容: ```yaml --- # 连接Elasticsearch的信息 client: hosts: - 127.0.0.1 port: 9200 url_prefix: use_ssl: False certificate: client_cert: client_key: ssl_no_validate: False http_auth: timeout: 30 master_only: False # 清理任务配置 actions: 1: action: delete_indices description: "Delete indices older than 30 days" filters: - filtertype: pattern kind: prefix value: your_index_prefix- - filtertype: age source: name direction: older timestring: '%Y.%m.%d' unit: days unit_count: 30 ``` 在上述配置中,`hosts`字段指定了Elasticsearch的地址和端口,`filters`字段定义了过滤器规则,这里以索引名的缀和索引创建时间进行过滤。 3. 创建定时任务:使用cron表达式来定义定时任务,可以在Linux系统的crontab中添加以下命令: ``` curator --config /path/to/curator_config.yml --dry-run ``` 上述命令中的`--dry-run`参数用于测试运行,可以在实际运行之先检查将要执行的操作。 4. 运行定时任务:如果测试运行没有问题,可以将上述命令添加到crontab中,并设置合适的执行频率。例如,每天凌晨执行一次: ``` 0 0 * * * curator --config /path/to/curator_config.yml ``` 通过以上步骤,您就可以设置定时清理过期索引的任务了。请确保对配置文件和定时任务的设置进行适当调整,以满足您的需求。希望对您有所帮助!如有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值