influxdb-cpp
influxdb-cpp copied to clipboard
how to insert batch data of array
x = [1,2,3,4,5] y = [1,2,3,4,5]
how to insert batch data once instead of insert five times?
- Bulk/batch/multiple insert also supports:
influxdb_cpp::builder()
.meas("foo") // series 1
.field("x", 10)
.meas("bar") // series 2
.field("y", 10.3)
.send_udp("127.0.0.1", 8091);
- Bulk/batch/multiple insert also supports:
influxdb_cpp::builder() .meas("foo") // series 1 .field("x", 10) .meas("bar") // series 2 .field("y", 10.3) .send_udp("127.0.0.1", 8091);
This answer does not answer my question which is the same as above. I have an array whose length is a variable other than constant. I want to write as follows e.g.
std::vector<double> values = {1,2,3,4,5};
auto builder = influxdb_cpp::builder();
for (double value: values) {
builder = builder.meas("foo").field("x", value);
}
builder.send_udp("127.0.0.1", 8091);
This is what I can not do right now.
x = [1,2,3,4,5] y = [1,2,3,4,5]
how to insert batch data once instead of insert five times?
I think I found a solution. Here is the code for demonstration.
auto influx_builder = influxdb_cpp::builder();
int point_idx = 0;
for (double value: values) {
if (0 == point_idx) {
influx_builder
.meas("means")
.tag("tag", "t")
.field("value", value)
.timestamp(time.time_since_epoch().count());
} else {
reinterpret_cast<influxdb_cpp::detail::ts_caller&>(influx_builder)
.meas("means")
.tag("tag", "t")
.field("value", value)
.timestamp(time.time_since_epoch().count());
}
point_idx ++;
}
reinterpret_cast<influxdb_cpp::detail::ts_caller&>(influx_builder)
.post_http(db_server_info_);
This trick worked for me.
#51