[Influxdb]Continuous Queries(CQ)

之前我介紹RP並了解作用為自動刪除過期資料,但如果你不想完全刪除資料,

這時就可以透過CQ自動把資料存放在另一個MEASUREMENT,

完全定時自動觸發,無須人工介入。

基本語法

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

 

Create CQ

CREATE CONTINUOUS QUERY cpu_3m ON telegraf 
BEGIN 
  SELECT mean("Percent_Processor_Time") INTO win_cpu_used_3m FROM "win_cpu" GROUP BY time(3m) 
END

每3分鐘彙總 win_cpu 中 Percent_Processor_Time的平均資料,並新增至  win_cpu_used_3m。

注意:一定要有一個彙總函示和group by time

 

SHOW CONTINUOUS QUERIES (顯示所有CQ)

DROP CONTINUOUS QUERY cpu_3m ON telegraf(刪除CQ)

 

進階語法

CREATE CONTINUOUS QUERY <cq_name> ON <database_name>
RESAMPLE EVERY <interval> FOR <interval>
BEGIN
  <cq_query>
END
CREATE CONTINUOUS QUERY "mem_every_1m" ON "telegraf"
RESAMPLE EVERY 1m
BEGIN
  SELECT mean("Available_Bytes") INTO "win_mem_every" FROM "win_mem" GROUP BY time(5m)
END

每1分鐘執行該query,資料區間為5分鐘,並新增至win_mem_every。

 

 

參考

Continuous Queries