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]>]
cq_query
中不需要时间范围。InfluxDB 自动生成cq_query
执行 CQ 的时间范围。系统将忽略cq_query
子句中任何用户指定的时间范围。cq_query
以间隔相同的间隔执行,并且它们在 InfluxDB 数据库预设时间边界的开始处运行。如果GROUP BY time()
间隔为一小时,则 CQ 在每小时开始时执行。now()
和now()
减去 GROUP BY time()
间隔之间的时间范围。如果GROUP BY time()
时间间隔为一小时,当前时间为 17:00,则查询的时间范围为 16:00 到 16:59.999999999。CREATE CONTINUOUS QUERY "cq_basic" ON "transportation"
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h)
END
可以选择存储在非默认保留策略中。
CREATE CONTINUOUS QUERY "cq_basic_rp" ON "transportation"
BEGIN
SELECT mean("passengers") INTO "transportation"."three_weeks"."average_passengers" FROM "bus_data" GROUP BY time(1h)
END
原指标数据
name: bus_data
--------------
time passengers complaints
2016-08-28T07:00:00Z 5 9
2016-08-28T07:15:00Z 8 9
2016-08-28T07:30:00Z 8 9
2016-08-28T07:45:00Z 7 9
2016-08-28T08:00:00Z 8 9
2016-08-28T08:15:00Z 15 7
2016-08-28T08:30:00Z 15 7
2016-08-28T08:45:00Z 17 7
2016-08-28T09:00:00Z 20 7
结果如下
name: average_passengers
------------------------
time mean
2016-08-28T07:00:00Z 7
2016-08-28T08:00:00Z 13.75
CREATE CONTINUOUS QUERY <cq_name> ON <database_name>
RESAMPLE EVERY <interval> FOR <interval>
BEGIN
<cq_query>
END
RESAMPLE
。如果EVERY
间隔是两小时,InfluxDB 每隔一小时执行一次 CQ。
示例一
CREATE CONTINUOUS QUERY "cq_advanced_every" ON "transportation"
RESAMPLE EVERY 30m
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h)
END
cq_advanced_every
以 30 分钟的间隔执行。每 30 分钟cq_advanced_every
运行一次查询,该查询涵盖当前时间段的时间范围,即与 相交的一小时时间段now()
。
8:00 `cq_advanced_every` executes a query with the time range `WHERE time >= '7:00' AND time < '8:00'`.
8:30 `cq_advanced_every` executes a query with the time range `WHERE time >= '8:00' AND time < '9:00'`.
9:00 `cq_advanced_every` executes a query with the time range `WHERE time >= '8:00' AND time < '9:00'`.
name: average_passengers
------------------------
time mean
2016-08-28T07:00:00Z 7
2016-08-28T08:00:00Z 13.75
示例二
CREATE CONTINUOUS QUERY "cq_advanced_for" ON "transportation"
RESAMPLE FOR 1h
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(30m)
END
8:00 `cq_advanced_for` executes a query with the time range `WHERE time >= '7:00' AND time < '8:00'`.
8:30 `cq_advanced_for` executes a query with the time range `WHERE time >= '7:30' AND time < '8:30'`.
9:00 `cq_advanced_for` executes a query with the time range `WHERE time >= '8:00' AND time < '9:00'`.
name: average_passengers
------------------------
time mean
2016-08-28T07:00:00Z 6.5
2016-08-28T07:30:00Z 7.5
2016-08-28T08:00:00Z 11.5
2016-08-28T08:30:00Z 16
示例三
CREATE CONTINUOUS QUERY "cq_advanced_every_for" ON "transportation"
RESAMPLE EVERY 1h FOR 90m
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(30m)
END
8:00 `cq_advanced_every_for` executes a query with the time range `WHERE time >= '6:30' AND time < '8:00'`.
9:00 `cq_advanced_every_for` executes a query with the time range `WHERE time >= '7:30' AND time < '9:00'`.
name: average_passengers
------------------------
time mean
2016-08-28T06:30:00Z 3
2016-08-28T07:00:00Z 6.5
2016-08-28T07:30:00Z 7.5
2016-08-28T08:00:00Z 11.5
2016-08-28T08:30:00Z 16
总结
EVERY 1h、FOR 90m、GROUP BY time(30m)的解释如下:
EVERY 1h
: 每60分钟执行一次CQ。FOR 90m
:查询now()
- now() - 90m
的范围数据。GROUP BY time(30m)
: 按照30分钟聚合数据。