/查询 1.x 兼容性 API
该 /query 1.x 兼容性端点使用 InfluxQL 查询 InfluxDB 2.7。
使用 GET 请求方法从 /query 端点查询数据。
GET http://localhost:8086/query
该 /query 兼容性端点使用在查询请求中指定的 数据库 和 保留策略 将请求映射到 InfluxDB 桶。有关更多信息,请参见 数据库和保留策略映射。
认证
使用以下认证方法之一:
- 令牌认证
- 使用用户名和密码的基本认证
- 使用用户名和密码进行查询字符串认证
有关更多信息,请参见 Authentication。
查询字符串参数
你
(可选) 1.x username 用于验证请求。
参见 query string authentication.正义
(可选) 1.x 密码 用于验证请求。
查看 查询字符串认证。数据库
(必填) 查询数据的数据库。 这映射到一个 InfluxDB 存储桶。 请参阅 数据库和保留策略映射。
rp
从查询数据的保留策略。这与InfluxDB 存储桶映射。见数据库和保留策略映射。
q
(必填) 要执行的 InfluxQL 查询。
要执行多个查询,请用分号 (;) 分隔查询。
纪元
返回具有 Unix 时间戳(也称为纪元时间戳)在指定精度的结果,而不是具有纳秒精度的 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();
}