使用InfluxQL查询数据
学习如何使用 InfluxQL 查询存储在 InfluxDB 集群中的数据。
使用InfluxQL探索您的模式
使用 InfluxQL SHOW 语句来返回关于您的数据架构的信息。
列出测量
SHOW MEASUREMENTS
列出测量中的字段键
SHOW FIELD KEYS FROM "measurement"
列出测量中的标签键
SHOW TAG KEYS FROM "measurement"
列出特定标签键的标签值
SHOW TAG VALUES FROM "measurement" WITH KEY = "tag-key" WHERE time > now() - 1d
执行基本的InfluxQL查询
一个基本的 InfluxQL 查询从 InfluxDB 查询数据通常包括 SELECT、FROM 和 WHERE 子句。
SELECT temp, room FROM home WHERE time >= now() - 1d
使用InfluxQL聚合数据
使用InfluxQL聚合和选择函数对你的时间序列数据进行聚合操作。
按组聚合字段
SELECT
MEAN(temp) AS mean,
FIRST(hum) as first,
FROM home
GROUP BY tag
按时间区间聚合
SELECT
MEAN(temp),
sum(hum),
FROM home
WHERE time >= now() - 24h
GROUP BY time(1h),room
排查 InfluxQL 错误
学习如何排查和修复常见的InfluxQL错误。
使用参数化查询与InfluxQL
使用参数化查询来防止注入攻击,并使查询更加可重用。
使用Go和influxdb3-go客户端
// Use the $parameter syntax to reference parameters in a query.
// The following InfluxQL query contains $room and $min_time parameters.
query := `
SELECT * FROM home
WHERE time >= $min_time
AND temp >= $min_temp
AND room = $room`
// Assign parameter names to input values.
parameters := influxdb3.QueryParameters{
"room": "Kitchen",
"min_temp": 20.0,
"min_time": "2024-03-18 00:00:00.00",
}
// Call the client's function to query InfluxDB with parameters and the
// the InfluxQL QueryType.
iterator, err := client.QueryWithParameters(context.Background(),
query,
parameters,
influxdb3.WithQueryType(influxdb3.InfluxQL))