influxdb的retention policy
定義
The part of InfluxDB’s data structure that describes for how long InfluxDB keeps data (duration), how many copies of this data is stored in the cluster (replication factor), and the time range covered by shard groups (shard group duration). RPs are unique per database and along with the measurement and tag set define a series.
下面簡單談一下我對這句話的理解。retention policy描述了influxdb中的數(shù)據(jù)會保留多長時間、數(shù)據(jù)保留幾個副本(開源版的只能保留一個副本),以及每個shard保存多長時間的數(shù)據(jù)。每個influxdb數(shù)據(jù)庫都有一個獨立的retention policy。這里面涉及到幾個基本概念,下面描述一下。
- DURATION:這個描述了保留策略要保留多久的數(shù)據(jù)。這個機制對于時序型的數(shù)據(jù)來講,是非常有用的。
- SHARD:這個是實際存儲influxdb數(shù)據(jù)的單元。每個shard保留一個時間片的數(shù)據(jù),默認是7天。如果你保存1年的數(shù)據(jù),那么influxdb會把連續(xù)7天的數(shù)據(jù)放到一個shard中,使用好多個shard來保存數(shù)據(jù)。
- SHARD DURATION:這個描述了每個shard存放多數(shù)據(jù)的時間片是多大。默認7天。需要注意的是,當數(shù)據(jù)超出了保留策略后,influxdb并不是按照數(shù)據(jù)點的時間一點一點刪除的,而是會刪除整個shard group。
- SHARD GROUP:顧名思義,這個一個shard group包含多個shard。對于開源版的influxdb,這個其實沒有什么區(qū)別,可以簡單理解為一個shard group只包含一個shard,但對于企業(yè)版的多節(jié)點集群模式來講,一個shard group可以包含不同節(jié)點上的不同shard,這使得influxdb可以保存更多的數(shù)據(jù)。
- SHARD REPLICATION:這個描述了每個shard有幾個副本。對于開源版來講,只支持單副本,對于企業(yè)版來講,每個shard可以冗余存儲,這樣可以避免單點故障。
關于SHARD DURATION
influxdb官方建議,如果保留期限低于2天,shard duration設置為1h,如果保留期限位于2天到6個月之間,shard duration設置為1d,如果保留期限大于6個月,shard duration設置為7d,
創(chuàng)建retention policy
當執(zhí)行create database時,會創(chuàng)建一個名叫autogen的retention policy,它會永久保留數(shù)據(jù)
> create database testdb > use testdb Using database testdb > show retention policies name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 true可以通過create retention policy的命令來創(chuàng)建一個新的retention policy
CREATE RETENTION POLICY "one_month" ON "testdb" DURATION 30d REPLICATION 1 DEFAULT然后再查看一下retention policy
> show retention policies name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 false one_month 720h0m0s 24h0m0s 1 true需要注意的是,如果切換數(shù)據(jù)庫的retention policy的話,已有的數(shù)據(jù)會被全部刪掉。
更新retention policy
如果想修改retention policy的數(shù)據(jù)保留時間,可以使用alter retention policy語句
> alter retention policy autogen on mydb duration 30d REPLICATION 1 SHARD DURATION 1d default> show retention policies name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 720h0m0s 24h0m0s 1 true更新retention policy的保留時間的話,數(shù)據(jù)不會全部丟失。
retention policy最佳實踐
關于retention policy,最好的方式是在創(chuàng)建數(shù)據(jù)庫時就考慮清楚數(shù)據(jù)要保留多長時間。簡單來講,可以通過下面的方式創(chuàng)建一個保留30天的數(shù)據(jù)庫,并且,influxdb還會自動生成一個比較合理的shardGroupDuration,此時是1天。
> create database testdb2 with duration 30d > show retention policies on testdb2 name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 720h0m0s 24h0m0s 1 true參考資料
https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
https://stackoverflow.com/questions/41620595/how-to-set-default-retention-policy-and-duration-for-influxdb-via-configuration
https://dzone.com/articles/simplifying-influxdb-shards-and-retention-policies
https://stackoverflow.com/questions/39878027/influxdb-whats-shard-group-duration
總結
以上是生活随笔為你收集整理的influxdb的retention policy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 李宏毅机器学习——领域适应Domain
- 下一篇: 教师计算机考核有啥用,教师计算机使用管理