初始配置
The initial setup process for an InfluxDB instance creates the following:
An organization with the name you provide.
A primary bucket with the name you provide.
An admin authorization with the following properties:
An API token (operator token). — 授予对 InfluxDB OSS 2.x 中所有组织和所有组织资源的完全读写访问权限;
Read-write permissions for all resources in the InfluxDB instance.
服务配置
服务访问方式
influx CLI
HTTP RESTful /api/v2
Web UI
创建初始用户
Enter a Username for your initial user.
Enter a Password and Confirm Password for your user.
Enter your initial Organization Name.
Enter your initial Bucket Name.
Click Continue.
All-Access Token
Create an API token in InfluxDB | InfluxDB OSS 2.6 Documentation
因为 Operator Token 对数据库中的所有组织具有完全的读写访问权限,所以我们建议为每个组织创建一个 All-Access Token 并使用这些令牌来管理 InfluxDB;
数据写入
数据写入方式
Influx user interface (UI)
InfluxDB HTTP API
influx CLI
Telegraf
InfluxDB client libraries
Line protocol
Line protocol 是在讲 InfluxDB 的数据格式。写入 InfluxDB 的所有数据均使用 Line protocol 写入,这是一种基于文本的格式,可让您提供必要的信息以将数据点写入 InfluxDB;
measurement: String that identifies the measurement to store the data in.
tag set: Comma-delimited list of key value pairs, each representing a tag. Tag keys and values are unquoted strings. Spaces, commas, and equal characters must be escaped.
field set: Comma-delimited list key value pairs, each representing a field. Field keys are unquoted strings. Spaces and commas must be escaped. Field values can be strings (quoted), floats, integers, unsigned integers, or booleans.
timestamp: Unix timestamp associated with the data. InfluxDB supports up to nanosecond precision. If the precision if the timestamp is not in nanoseconds, you must specify the precision when writing the data to InfluxDB.
能构造出该格式,便能向 InfluxDB 中写入数据:
export INFLUX_HOST=http://localhost:8086 export INFLUX_ORG=<YOUR_INFLUXDB_ORG> export INFLUX_TOKEN=<YOUR_INFLUXDB_API_TOKEN> curl --request POST \ "$INFLUX_HOST/api/v2/write?org=$INFLUX_ORG&bucket=get-started&precision=s" \ --header "Authorization: Token $INFLUX_TOKEN" \ --header "Content-Type: text/plain; charset=utf-8" \ --header "Accept: application/json" \ --data-binary " 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=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 "
数据查询
查询工具
InfluxDB user interface (UI)
InfluxDB HTTP API
influx CLI
Chronograf
Grafana
InfluxDB client libraries
Flux
如下示例:
from(bucket: "get-started") |> 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")
InfluxQL
InfluxQL is a SQL-like query language similar to most SQL languages, but specifically designed to query time series data from InfluxDB 0.x and 1.x.
鉴于此,我们不再关注 InfluxQL 的使用方法;
数据处理
WIP
数据展示
针对我们的场景,我们使用 Grafana 来展示 InfluxDB 中的数据;