orca-zhang / influxdb-cpp

💜 C++ client for InfluxDB.
MIT License
163 stars 83 forks source link

support bulk/batch/multiple insert in loop #28

Closed alwqx closed 4 years ago

alwqx commented 4 years ago

Hi, @orca-zhang thanks for you awesome working.

I want to insert 1000 series per second. Your README says:

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

It's not good when I have 1000 series which means I will write code likes:

influxdb_cpp::builder()
    .meas("foo")  // series 1
    .field("x", 1)
    .meas("foo")  // series 2
    .field("x", 2)
    ....
    meas("foo")  // series 2
    .field("x", 1000)
    .send_udp("127.0.0.1", 8091);

I wonder if you plan to add multiple insert in loop likes:

int test(influxdb_cpp::server_info si) {
    int i;
    auto build = influxdb_cpp::builder();
    for(i=0; i<1000; i++) {
        build.meas("stu") // detail::tag_caller
            .tag("region", "test3")
            .field("value", i+1);
    }

    string resp;
    build.post_http(si, &resp);
    cout<< "resp is:" << resp << endl;

    return 0;
}

I try to add this feature by add \n to lines_ but failed. My code is:

struct builder {
        detail::tag_caller& meas(const std::string& m) {
            lines_.imbue(std::locale("C"));
            lines_.clear();
            return _m(m);
        }

        int post_http(const server_info& si, std::string* resp) {
            return detail::inner::http_request("POST", "write", "", lines_.str(), si, resp);
        }

        void format() {
            lines_ << "\n";
        }
...
}

int test(influxdb_cpp::server_info si) {
    int i;
    auto build = influxdb_cpp::builder();
    for(i=0; i<1000; i++) {
        build.meas("stu") // detail::tag_caller
            .tag("region", "test3")
            .field("value", i+1);
        if(i!=999) build.format();
    }

    string resp;
    build.post_http(si, &resp);
    cout<< "resp is:" << resp << endl;

    return 0;
}
sherimao commented 4 years ago
influxdb_cpp::server_info si(host, port, db, "","");
string resp;
influxdb_cpp::builder builder;
influxdb_cpp::detail::ts_caller *caller = nullptr;
for (int i = 0; i < n ;i++) {
    if (caller) {
        caller = &caller->meas("m")
                .tag("t", a[i])
                .field("f", b[i]);
    } else {
        caller = &builder.meas("m")
                .tag("t", a[i])
                .field("f", b[i]);
    }
}
if (caller) {
    int result = caller->post_http(si, &resp);
}