乐趣区

关于influxdb:InfluxDB-Continuous-Query解析

Continuous Query(CQ)是外部周期运行的 InfluxQL 的查问,将查问后果写入指定的 measurement,用它能够实现降采样 downsampling。

联合 CQ 和 RP(Retention Policy),能够将历史数据保留为低精度,最近的数据保留为高精度,以升高存储用量。

Continuous Query 的语法结构:

CREATE CONTINUOUS QUERY <cq_name> ON <database_name>
BEGIN
<cq_query>
END

其中的 <cq_query> 须要一个函数,一个 INTO 字句和一个 GROUP BY time()字句,其语句构造:

SELECT <function[s]> INTO <destination_measurement> FROM <measurement> [WHERE <stuff>] GROUP BY time(<interval>)[,<tag_key[s]>]

举个简略的例子

将 cpu.user 数据进行 5m 均匀:

CREATE CONTINUOUS QUERY "cq_5" ON falcon 
BEGIN 
SELECT mean(value) INTO falcon.autogen."5_cpu.user" FROM falcon.autogen."cpu.user" GROUP BY time(5m), * 
END

其中的 falcon.autogen.”5_cpu.user”,falcon 示意 database,autogen 示意 retention policy,5_cpu.user 示意新的 table。

// 查问 cq
> show continuous queries;
name: falcon
name  query
----  -----
cq_5  CREATE CONTINUOUS QUERY cq_5 ON falcon BEGIN SELECT mean(value) INTO falcon.autogen."5_cpu.user" FROM falcon.autogen."cpu.user" GROUP BY time(5m), * END

// 查问 cq 中的数据
> select * from "5_cpu.user" order by time desc limit 10;
name: 5_cpu.user
time                 endpoint mean
----                 -------- ----
2020-06-09T04:55:00Z trovedev 6.806252400038498
2020-06-09T04:50:00Z trovedev 13.796289128479335
2020-06-09T04:45:00Z trovedev 16.57212610575366
2020-06-09T04:40:00Z trovedev 28.577591691423635
2020-06-09T04:35:00Z trovedev 6.02882334702075
2020-06-09T04:30:00Z trovedev 4.667398282326042
2020-06-09T04:25:00Z trovedev 4.746237171675345
2020-06-09T04:20:00Z trovedev 5.006169574660594
2020-06-09T04:15:00Z trovedev 5.002280037736261
2020-06-09T04:10:00Z trovedev 4.775274987155538

值得注意的是,CQ 仅对实时的数据进行聚合,不会对历史数据聚合;也就是说,如果没有新的源数据,就不会产生新的聚合点。

逆向援用主动采样

应用通配符 (*) 和 INTO 查问的逆向援用语法,能够将 db 中的所有 measurement 依照对立的聚合办法进行采样,比方:将 prometheus 中的每个 measuremnt 每 10min 采样平均值,而后存入到 opsultra 的雷同名称的 measurement 中

create continuous query "cq_basic" on "prometheus" 
begin 
    select mean(*) into "opsultra"."six_month".:MEASUREMENT from /.*/ group by time(10m),* 
end

参考:
1.CQ:https://jasper-zhang1.gitbook…

退出移动版