Elasticsearch按天生成和删除index脚本

        首先声明可以使用curator管理索引,这样大多数情况下来说会更安全更合理,也是官方推荐的。

        但是如果是按天建索引,且每天的数据量大体差别不大,且可以按天滚动索引,则可以使用脚本来简单粗暴的自动化管理,这对于我这个懒人来说很有诱惑力,大体的思路就是使用脚本通过elasticsearch的restful接口来执行新建和删除指定日期的索引,然后通过linux的crontab在业务低峰期如半夜11点55分来定时执行脚本,废话不说,上脚本:

#!/bin/bash

date2Create=`date -d +1days "+%Y%m%d"`
date2Delete=`date -d -6days "+%Y%m%d"`
curl -X PUT "localhost:9200/test_$date2Create?pretty" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 4
		//your other index setting
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
		//your other index mapping properties
        }
    }
}
'
echo "create index test_$date2Create succeed"
curl -X DELETE "localhost:9200/test_$date2Delete?pretty"
echo "delete index test_$date2Delete succeed"

        上述脚本实现了在凌晨之前新建第二天的索引,并且删除6天前的索引,保证elasticsearch中滚动存储7天的数据。大家可以根据自己的需求修改相关的参数或者增加其他处理。

猜你喜欢

转载自blog.csdn.net/microGP/article/details/107043898