influxdb-cpp icon indicating copy to clipboard operation
influxdb-cpp copied to clipboard

how to insert batch data of array

Open sunnyinchina opened this issue 1 year ago • 4 comments

x = [1,2,3,4,5] y = [1,2,3,4,5]

how to insert batch data once instead of insert five times?

sunnyinchina avatar Mar 15 '24 12:03 sunnyinchina

  • 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);

orca-zhang avatar May 31 '24 07:05 orca-zhang

  • 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.

Binary-Vanguard-12138 avatar Sep 29 '24 17:09 Binary-Vanguard-12138

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.

Binary-Vanguard-12138 avatar Sep 29 '24 18:09 Binary-Vanguard-12138

#51

orca-zhang avatar Dec 07 '24 15:12 orca-zhang