/query 1.x 兼容性 API
/query
1.x 兼容性端点使用 InfluxQL 查询 InfluxDB 2.7。使用 GET
请求方法从 /query
端点查询数据。
GET http://localhost:8086/query
/query
兼容性端点使用查询请求中指定的 database 和 retention policy 将请求映射到 InfluxDB 存储桶。有关更多信息,请参阅 数据库和保留策略映射。
身份验证
使用以下身份验证方法之一
- 令牌身份验证
- 使用用户名和密码的基本身份验证
- 使用用户名和密码的查询字符串身份验证
有关更多信息,请参阅 身份验证。
查询字符串参数
u
(可选) 用于验证请求的 1.x 用户名。请参阅 查询字符串身份验证。
p
(可选) 用于验证请求的 1.x 密码。请参阅 查询字符串身份验证。
db
(必需) 要从中查询数据的 database。这映射到 InfluxDB 存储桶。请参阅 数据库和保留策略映射。
rp
要从中查询数据的 retention policy。这映射到 InfluxDB 存储桶。请参阅 数据库和保留策略映射。
q
(必需) 要执行的 InfluxQL 查询。要执行多个查询,请使用分号 (;
) 分隔查询。
epoch
以指定的精度返回带有 Unix 时间戳(也称为 epoch 时间戳)的结果,而不是带有纳秒精度的 RFC3339 时间戳。以下精度可用
ns
- 纳秒u
或µ
- 微秒ms
- 毫秒s
- 秒m
- 分钟h
- 小时
查询示例
使用基本身份验证查询
#######################################
# Use Basic authentication with an
# InfluxDB 1.x compatible username and password
# to query the InfluxDB 1.x compatibility API.
#
# Replace INFLUX_USERNAME with your 1.x-compatible username.
# Replace INFLUX_PASSWORD_OR_TOKEN with your InfluxDB API token
# or 1.x-compatible password.
#######################################
# Use the default retention policy.
#######################################
# Use the --user option with `--user <username>:<password>` syntax
# or the `--user <username>` interactive syntax to ensure your credentials are
# encoded in the header.
#######################################
curl --get "http://localhost:8086/query" \
--user "INFLUX_USERNAME":"INFLUX_PASSWORD_OR_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "q=SELECT * FROM cpu_usage"
/**
* Use Basic authentication with an
* InfluxDB 1.x compatible username and password
* to query the InfluxDB 1.x compatibility API.
* Replace INFLUX_USERNAME with your 1.x-compatible username.
* Replace INFLUX_PASSWORD_OR_TOKEN with your InfluxDB API token
* or 1.x-compatible password.
* Use the default retention policy.
*/
const https = require('https');
const querystring = require('querystring');
function queryWithUsername() {
const queryparams = {
db: 'mydb',
q: 'SELECT * FROM cpu_usage',
};
const options = {
host: 'localhost:8086',
path: '/query?' + querystring.stringify(queryparams),
auth: 'INFLUX_USERNAME:INFLUX_PASSWORD_OR_TOKEN',
headers: {
'Content-type': 'application/json'
},
};
const request = https.get(options, (response) => {
let rawData = '';
response.on('data', () => {
response.on('data', (chunk) => { rawData += chunk; });
})
response.on('end', () => {
console.log(rawData);
})
});
request.end();
}
查询非默认保留策略
curl --get http://localhost:8086/query \
--header "Authorization: Token API_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "rp=customrp" \
--data-urlencode "q=SELECT used_percent FROM mem WHERE host=host1"
执行多个查询
curl --get http://localhost:8086/query \
--header "Authorization: Token API_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "q=SELECT * FROM mem WHERE host=host1;SELECT mean(used_percent) FROM mem WHERE host=host1 GROUP BY time(10m)"
返回带有毫秒 Unix 时间戳的查询结果
curl --get http://localhost:8086/query \
--header "Authorization: Token API_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "rp=myrp" \
--data-urlencode "q=SELECT used_percent FROM mem WHERE host=host1" \
--data-urlencode "epoch=ms"
从文件执行 InfluxQL 查询
curl --get http://localhost:8086/query \
--header "Authorization: Token API_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "q@path/to/influxql.txt" \
--data-urlencode "async=true"
替换以下内容
API_TOKEN
: 您的 InfluxDB API 令牌
此页内容是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。要寻求支持,请使用以下资源
拥有年度合同或支持合同的客户 可以联系 InfluxData 支持。