influxdb time series database new/delete continuous query
Dbs:{
host: "192.168.0.1",
port: 8086,
database: "mydb",
username: "root",
password: "root"
}
1. New continuous query
const Influx = require('influx');
const influx = new Influx.InfluxDB(Dbs)
await influx.createContinuousQuery(<连续查询名称>, <sql>, <时序数据库名>, <执行间隔>)
//连续查询名称 ——> dataanalysis
//sql ——> select count(FYJYL) as nsywzerototal INTO dataanalysis.autogen.dataanalysis FROM mydb.autogen.processdata where FYJYL = 0 group by time(1d),SBXLH fill(0)
//时序数据库名 ——> mydb
//执行间隔 ——> RESAMPLE EVERY 4h
2. Delete continuous query
const influx = new Influx.InfluxDB(Dbs)
await influx.dropContinuousQuery(<连续查询名称>, <时序数据库名>)
3. Query
const influx = new Influx.InfluxDB(Dbs)
await influx.query(`select ...`).then(result=>{
})
4. Precautions for continuous query
(1) When the content field of the query cannot be inserted into the table in one continuous query, multiple continuous queries can be created, but the repeated fields cannot be included, otherwise the content of the repeated fields will be overwritten
; When the key fields are the same, its content will be overwritten by the latest data, that is, there is only one piece of data for the same key at the same time.
5. SQL direct execution
Official website portal: https://docs.influxdata.com/influxdb/v1.7/query_language/continuous_queries/
involves INTO knowledge points
SELECT_clause INTO <插入的表> FROM_clause [WHERE_clause] [GROUP_BY_clause]
#select count(FYJYL) as nsywzerototal INTO dataanalysis.autogen.dataanalysis FROM mydb.autogen.processdata where FYJYL = 0 group by time(1d),SBXLH fill(0)
New continuous query
CREATE CONTINUOUS QUERY <连续查询名称> ON <库名>
RESAMPLE EVERY <执行时间间隔> FOR 执行时间范围
BEGIN
<cq_query>
END
For example, RESAMPLE EVERY 30m FOR 60m
==> calculates the data within 60 minutes every 30 minutes
6. Visually create a new continuous query
7. Continuous query execution order problem
The order in which continuous queries are created refers to the order in which continuous queries are executed. Continuous queries created first are executed first; time-series databases continue to insert data, so there may be errors between statistical data and actual data (it is inevitable to use continuous queries).
8. Data insertion
insert table without will be created automatically
influx.writePoints([{
measurement: 'table_name',//表名
tags:{code:4401},
fields: { time:'2022-12-01 14:00:00', value:100.02 } //字段
}])
await influx.query(`select ...`).then(result=>{
console.log(result) // [{time:'2022-12-01 14:00:00',value:100.02,code:4401}]
})
Reference: https://node-influx.github.io/class/src/index.js~InfluxDB.html