InfluxDB -- Continuous Query解析

Continuous Query(CQ)是内部周期运行的InfluxQL的查询,将查询结果写入指定的measurement,用它可以实现降采样downsampling。

结合CQ和RP(Retention Policy),可以将历史数据保存为低精度,最近的数据保存为高精度,以降低存储用量。

Continuous Query的语法结构:

CREATE CONTINUOUS QUERY  ON 
BEGIN

END

其中的需要一个函数,一个INTO字句和一个GROUP BY time()字句,其语句结构:

SELECT  INTO  FROM  [WHERE ] GROUP BY time()[,]

举个简单的例子

将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...

你可能感兴趣的:(influxdb)