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==

初始化客户端

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/907217.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛谷 P3372 【模板】线段树 1

【题目链接】 洛谷 P3372 【模板】线段树 1 【题目考点】 1. 线段树 2. 树状数组 【解题思路】 本题要求维护区间和&#xff0c;实现区间修改、区间查询。 可以使用树状数组或线段树完成该问题&#xff0c;本文仅介绍使用线段树的解法。 解法1&#xff1a;线段树 线段树…

软件更新 | TSMaster 202504 版本已上线!三大功能让车载测试更智能

车载测试的智能化时代正在加速到来&#xff01;TSMaster 202504 版本正式发布&#xff0c;本次更新聚焦以太网通信与数据高效处理&#xff0c;带来三大核心功能升级—以太网报文信息过滤、XCP on Ethernet支持、按时间范围离线回放&#xff0c;助力工程师更精准、更灵活地完成测…

java-单列集合list与set。

集合定位&#xff1a;存储数据的容器 与数组的区别&#xff1a; 数组只能存储同种数据类型数据&#xff0c;集合可以存储不同类型的数据。 数组的长度一旦创建长度不可变&#xff0c;集合的长度是可变的 数组的操作单一&#xff0c;集合的操作比较丰富&#xff08;增删改查&…

ai之pdf解析工具 PPStructure 还是PaddleOCR

目录 重点是四 先用 PPStructure 版面分析,分成不同的块儿,再选用 PaddleOCR、或PPStructure基础路径OCR模型配置OCR模型配置GPU配置硬件配置性能配置一、框架选型对比分析1. **PaddleOCR核心能力**2. **PP-Structure核心能力**3. **选型结论**二、错误根因分析与修复方案1. …

Android计算机网络学习总结

TCP vs UDP 核心区别​​ ​题目​&#xff1a;TCP为什么称为可靠传输协议&#xff1f;UDP在哪些场景下比TCP更具优势&#xff1f; ​得分要点​&#xff1a; ​可靠性机制​ 三握四挥建立可靠连接确认应答&#xff08;ACK&#xff09; 超时重传滑动窗口流量控制拥塞控制&…

深入解析Java组合模式:构建灵活树形结构的艺术

引言&#xff1a;当对象需要树形组织时 在日常开发中&#xff0c;我们经常需要处理具有层次结构的对象集合。比如&#xff1a; 文件系统中的文件夹与文件GUI界面中的容器与控件企业组织架构中的部门与员工 这类场景中的对象呈现明显的整体-部分层次结构&#xff0c;如何优雅…

mobaxterm通过ssh登录docker无图形界面

1. 流程 下面是使用Mobaxterm通过SSH登录Docker无图形界面的步骤&#xff1a; 步骤 操作 1 在本地安装Mobaxterm 2 配置Mobaxterm连接SSH 3 启动Docker容器 4 在Mobaxterm中通过SSH连接到Docker容器 2. 操作步骤 步骤1&#xff1a;安装Mobaxterm 首先&#xff…

【赵渝强老师】HBase的体系架构

HBase是大表&#xff08;BigTable&#xff09;思想的一个具体实现。它是一个列式存储的NoSQL数据库&#xff0c;适合执行数据的分析和处理。简单来说&#xff0c;就是适合执行查询操作。从体系架构的角度看&#xff0c;HBase是一种主从架构&#xff0c;包含&#xff1a;HBase H…

linux 新增驱动宏config.in配置

‌1. 添加配置宏步骤‌ ‌1.1 修改 Kconfig&#xff08;推荐方式&#xff09;‌ ‌定位 Kconfig 文件‌ 内核各子目录&#xff08;如 drivers/char/&#xff09;通常包含 Kconfig 文件&#xff0c;用于定义模块配置选项7。‌添加宏定义‌ 示例&#xff1a;在 drivers/char/Kc…

关于git的使用

下载git 可以去git的官网下载https://git-scm.com/downloads 也可以去找第三方的资源下载&#xff0c;下载后是一个exe应用程序&#xff0c;直接点开一直下一步就可以安装了 右键任意位置显示这两个就代表成功&#xff0c;第一个是git官方的图形化界面&#xff0c;第二个是用…

WPF【11_8】WPF实战-重构与美化(UI 与视图模型的联动,实现INotifyPropertyChanged)

11-13 【重构】INotifyPropertyChanged 与 ObservableCollection 现在我们来完成新建客户的功能。 当用户点击“客户添加”按钮以后系统会清空当前所选定的客户&#xff0c;客户的详细信息以及客户的预约记录会从 UI 中被清除。然后我们就可以在输入框中输入新的客户信息了&am…

ArkUI:鸿蒙应用响应式与组件化开发指南(一)

文章目录 引言1.ArkUI核心能力概览1.1状态驱动视图1.2组件化&#xff1a;构建可复用UI 2.状态管理&#xff1a;从单一组件到全局共享2.1 状态装饰器2.2 状态传递模式对比 引言 鸿蒙生态正催生应用开发的新范式。作为面向全场景的分布式操作系统&#xff0c;鸿蒙的北向应用开发…

List优雅分组

一、前言 最近小永哥发现&#xff0c;在开发过程中&#xff0c;经常会遇到需要对list进行分组&#xff0c;就是假如有一个RecordTest对象集合&#xff0c;RecordTest对象都有一个type的属性&#xff0c;需要将这个集合按type属性进行分组&#xff0c;转换为一个以type为key&…

AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理

引言 自然语言处理&#xff08;Natural Language Processing, NLP&#xff09;是人工智能领域中最具活力和潜力的分支之一。从智能客服到机器翻译&#xff0c;再到语音识别&#xff0c;NLP技术正以其强大的功能改变着我们的生活方式和工作模式。 Catalyst的推出极大降低了NLP…

MySQL 8.0 OCP 1Z0-908 题目解析(13)

题目49 Choose the best answer. t is a non - empty InnoDB table. Examine these statements, which are executed in one session: BEGIN; SELECT * FROM t FOR UPDATE;Which is true? ○ A) mysqlcheck --analyze --all - databases will execute normally on all ta…

Docker 一键部署倒计时页面:Easy Countdown全设备通用

Easy Countdown 介绍 Easy countdown是一个易于设置的倒计时页面。可以设置为倒计时或计时器。可用于个人生活、工作管理、教育、活动策划等多个领域。 &#x1f6a2; 项目地址 Github&#xff1a;https://github.com/Yooooomi/easy-countdown &#x1f680;Easy Countdown …

Python训练打卡Day35

模型可视化与推理 知识点回顾&#xff1a; 三种不同的模型可视化方法&#xff1a;推荐torchinfo打印summary权重分布可视化进度条功能&#xff1a;手动和自动写法&#xff0c;让打印结果更加美观推理的写法&#xff1a;评估模式 模型结构可视化 理解一个深度学习网络最重要的2点…

四、生活常识

一、效应定律 效应 1、沉没成本效应 投入的越多&#xff0c;退出的难度就越大&#xff0c;因为不甘心自己之前的所有付出都付之东流。 2、破窗效应 干净的环境下&#xff0c;没有人会第一个丢垃圾&#xff0c;但是当环境变得糟糕&#xff0c;人们就开始无所妒忌的丢垃圾。…

机器学习圣经PRML作者Bishop20年后新作中文版出版!

机器学习圣经PRML作者Bishop20年后新书《深度学习&#xff1a;基础与概念》出版。作者克里斯托弗M. 毕晓普&#xff08;Christopher M. Bishop&#xff09;微软公司技术研究员、微软研究 院 科学智 能 中 心&#xff08;Microsoft Research AI4Science&#xff09;负责人。剑桥…

Python应用嵌套猜数字小游戏

大家好!今天向大家分享的是有关“嵌套”的猜数字小游戏。希望能够帮助大家理解嵌套。 代码呈现: # 1. 构建一个随机的数字变量 import random num random.randint(1, 10)guess_num int(input("输入你要猜测的数字&#xff1a; "))# 2. 通过if判断语句进行数字的猜…