Nodejs实现自动清理elasticsearch过期索引(日志清理)--学习笔记

2018-08-16更新 修正支持5.X全系 最新6.X 版本自动清除索引 增加权限验证。

 

 

本人是C#开发者,写起Nodejs估计还保留着浓浓的C#风格吧。

elasticsearch简单说一下,索引一般推荐以日期为单位。没啥原因大家的经验而已。

elasticsearch相关资料:https://www.elastic.co/guide/en/elasticsearch/client/index.html

                                       https://github.com/elastic/elasticsearch

主要使用的API https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-2-4.html#api-fieldstats-2-4

indices.delete 删除索引
 
indices.get 获取索引信息
 
create 建立索引并插入信息,如存在则追加信息。

这里我们看的是2.4的,5.X的应该也没啥大问题。这些都是相对简单的。

配置文件AppConfig.js

clearhistory:

      indexPrefix:索引前缀,分割符为下划线("_")。日期格式之间为"-"。索引格式为:xxx_xxx_2017-02-17

      month:保留几个月

      day:保留几天

      apiVersion:'5.6' //es服务端对应的版本号 切记切记要对应否则获取索引及其它可能会报错

AppConfig.js源码

var AppConfig = {
    debug: false,
    elasticSearch: {
        hosts: ['http://192.168.151.190:9200/'],//ES服务器IP地址    
        sniffOnStart: false,
        sniffInterval: 300000,
        httpAuth:'elastic:elastic2018',
        keepAlive:true,  
        connectionClass:"http",
        apiVersion:'5.6' //es服务端对应的版本号 切记切记要对应否则获取索引及其它可能会报错       
    },
    clearhistory: [{
        indexPrefix: 'xxx_xxx_',
        month: 0,
        day: 6
    }, {
        indexPrefix: 'xxx_frontend_',
        month: 0,
        day: 1
    }, {
        indexPrefix: 'xxx_',
        month: 0,
        day: 1
    }],
    log4js: {
        appenders: [{
            type: 'dateFile',
            absolute: true,
            filename: './logs/',
            maxLogSize: 1024 * 1024,
            backup: 3,
            pattern: 'yyyy-MM-dd.txt',
            alwaysIncludePattern: true,
            category: 'normal'
        }],
        replaceConsole: false
    }
};
module.exports = AppConfig;

ClearHistoryES.js

主要函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值