當(dāng)需要存儲(chǔ)的數(shù)據(jù)量特別大的時(shí)候,我們會(huì)希望ES能夠通過(guò)預(yù)先設(shè)定的閾值,去自動(dòng)創(chuàng)建索引,并將過(guò)期的索引刪除掉。這里就需要用到ES的生命周期。
ES的生命周期分為4個(gè)階段:HOT->WARM->COLD->DELETE
。
elasticsearch version 7.9.3
kibana version 7.9.3
其中的 HOT 階段是必須配置的,其他三個(gè)階段可選。rollover
下包含三個(gè)屬性,可以從三個(gè)維度控制文檔的滾動(dòng)閾值。只要達(dá)到其中的任意一個(gè)條件及會(huì)生成新的索引
# DELETE _ilm/policy/message_policy
PUT _ilm/policy/message_policy
{"policy": {"phases": { "hot": {"actions": { "rollover": {"max_docs" : 10, # 當(dāng)索引的中文檔數(shù)達(dá)到10時(shí),觸發(fā)滾動(dòng)策略
"max_size": "30GB", # 當(dāng)索引的數(shù)據(jù)容量達(dá)到30GB時(shí),觸發(fā)滾動(dòng)策略
"max_age": "1d" # 每天生成一個(gè)新的索引
}
}
},
"delete": {"min_age": "5s", # 當(dāng)索引的創(chuàng)建時(shí)間超過(guò)5s,索引就會(huì)被自動(dòng)清理掉
"actions": { "delete": {}
}
}
}
}
}
定義索引的模版我們可以通過(guò)定義模版,將匹配正則表達(dá)式message_info-*
的所有新創(chuàng)建的索引設(shè)置成模板中設(shè)定的屬性。還需要將我們之前定義好的生命周期message_policy
設(shè)置到模版中index.lifecycle.name
屬性中。index.lifecycle.rollover_alias
屬性的含義是,以后通過(guò)生命周期管理新創(chuàng)建出來(lái)的索引,都可以通過(guò)這個(gè)屬性中配置的別名message_alias
訪問(wèn)到。
# DELETE _template/message_template
PUT _template/message_template
{"order": 1,
"index_patterns": [
"message_info-*"
],
"settings": {"number_of_shards": 3,
"number_of_replicas": 1,
"index.lifecycle.name": "message_policy",
"index.lifecycle.rollover_alias": "message_alias"
},
"mappings": {"properties": { "deviceId": {"type": "keyword"
},
"messageId": {"type": "keyword"
}
}
}
}
這里需要手動(dòng)設(shè)置一下第一創(chuàng)建的索引,目的是為了設(shè)置is_write_index屬性is_write_index
:表示能夠通過(guò)別名插入數(shù)據(jù),默認(rèn)情況下是不能通過(guò)別名插入的
這樣的話,以后滾動(dòng)生成的索引都會(huì)包含這個(gè)屬性,這樣就可以通過(guò)別名往新的索引中寫(xiě)入數(shù)據(jù)。%3Cmessage_info_%7Bnow%2Fd%7D-000001%3E
這個(gè)配置是想創(chuàng)建的索引名稱(chēng)中包含日期,穿件出來(lái)的索引就是這個(gè)樣子的message_info_2022.12.16-000001
。
需要注意一點(diǎn)是,索引的名字必須是以橫杠(-)加 數(shù)字結(jié)尾
,也就是索引的名字必須匹配^.*-\d+$
這個(gè)正則表達(dá)式。否則通過(guò)索引的生命周期是無(wú)法創(chuàng)建新的索引的,ES在滾動(dòng)創(chuàng)建索引的時(shí)候會(huì)報(bào)錯(cuò)。
# DELETE /message_info-*
PUT %3Cmessage_info_%7Bnow%2Fd%7D-000001%3E
{"aliases": {"message_alias": { "is_write_index": true
}
}
}
修改ES的定期檢測(cè)時(shí)間ES會(huì)根據(jù)設(shè)置時(shí)間定期檢測(cè),默認(rèn)為10分鐘,為了更快的看到效果,可以改為10秒。
PUT /_cluster/settings
{"transient": {"indices.lifecycle.poll_interval": "10s"
}
}
插入數(shù)據(jù)POST /message_alias/_bulk
{"create":{"_type":"_doc"}}
{"deviceId":"張三","messageId":"1212"}
通過(guò)索引數(shù)據(jù)通過(guò)別名查看
GET /message_alias/_search
通過(guò)別名查看索引定義主要關(guān)注setting
中的生命周期的配置是否正確
GET /message_alias
{"message_info_2022.12.16-000005" : {"aliases" : { "message_alias" : {"is_write_index" : true
}
},
"mappings" : { "properties" : {"deviceId" : { "type" : "keyword"
},
"messageId" : { "type" : "keyword"
}
}
},
"settings" : { "index" : {"lifecycle" : { "name" : "message_policy",
"rollover_alias" : "message_alias"
},
"number_of_shards" : "3",
"provided_name" : "",
"creation_date" : "1671157454338",
"number_of_replicas" : "1",
"uuid" : "snU_zflXTOW7CjqWqd63ug",
"version" : { "created" : "7090399"
}
}
}
}
}
查看索引滾動(dòng)計(jì)劃可以看到索引的生命周期信息
GET message_info-*/_ilm/explain
{"indices" : {"message_info_2022.12.16-000005" : { "index" : "message_info_2022.12.16-000005",
"managed" : true,
"policy" : "message_policy",
"lifecycle_date_millis" : 1671157454338,
"age" : "16.13m",
"phase" : "hot",
"phase_time_millis" : 1671157454438,
"action" : "rollover",
"action_time_millis" : 1671157464410,
"step" : "check-rollover-ready",
"step_time_millis" : 1671157464410,
"phase_execution" : {"policy" : "message_policy",
"phase_definition" : { "min_age" : "0ms",
"actions" : {"rollover" : { "max_size" : "30gb",
"max_age" : "1d",
"max_docs" : 10
}
}
},
"version" : 4,
"modified_date_in_millis" : 1671111430428
}
}
}
}
對(duì)已存在的索引設(shè)置生命周期PUT jidu_iot_message_handle_device_message_info_*/_settings
{"index": {"lifecycle": { "name": "jidu_iot_message_handle_device_message_policy"
}
}
}
參考文獻(xiàn)https://www.elastic.co/guide/en/elasticsearch/reference/7.9/set-up-lifecycle-policy.html#ilm-create-policy
https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-rollover-index.html#roll-over-index-alias-with-write-index
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧