nodejs 操作influxdb
const { OrgsAPI, BucketsAPI, UsersAPI, AuthorizationsAPI } = require( '@influxdata/influxdb-client-apis');
const { InfluxDB } = require('@influxdata/influxdb-client');
const uuid = require('uuid');
const influxHost = “127.0.0.1”;
const influxPort = 27017;
const url = `http://${influxHost}:${influxPort}`;
const token = 'zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==';
const influxDB = new InfluxDB({url, token});
const name = `ecmaster`; //orgName
// org create
const orgsAPI = new OrgsAPI(influxDB);
const organization = await orgsAPI.postOrgs({body: {name}});
const orgID = organization.id;
//bucket create
const bucketsAPI = new BucketsAPI(influxDB);
const bucket = await bucketsAPI.postBuckets({body: {orgID, name }});
// user create
const usersAPI = new UsersAPI(influxDB);
const user = await usersAPI.postUsers({body: {name}});
const userID = user.id;
const password = uuid.v4();
await usersAPI.postUsersIDPassword({userID, body: {password}});
await orgsAPI.postOrgsIDOwners({ orgID, body: { id: user.id } }); // 当前orgID的所有者,拥有此org中所有资源的权限,没有org的write权限。 此用户登陆web界面创建的API Token分配的资源权限均是此org的。
//APIToken create
/*
API Token permission字段相关参数解释:
[name] 可选: 如果设置了名称,则是该名称的资源的权限。如果未设置,则为该资源类型的所有资源的权限。
[id] 可选: 如果设置了 ID,则是 该 ID 资源的权限。如果未设置,则为该资源类型的所有资源的权限。
[orgID] 可选: 如果设置了 orgID,则这是该orgID组织拥有的所有资源的权限。如果未设置,则为该资源类型的所有资源的权限。
type 必填: "authorizations" "buckets" "dashboards" "orgs" "sources" "tasks" "telegrafs" "users" "variables" "scrapers" "secrets" "labels" "views" "documents" "notificationRules" "notificationEndpoints" "checks" "dbrp" "notebooks" "annotations" "remotes" "replications"
*/
const authorizationsAPI = new AuthorizationsAPI(influxDB);
const apiToken = await authorizationsAPI.postAuthorizations({ body : {orgID, userID: user.id, permissions: [
{
"action": "read",
"resource": {
"orgID": `${orgID}`,
"type": "authorizations"
}
},
{
"action": "read",
"resource": {
"orgID": `${orgID}`,
"type": "buckets"
}
},
{
"action": "read",
"resource": {
"orgID": `${orgID}`,
"type": "users"
}
},
{
"action": "write",
"resource": {
"orgID": `${orgID}`,
"type": "buckets"
}
}
]}
});
const influxSetting= {
org: organization.name,
bucket: bucket.name,
url,
token: apiToken.token,
username: user.name,
password: password
}
console.log(influxSetting);
influxdb-cli
mac安装:
brew install influxdb-cli
influx config create --config-name onboarding \
--host-url "http://ip:8086" \
--token "zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==" \
--active
influx config list
influx config delete onboarding
org相关操作
influx org list 查询 默认limit只显示20个。
influx org list --name pppp_ecmaster //超过20个指定name可查询到
influx org delete -i 07668febbebcbbbd //删除org -i 指定id
bucket相关操作
influx bucket list -o undefined_ecmaster //查看指定org的bucket
influx bucket delete -i 6d10e05825be0228 -o undefined_ecmaster
API Token相关操作
influx auth list -o ecmaster //查看指定org的APIToken
user相关操作
influx user list 查询 默认limit只显示20个
influx user list --name pppp_ecmaster //超过20个 指定name才可查询到
influx user delete -i 0ba14fd3b1efe000 //删除用户 指定id
读写数据
influx write --bucket undefined_ecmaster -o undefined_ecmaster --url https://influx-testdata.s3.amazonaws.com/air-sensor-data-annotated.csv //往bucket中写入数据
influx query -o undefined_ecmaster 'from(bucket:"undefined_ecmaster") |> range(start:-1d,stop:-1ms)' //读取数据
curl方式调用:
查询org:
curl --request GET "ip:8086/api/v2/orgs" \
--header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw=="
新增 org:
curl --request POST "http://ip:8086/api/v2/orgs" \
--header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{"name": "personal_ecmaster1" }'
删除org:
curl --request DELETE "ip:8086/api/v2/orgs/f70722a047489640" \
--header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw=="
接口文档:https://docs.influxdata.com/influxdb/v2/api/#operation/DeleteAuthorizationsID