Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to insert batch data of array #47

Open
sunnyinchina opened this issue Mar 15, 2024 · 3 comments
Open

how to insert batch data of array #47

sunnyinchina opened this issue Mar 15, 2024 · 3 comments

Comments

@sunnyinchina
Copy link

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

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

@orca-zhang
Copy link
Owner

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

@Binary-Vanguard-12138
Copy link

  • 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
Copy link

Binary-Vanguard-12138 commented Sep 29, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants