InfluxDB 1.x 兼容性 API
InfluxDB v2 API 包含与 InfluxDB 1.x 客户端库和第三方集成(如 Grafana 等)兼容的 InfluxDB 1.x 端点。
认证
InfluxDB 1.x 兼容性端点要求所有查询和写入请求都经过认证,使用 API token 或 1.x 兼容的 凭据。
使用令牌方案进行身份验证
令牌认证需要以下凭据:
- token: InfluxDB API 令牌
使用 Authorization 头与 Token 方案将您的令牌提供给 InfluxDB。
语法
Authorization: Token INFLUX_API_TOKEN
示例
#######################################
# Use a token in the Authorization header
# to query the InfluxDB 1.x compatibility API.
#
# Replace INFLUX_API_TOKEN with your InfluxDB API token.
#######################################
curl --get "http://localhost:8086" \
--header "Authorization: Token INFLUX_API_TOKEN" \
--header 'Content-type: application/json' \
--data-urlencode "db=mydb" \
--data-urlencode "q=SELECT * FROM cpu_usage"
/**
* Use the Token authentication scheme
* to query the InfluxDB 1.x compatibility API.
*
* Replace INFLUX_API_TOKEN with your InfluxDB API token.
*/
const https = require('https');
const querystring = require('querystring');
function queryWithToken() {
const queryparams = {
db: 'mydb',
q: 'SELECT * FROM cpu_usage',
};
const options = {
host: 'localhost:8086',
path: "/query?" + querystring.stringify(queryparams),
headers: {
'Authorization': 'Token INFLUX_API_TOKEN',
'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();
}
使用用户名和密码方案进行身份验证
使用以下认证方案与支持 InfluxDB 1.x 约定的客户端,即 username 和 password(不支持 Authorization: Token 方案):
管理凭据
- username: InfluxDB Cloud 用户名
(使用您注册时的电子邮件地址作为用户名,比如
exampleuser@influxdata.com.) - 密码: InfluxDB Cloud API token
基本认证
使用 Authorization 头与 Basic 方案提供用户名和密码凭据给 InfluxDB。
大多数HTTP客户端提供基本认证选项,该选项接受<username>:<password>语法,并在发送请求之前对凭据进行编码。
语法
Authorization: Basic exampleuser@influxdata.com:INFLUX_API_TOKEN
示例
#######################################
# 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();
}
替换以下内容:
: 您注册时使用的电子邮件地址exampleuser@influxdata.com: 你的 InfluxDB API tokenINFLUX_API_TOKEN
查询字符串认证
使用 InfluxDB 1.x API 参数通过查询字符串提供凭证。
使用查询字符串参数时需要考虑
- 对可能包含空格或其他特殊字符的查询参数进行URL编码。
- 在通过URL暴露敏感数据时要注意风险。
语法
/query/?u=INFLUX_USERNAME&p=INFLUX_API_TOKEN
/write/?u=INFLUX_USERNAME&p=INFLUX_API_TOKEN
示例
#######################################
# 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();
}
替换以下内容:
: 您注册时使用的电子邮件地址exampleuser@influxdata.com: 你的 InfluxDB API tokenINFLUX_API_TOKEN
InfluxQL 支持
兼容性API支持InfluxQL,但有以下注意事项:
- 不支持
INTO子句(例如,SELECT ... INTO ...)。 - 除了
DELETE和DROP MEASUREMENT查询仍然被允许,InfluxQL数据库管理命令不被支持。
兼容性端点
/查询
该 /query 1.x 兼容性端点使用 InfluxQL 查询 InfluxDB Cloud 和 InfluxDB OSS 2.x。
GET http://localhost:8086/query
/写
该 /write 1.x 兼容性端点使用来自 InfluxDB 1.x /write API 端点的模式将数据写入 InfluxDB Cloud 和 InfluxDB OSS 2.x。
POST http://localhost:8086/write
数据库和保留策略映射
数据库和保留策略(DBRP)映射服务将 InfluxDB 1.x 数据库和保留策略组合映射到 InfluxDB Cloud 和 InfluxDB OSS 2.x 存储桶。