写一个influxdb C++ 的客户端,influxDB 网站上推荐了influxdb.cxx https://github.com/awegrzyn/influxdb-cxx.
下载下来运行了一个 benchmark 的代码
#include <cstdio>
#include <cstdlib>
#include <string>
#include <InfluxDBFactory.h>
#include <boost/program_options.hpp>
#include <iostream>
using namespace influxdb;
int main(int argc, char* argv[]) {
int count = 1;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("count", boost::program_options::value<int>(), "128")
("url", boost::program_options::value<std::string>()->required(), "http://192.168.31.108:8086")
("buffer", boost::program_options::value<int>(), "Buffer size");
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
auto db = InfluxDBFactory::Get(vm["url"].as<std::string>());
if (vm.count("count")) {
count = std::floor(vm["count"].as<int>()/2) + 1;
}
if (vm.count("buffer")) {
db->batchOf(vm["buffer"].as<int>());
}
for(int i = 0; i <= count; i++) {
db->write(Point{"int"}.addField("value1", 10).addField("value2", "11").addTag("tag1", "machine"));
db->write(Point{"double"}.addField("value1", 10.10).addField("value2", "11.11").addTag("tag1", "machine"));
sleep(1);
}
std::cout << "Written " << 2*count-1 << " points";
if (vm.count("buffer")) { std::cout << " through " << vm["buffer"].as<int>(); }
std::cout << " to " << vm["url"].as<std::string>() << std::endl;
}
编译方式
clang++ benchmark.cxx -L ./lib -I ./include -o benchmark -lboost_system -lboost_program_options -linfluxDB -std=c++17
注意:
使用了boost 库,并且 -std=c++17
运行的方式
./benchmark --url http://192.168.31.108:8086/write?db=mydb3 --count 1200
其中 count=1200 表示连续发送1200个数据到influxdb 数据库
也可以用这个程序建立数据库
./benchmark --url http://192.168.31.108:8086/query?q=CREATE+DATABASE+mydb4
上述程序在windows subsystem for linux 上运行,访问另一台ubuntu linux(192.168.31.108)上的influxdb 数据库。
动态写入数据
上面的程序在实际应用中有点不方便,
db->write(Point{"int"}.addField("value1", 10).addField("value2", "11").addTag("tag1", "machine"));
语句中的格式是固定的
可以改写成:
influxdb::Point mPoint(pname);
mPoint.addField(...)
mPoint.addTag(....)
db->write(std::move(mPoint));
其中 std::move 将右值转换成左值,大概就是将一个对象的数据直接注入另一个对象。看的有点稀里糊涂。具体看左值。右值。和std::move的解释吧!在这里很重要