/查询 1.x 兼容性 API
该 /query 1.x 兼容性端点使用 InfluxQL 查询 InfluxDB Cloud。 使用 GET 请求方法从 /query 端点查询数据。
GET http://localhost:8086/query
该 /query 兼容性端点使用在查询请求中指定的 数据库 和 保留策略 将请求映射到 InfluxDB 桶。有关更多信息,请参见 数据库和保留策略映射。
如果您有一个现有的存储桶,它不遵循 database/retention-policy 命名约定,您 必须 手动创建一个数据库和保留策略映射 以便使用 /query 兼容 API 查询该存储桶。
认证
使用以下认证方法之一:
- 令牌认证
- 使用用户名和密码的基本认证
- 使用用户名和密码进行查询字符串认证
有关更多信息,请参见 Authentication。
查询字符串参数
你
(可选) 1.x username 用于验证请求。
参见 query string authentication.正义
(可选) 1.x 密码 用于验证请求。
查看 查询字符串认证。数据库
(必填) 查询数据的数据库。 这映射到一个 InfluxDB 存储桶。 请参阅 数据库和保留策略映射。
rp
从查询数据的保留策略。这与InfluxDB 存储桶映射。见数据库和保留策略映射。
q
(必填) 要执行的 InfluxQL 查询。
要执行多个查询,请用分号 (;) 分隔查询。
纪元
返回具有 Unix 时间戳(也称为纪元时间戳)在指定精度的结果,而不是具有纳秒精度的 RFC3339 时间戳。可用的精度如下:
ns- 纳秒u或µ- 微秒ms- 毫秒s- 秒m- 分钟h- 小时
查询示例
使用基本认证查询
#######################################
# Use an InfluxDB 1.x compatible username
# and password with Basic Authentication
# to query the InfluxDB 1.x compatibility API
#######################################
# Use default retention policy
#######################################
# Use the --user option with `--user INFLUX_USERNAME:INFLUX_API_TOKEN` syntax
# or the `--user INFLUX_USERNAME` interactive syntax to ensure your credentials are
# encoded in the header.
#######################################
curl --get "http://localhost:8086/query" \
--user "exampleuser@influxdata.com":"INFLUX_API_TOKEN" \
--data-urlencode "db=mydb" \
--data-urlencode "q=SELECT * FROM cpu_usage"
/**
* Use an InfluxDB Cloud username and token
* with Basic Authentication
* to query the InfluxDB 1.x compatibility API
*/
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: 'exampleuser@influxdata.com:INFLUX_API_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();
}