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天的數據。大家可以根據自己的需求修改相關的參數或者增加其他處理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章