如何发布数据,包括使用libcurl(用于InfuxDB)的线路中断

How to post data including a line break with libcurl (for Influxdb)?

本文关键字:InfuxDB 用于 中断 线路 libcurl 数据 何发布 包括使      更新时间:2023-10-16

我在C 中使用libcurl向influxdb发布数据。单个数据点正在起作用,但是对于多个点,我在influxdb http api中定义的必要线路破解( n(苦苦挣扎:

通过通过新行分开每个点,用一个请求写几个点。

用 @ flag从文件中写入点。该文件应以行协议格式包含一批点。各个点必须在自己的线上,并由新线字符( n(隔开。包含运输退货的文件将导致解析器错误。

我想这可能是炭格式的问题,但是我不知道为什么?以下代码仅在距离上写入5个,但应该写两个距离。

#include "stdafx.h"
#include "tchar.h"
#include <string>
#include <curlcurl.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: %sn",
                curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    return 0;
}

在python中具有 <br> html标记,在标签/字段值中似乎像是一个线破裂,同时在格拉法纳(Grafana(查看此数据。

关于 n infuxdb说

注意:行协议不支持标签值或字段值中的newline字符 n。

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