安装C++ influxdb环境
📦仓库:GitHub - orca-zhang/influxdb-cpp: 💜 C++ client for InfluxDB.
直接将influxdb.hpp
放到工程src/
中,并引用该头文件即可,非常方便
连接并读取influxdb数据库
连接
influxdb_cpp::server_info insense("127.0.0.1", 8086, "test", "username", "password");
读取
string resp;
influxdb_cpp::query(resp, "select * from table_name limit 100", insense);
std::cout << resp << std::endl;
写入influxdb数据库
连接
influxdb_cpp::server_info insense("127.0.0.1", 8086, "test", "invix", "invix");
写入
influxdb_cpp::builder()
.meas("final_cost")
.field("cost", final_cost)
.post_http(insense);
一些概念
influxdb中 连接里的是database,写入的是measurement
CRUD API
Install Database
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.7.7_amd64.deb
sudo dpkg -i influxdb_1.7.7_amd64.deb
Config Database
influxd --启动数据库
CREATE DATABASE "testDB" --创建数据库
CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES --创建用户
show retention policies on "db_name" --查看当前数据库Retention Policies
alter retention policy "rp_name" on "db_name" duration 30d default --修改Retention Policies
drop retention policy "rp_name" on "db_name" --删除Retention Policies
Create database and use database
CREATE DATABASE "testDB" --创建数据库
show databases --展示所有数据库
use test 使用 --数据库
Query table
SHOW MEASUREMENTS --查询当前数据库表
SHOW FIELD KEY --查看数据表字段
SHOW series from key 查看key值
SHOW TAG KEYS from "KEY" --查看key中tag key值
Query records
SELECT * FROM /.*/ LIMIT 1 --查询当前数据库下所有表的第一行
SELECT * FROM xx ORDER BY xx DESC limit 2
DELETE FROM "table_name" (delete table and all its data)
DROP MEASUREMENT "table_name" (table is dropped but data is remain)
DELETE FROM cpu WHERE time < '2000-01-01T00:00:00Z'
DELETE WHERE time < '2000-01-01T00:00:00Z'
DROP DATABASE “testDB” --删除数据库
DROP RETENTION POLICY "dbbak" ON mydb --删除保留数据为dbbak数据
DROP SERIES from pay where tag_key='' --删除key中的tag