当前位置: 首页 > article >正文

influxdb时序数据库

以下概念及操作均来自influxdb2 官方文档

InfluxDB2 is the platform purpose-built to collect, store, process and visualize time series data. Time series data is a sequence of data points indexed in time order. Data points typically consist of successive measurements made from the same source and are used to track changes over time. Examples of time series data include:

  • Industrial sensor data
  • Server performance metrics
  • Heartbeats per minute
  • Electrical activity in the brain
  • Rainfall measurements
  • Stock prices

使用docker安装

docker run \--name influxdb2 \--publish 8086:8086 \--mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2 \--mount type=volume,source=influxdb2-config,target=/etc/influxdb2 \--env DOCKER_INFLUXDB_INIT_MODE=setup \--env DOCKER_INFLUXDB_INIT_USERNAME=ADMIN_USERNAME \--env DOCKER_INFLUXDB_INIT_PASSWORD=ADMIN_PASSWORD \--env DOCKER_INFLUXDB_INIT_ORG=ORG_NAME \--env DOCKER_INFLUXDB_INIT_BUCKET=BUCKET_NAME \influxdb:2Example:
docker run \--name influxdb2 \--publish 8086:8086 \--mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2 \--mount type=volume,source=influxdb2-config,target=/etc/influxdb2 \--env DOCKER_INFLUXDB_INIT_MODE=setup \--env DOCKER_INFLUXDB_INIT_USERNAME=influxdb \--env DOCKER_INFLUXDB_INIT_PASSWORD=influxdb \--env DOCKER_INFLUXDB_INIT_ORG=influxdb \--env DOCKER_INFLUXDB_INIT_BUCKET=influxdb \influxdb:2
  • –publish 8086:8086: Exposes the InfluxDB UI and HTTP API on the host’s 8086 port.
  • –mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2: Creates a volume named influxdb2-data mapped to the InfluxDB data directory to persist data outside the container.
  • --mount type=volume,source=influxdb2-config,target=/etc/influxdb2: Creates a volume named influxdb2-config mapped to the InfluxDB configuration directory to make configurations available outside the container.
  • --env DOCKER_INFLUXDB_INIT_MODE=setup: Environment variable that invokes the automated setup of the initial organization, user, bucket, and token when creating the container.
  • --env DOCKER_INFLUXDB_INIT_<SETUP_OPTION>: Environment variables for initial setup options–replace the following with your own values:
      • ADMIN_USERNAME: The username for the initial user–an admin user with an API Operator token.
      • ADMIN_PASSWORD: The password for the initial user.
      • ORG_NAME: The name for the initial organization.
      • BUCKET_NAME: The name for the initial bucket.

If successful, the command starts InfluxDB initialized with the user, organization, bucket, and Operator token, and logs to stdout.

You can view the Operator token in the /etc/influxdb2/influx-configs file and use it to authorize creating an All Access token.

访问: http://127.0.0.1:8086/

操作 CLI

write

  • -b, --bucket or --bucket-id flag with the bucket name or ID to write do.
  • -p, --precision flag with the timestamp precision (s).
  • String-encoded line protocol.
influx write \--bucket influxdb \--precision s "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
"

Query

root@c2ecea1e4729:/# influx query '
from(bucket: "influxdb")|> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field== "co" or r._field == "hum" or r._field == "temp")
'

结果

Result: _result
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:int
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  --------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T08:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T09:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T10:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T11:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T12:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T13:00:00.000000000Z                           1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T14:00:00.000000000Z                           1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T15:00:00.000000000Z                           3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T16:00:00.000000000Z                           7
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T17:00:00.000000000Z                           9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T18:00:00.000000000Z                          18
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T19:00:00.000000000Z                          22
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen  2022-01-01T20:00:00.000000000Z                          26
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T08:00:00.000000000Z                          35.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T09:00:00.000000000Z                          36.2
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T10:00:00.000000000Z                          36.1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T11:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T12:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T13:00:00.000000000Z                          36.5
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T14:00:00.000000000Z                          36.3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T15:00:00.000000000Z                          36.2
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T16:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T17:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T18:00:00.000000000Z                          36.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T19:00:00.000000000Z                          36.6
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen  2022-01-01T20:00:00.000000000Z                          36.5
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T08:00:00.000000000Z                            21
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T09:00:00.000000000Z                            23
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T10:00:00.000000000Z                          22.7
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T11:00:00.000000000Z                          22.4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T12:00:00.000000000Z                          22.5
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T13:00:00.000000000Z                          22.8
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T14:00:00.000000000Z                          22.8
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T15:00:00.000000000Z                          22.7
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T16:00:00.000000000Z                          22.4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T17:00:00.000000000Z                          22.7
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T18:00:00.000000000Z                          23.3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T19:00:00.000000000Z                          23.1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T20:00:00.000000000Z                          22.7
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:int
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  --------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T08:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T09:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T10:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T11:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T12:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T13:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T14:00:00.000000000Z                           0
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T15:00:00.000000000Z                           1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T16:00:00.000000000Z                           4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T17:00:00.000000000Z                           5
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T18:00:00.000000000Z                           9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T19:00:00.000000000Z                          14
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room  2022-01-01T20:00:00.000000000Z                          17
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T08:00:00.000000000Z                          35.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T09:00:00.000000000Z                          35.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T10:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T11:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T12:00:00.000000000Z                          35.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T13:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T14:00:00.000000000Z                          36.1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T15:00:00.000000000Z                          36.1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T16:00:00.000000000Z                            36
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T17:00:00.000000000Z                          35.9
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T18:00:00.000000000Z                          36.2
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T19:00:00.000000000Z                          36.3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room  2022-01-01T20:00:00.000000000Z                          36.4
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T08:00:00.000000000Z                          21.1
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T09:00:00.000000000Z                          21.4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T10:00:00.000000000Z                          21.8
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T11:00:00.000000000Z                          22.2
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T12:00:00.000000000Z                          22.2
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T13:00:00.000000000Z                          22.4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T14:00:00.000000000Z                          22.3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T15:00:00.000000000Z                          22.3
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T16:00:00.000000000Z                          22.4
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T17:00:00.000000000Z                          22.6
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T18:00:00.000000000Z                          22.8
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T19:00:00.000000000Z                          22.5
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T20:00:00.000000000Z                          22.2

influxQL query

root@c2ecea1e4729:/# influx v1 shell
SELECT co,hum,temp,room FROM "influxdb".autogen.home WHERE time >= '2022-01-01T08:00:00Z' AND time <= '2022-01-01T20:00:00Z'

结果:
在这里插入图片描述

在数据中重新映射或赋值

使用map()函数迭代数据中的每一行,并更新该行中的值。map()是Flux中最有用的函数之一,将帮助您完成许多需要执行的数据处理操作。

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "hum")|> map(fn: (r) => ({r with _value: r._value / 100.0}))
'

分组数据

使用group()函数按特定列值重新组合数据,为进一步处理做准备。

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> group(columns: ["room", "_field"])
'

Aggregate or select specific data

Use Flux aggregate or selector functions to return aggregate or selected values from each input table.

mean()返回每个输入表中指定列中非空值的平均值。

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "co" or r._field == "hum" or r._field == "temp")|> mean()
'
root@c2ecea1e4729:/# influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "co" or r._field == "hum" or r._field == "temp")|> mean()
'
Result: _result
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home                 Kitchen            6.6923076923076925
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home                 Kitchen            36.246153846153845
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen            22.623076923076926
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                      co                    home             Living Room            3.8461538461538463
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                     hum                    home             Living Room             36.05384615384615
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2022-01-01T08:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room             22.16923076923077

Aggregate functions

Aggregate functions drop columns that are not in the group key and return a single row for each input table with the aggregate value of that table.

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "temp")|> mean()|> duplicate(column: "_stop", as: "_time")
'
root@c2ecea1e4729:/# influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "temp")|> mean()|> duplicate(column: "_stop", as: "_time")
'
Result: _result
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float                      _time:time
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------  ------------------------------
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen            22.814285714285713  2022-01-01T20:00:01.000000000Z
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                  _value:float                      _time:time
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------------  ------------------------------
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room             22.44285714285714  2022-01-01T20:00:01.000000000Z

将数据转换为关系模式

如果来自关系SQL或类SQL查询语言,如InfluxQL,Flux使用的数据模型与您习惯的不同。Flux返回多个表,每个表包含一个不同的字段。“关系”模式将每个字段构造为每行中的一列。
使用pivot()函数根据时间戳将数据转换为“关系”模式。

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "co" or r._field == "hum" or r._field == "temp")|> filter(fn: (r) => r.room == "Kitchen")|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
'
root@c2ecea1e4729:/# influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "co" or r._field == "hum" or r._field == "temp")|> filter(fn: (r) => r.room == "Kitchen")|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
'
Result: _result
Table: keys: [_start, _stop, _measurement, room]_start:time                      _stop:time     _measurement:string             room:string                      _time:time                      co:int                     hum:float                    temp:float
------------------------------  ------------------------------  ----------------------  ----------------------  ------------------------------  --------------------------  ----------------------------  ----------------------------
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T14:00:00.000000000Z                           1                          36.3                          22.8
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T15:00:00.000000000Z                           3                          36.2                          22.7
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T16:00:00.000000000Z                           7                            36                          22.4
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T17:00:00.000000000Z                           9                            36                          22.7
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T18:00:00.000000000Z                          18                          36.9                          23.3
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T19:00:00.000000000Z                          22                          36.6                          23.1
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    home                 Kitchen  2022-01-01T20:00:00.000000000Z                          26                          36.5                          22.7

数据下采样

数据下采样是一种提高查询性能并优化长期数据存储的策略。简而言之,下采样可以减少查询返回的点数,而不会丢失数据的总体趋势。

有关数据下采样的更多信息,请参阅数据下采样。

最常见的数据下采样方法是按时间间隔或“窗口”。例如,您可能希望查询过去一小时的数据,并返回每五分钟窗口的平均值。

使用aggregateWindow()按指定的时间间隔对数据进行下采样:

  • 使用 every 参数指定每个窗口的持续时间。

  • 使用 fn 参数指定要应用于每个窗口的聚合函数或选择器函数。

  • (可选)使用 timeSrc 参数指定要使用哪个列值来为每个窗口创建新的聚合时间戳。默认值为 _stop。

influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "temp")|> aggregateWindow(every: 2h, fn: mean)
'
root@c2ecea1e4729:/# influx query 'from(bucket: "influxdb")|> range(start: 2022-01-01T14:00:00Z, stop: 2022-01-01T20:00:01Z)|> filter(fn: (r) => r._measurement == "home")|> filter(fn: (r) => r._field == "temp")|> aggregateWindow(every: 2h, fn: mean)
'
Result: _result
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T16:00:00.000000000Z                         22.75
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T18:00:00.000000000Z            22.549999999999997
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T20:00:00.000000000Z            23.200000000000003
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home                 Kitchen  2022-01-01T20:00:01.000000000Z                          22.7
Table: keys: [_start, _stop, _field, _measurement, room]_start:time                      _stop:time           _field:string     _measurement:string             room:string                      _time:time                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ------------------------------  ----------------------------
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T16:00:00.000000000Z                          22.3
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T18:00:00.000000000Z                          22.5
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T20:00:00.000000000Z                         22.65
2022-01-01T14:00:00.000000000Z  2022-01-01T20:00:01.000000000Z                    temp                    home             Living Room  2022-01-01T20:00:01.000000000Z                          22.2

Automate processing with InfluxDB tasks

InfluxDB tasks are scheduled queries that can perform any of the data processing operations described above. Generally tasks then use the to() function to write the processed result back to InfluxDB.

For more information about creating and configuring tasks, see Get started with InfluxDB tasks.

Go操作Influxdb2

完整代码

安装依赖

First, you need to create a new go module. Run the commands below in your terminal.

mkdir -p influxdb_go_client
cd influxdb_go_client
go mod init influxdb_go_client
touch main.go
go get github.com/influxdata/influxdb-client-go/v2

获取Token

InfluxDB Cloud使用令牌来验证API访问。我们已为此设置过程为您创建了一个全访问令牌。

LVbJ5lreeauQy8QpILuBoTrqArti8hTRtrQDSG4_xoQefYIPl9yqJFYBt03a2kO5GNgcgKU0diYIMnEnJCVsYg==

初始化客户端

package mainimport influxdb2 "github.com/influxdata/influxdb-client-go/v2"const (TOKEN = "LVbJ5lreeauQy8QpILuBoTrqArti8hTRtrQDSG4_xoQefYIPl9yqJFYBt03a2kO5GNgcgKU0diYIMnEnJCVsYg=="url = "http://127.0.0.1:8086"
)
func main() {client := influxdb2.NewClient(url, TOKEN)defer client.Close()
}

写入数据

func main() {client := influxdb2.NewClient(url, TOKEN)defer client.Close()org := "influxdb"bucket := "influxdb"writeAPI := client.WriteAPIBlocking(org, bucket)for value := 0; value < 5; value++ {tags := map[string]string{"tagname1": "tagvalue1",}fields := map[string]interface{}{"field1": value,}point := write.NewPoint("measurement1", tags, fields, time.Now())time.Sleep(1 * time.Second)if err := writeAPI.WritePoint(context.Background(), point); err != nil {log.Fatal(err)}}	
}

执行简单查询

influx query 'from(bucket: "influxdb")|> range(start: -10m)
'
 	org := "influxdb"queryAPI := client.QueryAPI(org)query := `from(bucket: "influxdb")|> range(start: -10m)|> filter(fn: (r) => r._measurement == "measurement1")`results, err := queryAPI.Query(context.Background(), query)if err != nil {log.Fatal(err)}for results.Next() {fmt.Println(results.Record())}if err := results.Err(); err != nil {log.Fatal(err)}

执行汇总查询

	org := "influxdb"queryAPI := client.QueryAPI(org)query := `from(bucket: "influxdb")|> range(start: -10m)|> filter(fn: (r) => r._measurement == "measurement1")|> mean()`results, err := queryAPI.Query(context.Background(), query)if err != nil {log.Fatal(err)}for results.Next() {fmt.Println(results.Record())}if err := results.Err(); err != nil {log.Fatal(err)}

Node操作Influxdb2

完整代码

安装依赖

First, you need to create a new go module. Run the commands below in your terminal.

mkdir -p influxdb_node_client
cd influxdb_go_client
npm install --save @influxdata/influxdb-client

获取Token

InfluxDB Cloud使用令牌来验证API访问。我们已为此设置过程为您创建了一个全访问令牌。

LVbJ5lreeauQy8QpILuBoTrqArti8hTRtrQDSG4_xoQefYIPl9yqJFYBt03a2kO5GNgcgKU0diYIMnEnJCVsYg==

初始化客户端

相关文章:

influxdb时序数据库

以下概念及操作均来自influxdb2 官方文档 InfluxDB2 is the platform purpose-built to collect, store, process and visualize time series data. Time series data is a sequence of data points indexed in time order. Data points typically consist of successive meas…...

OpenCV CUDA模块图像处理------颜色空间处理之用于执行伽马校正(Gamma Correction)函数gammaCorrection()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 cv::cuda::gammaCorrection 是 OpenCV 的 CUDA 模块中用于执行伽马校正&#xff08;Gamma Correction&#xff09;的一个函数。伽马校正通常用于…...

机器学习10-随机森林

随机森林学习笔记 一、随机森林简介 随机森林&#xff08;Random Forest&#xff09;是一种集成学习算法&#xff0c;基于决策树构建模型。它通过组合多个决策树的结果来提高模型的准确性和稳定性。随机森林的核心思想是利用“集成”的方式&#xff0c;将多个弱学习器组合成一…...

商品条形码查询接口如何用C#进行调用?

一、什么是商品条码查询接口&#xff1f; 1974年6月26日&#xff0c;美国俄亥俄州的一家超市首次使用商品条码完成结算&#xff0c;标志着商品条码正式进入商业应用领域。这项技术通过自动识别和数据采集&#xff0c;极大提升了零售行业的作业效率&#xff0c;减少了人工录入错…...

编译pg_duckdb步骤

1. 要求cmake的版本要高于3.17&#xff0c;可以通过下载最新的cmake的程序&#xff0c;然后设置.bash_profile的PATH环境变量&#xff0c;将最新的cmake的bin目录放到PATH环境变量的最前面 2. g的版本要支持c17标准&#xff0c;否则会报 error ‘invoke_result in namespace ‘…...

多模态大语言模型arxiv论文略读(九十一)

FineCLIPER: Multi-modal Fine-grained CLIP for Dynamic Facial Expression Recognition with AdaptERs ➡️ 论文标题&#xff1a;FineCLIPER: Multi-modal Fine-grained CLIP for Dynamic Facial Expression Recognition with AdaptERs ➡️ 论文作者&#xff1a;Haodong C…...

攻防世界 - MISCall

下载得到一个没有后缀的文件&#xff0c;把文件放到kali里面用file命令查看 发现是bzip2文件 解压 变成了.out文件 查看发现了一个压缩包 将其解压 发现存在.git目录和一个flag.txt&#xff0c;flag.txt是假的 恢复git隐藏文件 查看发现是将flag.txt中内容读取出来然后进行s…...

数据结构测试模拟题(2)

1、选择排序&#xff08;输出过程&#xff09; #include <iostream> using namespace std;int main() {int a[11]; // 用a[1]到a[10]来存储输入// 读取10个整数for(int i 1; i < 10; i) {cin >> a[i];}// 选择排序过程&#xff08;只需9轮&#xff09;for(int…...

在PyTorch中,对于一个张量,如何快速为多个元素赋值相同的值

我们以“a torch.arange(12).reshape((3, -1))”为例&#xff0c;a里面现在是&#xff1a; 如果我们想让a的右下角的2行3列的元素都为10的话&#xff0c;可以如何快速实现呢&#xff1f; 我们可以用到索引和切片技术&#xff0c;执行如下的指令即可达到目标&#xff1a; a[1…...

苍穹外卖--Redis

1.Redis入门 1.1Redis简介 Redis是一个基于内存的key-value结果数据库 基于内存存储&#xff0c;读写性能高 适合存储热点数据(热点商品、资讯、新闻) 企业应用广泛 Redis的Windows版属于绿色软件&#xff0c;直接解压即可使用&#xff0c;解压后目录结构如下&#xff1a…...

C++ 条件变量虚假唤醒问题的解决

在 C 中&#xff0c;std::condition_variable 的 wait 和 wait_for 方法除了可以传入一个锁&#xff08;std::unique_lock&#xff09;&#xff0c;还可以传入一个谓词函数&#xff08;函数或可调用对象&#xff09;。这个谓词的作用是让条件变量在特定的条件满足时才退出等待。…...

深度学习————注意力机制模块

关于注意力机制我自己的一点理解&#xff1a;建立各个维度数据之间的关系&#xff0c;就是对已经处理为特征图的数据&#xff0c;将其他影响因素去除&#xff08;比如通道注意力&#xff0c;就将空间部分的影响因素消除或者减到极小&#xff09;再对特征图进行以此特征提取 以此…...

openssl 使用生成key pem

好的&#xff0c;以下是完整的步骤&#xff0c;帮助你在 Windows 系统中使用 OpenSSL 生成私钥&#xff08;key&#xff09;和 PEM 文件。假设你的 openssl.cnf 配置文件位于桌面。 步骤 1&#xff1a;打开命令提示符 按 Win R 键&#xff0c;打开“运行”对话框。输入 cmd&…...

python:基础爬虫、搭建简易网站

一、基础爬虫代码&#xff1a; # 导包 import requests # 从指定网址爬取数据 response requests.get("http://192.168.34.57:8080") print(response) # 获取数据 print(response.text)二、使用FastAPI快速搭建网站&#xff1a; # TODO FastAPI 是一个现代化、快速…...

好坏质检分类实战(异常数据检测、降维、KNN模型分类、混淆矩阵进行模型评估)

任务 好坏质检分类实战 task: 1、基于 data_class_raw.csv 数据&#xff0c;根据高斯分布概率密度函数&#xff0c;寻找异常点并剔除 2、基于 data_class_processed.csv 数据&#xff0c;进行 PCA 处理&#xff0c;确定重要数据维度及成分 3、完成数据分离&#xff0c;数据分离…...

链表:数据结构的灵动舞者

在数据结构的舞台之上&#xff0c;链表以它灵动的身姿演绎着数据的精彩故事。与顺序表的规整有序不同&#xff0c;链表展现出了别样的灵活性与独特魅力。今天&#xff0c;就让我们一同走进链表的世界&#xff0c;去领略它的定义、结构、操作&#xff0c;对比它与顺序表的优缺点…...

YOLOv4:目标检测的新标杆

引言 YOLO(You Only Look Once)系列作为目标检测领域的经典算法&#xff0c;以其高效的检测速度和良好的准确率闻名。2020年推出的YOLOv4在保持YOLO系列高速检测特点的同时&#xff0c;通过引入多项创新技术&#xff0c;将检测性能提升到了新高度。本文将详细介绍YOLOv4的核心…...

PyTorch 2.1新特性:TorchDynamo如何实现30%训练加速(原理+自定义编译器开发)

一、PyTorch 2.1动态编译架构演进 PyTorch 2.1的发布标志着深度学习框架进入动态编译新纪元。其核心创新点TorchDynamo通过字节码即时重写技术&#xff0c;将Python动态性与静态图优化完美结合。相较于传统JIT方案&#xff0c;TorchDynamo实现了零侵入式加速——开发者只需添加…...

LabVIEW通用测控平台设计

基于 LabVIEW 图形化编程环境&#xff0c;设计了一套适用于工业自动化、科研测试领域的通用测控平台。通过整合研华、NI等品牌硬件&#xff0c;实现多类型数据采集、实时控制及可视化管理。平台采用模块化架构&#xff0c;支持硬件灵活扩展&#xff0c;解决了传统测控系统开发周…...

【机器学习基础】机器学习入门核心算法:K-近邻算法(K-Nearest Neighbors, KNN)

机器学习入门核心算法&#xff1a;K-近邻算法&#xff08;K-Nearest Neighbors, KNN&#xff09; 一、算法逻辑1.1 基本概念1.2 关键要素距离度量K值选择 二、算法原理与数学推导2.1 分类任务2.2 回归任务2.3 时间复杂度分析 三、模型评估3.1 评估指标3.2 交叉验证调参 四、应用…...

FastMoss 国际电商Tiktok数据分析 JS 逆向 | MD5加密

1.目标 目标网址&#xff1a;https://www.fastmoss.com/zh/e-commerce/saleslist 切换周榜出现目标请求 只有请求头fm-sign签名加密 2.逆向分析 直接搜fm-sign 可以看到 i["fm-sign"] A 进入encryptParams方法 里面有个S()方法加密&#xff0c;是MD5加密 3.代…...

Redis分布式缓存核心架构全解析:持久化、高可用与分片实战

一、持久化机制&#xff1a;数据安全双引擎 1.1 RDB与AOF的架构设计 Redis通过RDB&#xff08;快照持久化&#xff09;和AOF&#xff08;日志持久化&#xff09;两大机制实现数据持久化。 • RDB架构&#xff1a;采用COW&#xff08;写时复制&#xff09;技术&#xff0c;主进程…...

【Linux】基础开发工具(下)

文章目录 一、自动化构建工具1. 什么是 make 和 Makefile&#xff1f;2. 如何自动化构建可执行程序&#xff1f;3. Makefile 的核心思想4. 如何清理可执行文件&#xff1f;5. make 的工作原理5.1 make 的执行顺序5.2 为什么 make 要检查文件是否更新&#xff1f;5.2.1 避免重复…...

Python爬虫实战:研究Portia框架相关技术

1. 引言 1.1 研究背景与意义 在大数据时代,网络数据已成为企业决策、学术研究和社会分析的重要资源。据 Statista 统计,2025 年全球数据总量将达到 175ZB,其中 80% 以上来自非结构化网络内容。如何高效获取并结构化这些数据,成为数据科学领域的关键挑战。 传统爬虫开发需…...

chrome打不开axure设计的软件产品原型问题解决办法

1、打开原型文件夹&#xff0c;进入到其中的如下目录中&#xff1a;resources->chrome->axure-chrome-extension.crx&#xff0c;找到 Axure RP Extension for Chrome插件。 2、axure-chrome-extension.crx文件修改扩展名.rar&#xff0c;并解压到文件夹 axure-chrome-ex…...

达梦数据库-学习-23-获取执行计划的N种方法

目录 一、环境信息 二、说点什么 三、测试数据生成 四、测试语句 五、获取执行计划方法 1、EXPLAIN &#xff08;1&#xff09;样例 &#xff08;2&#xff09;优势 &#xff08;3&#xff09;劣势 2、ET &#xff08;1&#xff09;开启参数 &#xff08;2&#xff…...

【数据结构】树形结构--二叉树

【数据结构】树形结构--二叉树 一.知识补充1.什么是树2.树的常见概念 二.二叉树&#xff08;Binary Tree&#xff09;1.二叉树的定义2.二叉树的分类3.二叉树的性质 三.二叉树的实现1.二叉树的存储2.二叉树的遍历①.先序遍历②.中序遍历③.后序遍历④.层序遍历 一.知识补充 1.什…...

Baklib构建企业CMS高效协作与安全管控体系

企业CMS高效协作体系构建 基于智能工作流引擎的设计逻辑&#xff0c;现代企业内容管理系统通过预设多节点审核路径与自动化任务分配机制&#xff0c;有效串联市场、技术、法务等跨部门协作链路。系统支持多人同时编辑与版本追溯功能&#xff0c;结合细粒度权限管控模块&#x…...

深入理解 JDK、JRE 和 JVM 的区别

在 Java 中&#xff0c;JDK、JRE 和 JVM 是非常重要的概念&#xff0c;它们各自扮演着不同的角色&#xff0c;却又紧密相连。今天&#xff0c;就让我们来详细探讨一下它们之间的区别。 一、JVM JVM 即 Java 虚拟机&#xff0c;它是整个 Java 技术体系的核心。JVM 提供了 Java…...

LSTM 与 TimesNet的时序分析对比解析

前言 Hi&#xff0c;我是GISerLiu&#x1f642;, 这篇文章是参加2025年5月Datawhale学习赛的打卡文章&#xff01;&#x1f4a1; 本文将深入探讨在自定义时序数据集上进行下游分类任务的两种主流分析方法。一种是传统的“先插补后分析”策略&#xff0c;另一种是采用先进的端到…...