gpt4 book ai didi

c++ - 如何使用libcurl发布数据(包括换行符)(适用于Influxdb)?

转载 作者:行者123 更新时间:2023-12-02 10:38:48 25 4
gpt4 key购买 nike

我正在使用C++中的libcurl将数据点发布到InfluxDB。单个数据点有效,但对于多个点,我正在努力按照InfluxDB HTTP API的定义在必要的换行符(\ n)中:

Write several points to the database with one request by separating each point by a new line.

Write points from a file with the @ flag. The file should contain a batch of points in the Line Protocol format. Individual points must be on their own line and separated by newline characters (\n). Files containing carriage returns will cause parser errors.


我想这可能与char格式有关,但我不知道为什么吗?以下代码只写了5个距离,但应该写两个距离。
#include "stdafx.h"
#include "tchar.h"
#include <string>
#include <curl\curl.h>
#include <sstream>

int main()
{
int dataPoints[] = { 3, 5 };
std::string fieldIdentifier = "distance";

std::stringstream ss;

for (int i = 0; i < 1; i++) {
ss << "aufbau1,ort=halle ";
ss << fieldIdentifier;
ss << "=" << dataPoints[i];
ss << std::endl; //I guess this is the problem, it adds \n
}

ss << "aufbau1,ort=halle ";
ss << fieldIdentifier;
ss << "=" << dataPoints[1];

std::string data = ss.str();
const char *dataChar = data.c_str();

CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();

if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://myIP:8086/write?db=testdb&u=myUser&p=myPwd");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataChar);
res = curl_easy_perform(curl);

/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

curl_easy_cleanup(curl);
}
return 0;
}

最佳答案

在python中,在grafana中查看此数据时,在tag / field值内包含<br> html标签的行为似乎像换行符。

关于\n influxdb说

Note: Line protocol does not support the newline character \n in tag values or field values.



https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/

关于c++ - 如何使用libcurl发布数据(包括换行符)(适用于Influxdb)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com