elasticsearch学习系列:开发springboot程序进行定时删除过期索引

场景

项目使用了elasticsearch技术来进行数据搜索,而单天的数据量比较大,随着时间的流逝,整个elasticsearch集群所占的空间会越来越大。如果不进行定时的删除,就会导致存储满载,进而影响系统。而elasticsearch支持使用curl命令调用elasticsearch集群命令,定时删除某一天的索引数据。但是没有办法删除某个时间点之前的数据。那么假如有一天脚本没有执行,那没有执行的日期数据就没办法删除,只能手动去删除。这样还是会存在问题的。这个时候,就需要有一个程序,可以自定义对某个索引进行扫描,删除自定义天数的数据。这样,不管什么时候执行,都会将保留周期之外的索引数据给删除掉。

环境

软件版本
JDK8
spring-data-elasticsearch3.1.10.RELEASE
spring-boot2.1.8.RELEASE
elasticsearch6.2.2

正文

流程梳理

接下来,让我们梳理一下整个程序的开发流程,如下:

Created with Raphaël 2.2.0 开始 初始化ElasticSearch组件 获取配置数据 获取全部索引 从全部索引中,筛选出过期的索引数据 删除过期的索引数据 结束

show the code

对于程序员来说,除了理论要溜,代码也得溜。所以,接下来会展示整个过程有用的代码。

maven依赖

这里只展示主要的依赖,像微服务Spring-cloud相关的依赖就不放在里面了。


 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.1.8.RELEASE</version>
 </parent>
 
 <dependencies>
	 <!-- elasticsearch -->
	 <dependency>
	     <groupId>org.springframework.boot</groupId>
	     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	 </dependency>
</dependencies>

配置文件

配置文件主要是写在application.properties文件里面,下面展示一个样例:

# es配置
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=test
# 过期索引配置,值为天数
index.delindex.service-test=7
index.delindex.service-test2=7

主要函数

配置类
@Component
@Configuration
@PropertySource(value = {"classpath:/application.properties"}, encoding = "utf-8")
@ConfigurationProperties(prefix = "index")
@Getter
@Setter
public class ElasticsearchDelIndexConfig {
    private Map delindex = new HashMap();
}
处理类

因为功能比较简单,所以是直接将代码放到主类里面,直接跑的。代码如下,如果有需求,直接复制就可以跑起来的。

@Slf4j
@EnableEurekaClient
@SpringBootApplication
public class ElasticsearchDelApplication implements CommandLineRunner {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private ElasticsearchDelIndexConfig delIndexConfig;

    /**
     * 主进程
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(ElasticsearchDelApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        while (true) {
            delIndexConfig.getDelindex().forEach((k, v) -> {
                log.info("索引起始名称为{},保留周期为{}天", k, v);
                Set<String> allIndexes = getAllIndices();
                if (allIndexes.size() > 0) {
                    log.info("当前索引总数为:{}", allIndexes.size());

                    List<String> timeoutList = getIndicesTimeout(allIndexes, String.valueOf(k), Long.parseLong(String.valueOf(v)), TimeUnit.DAYS);
                    if (timeoutList.size() > 0) {
                        log.info("对于前缀为 {} 的索引 过期数目为:{}", k, timeoutList.size());
                        timeoutList.forEach(indexName -> {
                            if (elasticsearchTemplate.deleteIndex(indexName)) {
                                log.info("成功删除 {} 索引", indexName);
                            } else {
                                log.error("删除 {} 索引 失败", indexName);
                            }
                        });
                    }
                }
            });
            log.info("休眠 10 分钟");
            ThreadUtil.sleep(10, TimeUnit.MINUTES);
        }
    }

    /**
     * 获取所有index
     */
    public Set<String> getAllIndices() {
        ActionFuture<IndicesStatsResponse> isr = elasticsearchTemplate.getClient().admin().indices().stats(new IndicesStatsRequest().all());
        Set<String> set = isr.actionGet().getIndices().keySet();
        return set;
    }


    /**
     * 获取指定索引的创建时间
     * @param indexName 索引名称
     * @return 索引的创建时间
     */
    public String getCreateTimeForIndex(String indexName) {
        String createTime = elasticsearchTemplate.getClient().admin()
                .indices().getSettings(new GetSettingsRequest().indices(indexName))
                .actionGet().getIndexToSettings().get(indexName)
                .getAsSettings("index").get("creation_date");
        return createTime;
    }

    /**
     * 获取前缀相同的过时索引
     * @param allIndices 索引列表
     * @param indexName 要过滤的索引前缀
     * @param time 超时时间
     * @param timeUnit 时间单位
     * @return 超时的索引列表
     */
    public List<String> getIndicesTimeout(Set<String> allIndices, String indexName, Long time, TimeUnit timeUnit) {
        List<String> list = new ArrayList<>();
        allIndices.parallelStream()
                .filter(name -> name.startsWith(indexName))
                .forEach(one -> {
                    String createTime = getCreateTimeForIndex(one);
                    if (System.currentTimeMillis() - Long.parseLong(createTime) > timeUnit.toMillis(time)) {
                        log.info("索引 {} 已经过期,创建时间为{}", one, createTime);
                        list.add(one);
                    }
                });
        return list;
    }
}

结果

程序跑起来之后,elasticsearch集群的特定索引的过期数据都会被删除。

总结

spring-boot可以封装了很多开发细节,减少了我们很多工作。在懂得内部原理的时候,的确很好用。不过对于初学者,在使用spring-boot的时候,记得学习里面内部的原理。这样遇到问题才能快速锁定问题,并解决。

随缘求赞

如果我的文章对大家产生了帮忙,可以在文章底部点个赞或者收藏;
如果有好的讨论,可以留言;
如果想继续查看我以后的文章,可以点击关注
可以扫描以下二维码,关注我的公众号:枫夜之求索阁,查看我最新的分享!
在这里插入图片描述
拜拜

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!对于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 ``` 通过以上步骤,您就可以设置定时清理过期索引的任务了。请确保对配置文件和定时任务的设置进行适当调整,以满足您的需求。希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫夜求索阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值