【发布时间】:2018-02-01 07:19:57
【问题描述】:
我正在尝试使用 node/windows 将数据插入 InfluxDb,但是有一个(看似众所周知的)错误,我无法解决问题。我正在尝试通过从文件加载数据并使用 POST 发送到我的数据库来批量插入本地数据库。这是我要插入的数据:
test_data.txt
test_measurement test=val1 1516665684
test_measurement price=val2 1516665685
test_measurement price=val3 1516665686
使用代码:
var http = require("http");
var fs = require('fs');
fs.readFile('test_data.txt', { encoding: 'utf8' }, function (err, data) {
var options = {
"method": "POST",
"hostname": "localhost",
"port": "8086",
"path": "/write?db=test_db",
"headers": {
"cache-control": "no-cache"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(data);
req.end();
});
我得到了错误
{"error":"unable to parse 'test_measurement test=val1 1516665684\r': bad timestamp\nunable to parse 'test_measurement price=val2 1516665685\r': bad timestamp\nunable to parse 'test_measurement price=val3 1516665686': bad timestamp"}
根据我的研究,我了解到 Windows 使用 \r\n 字符作为回车符,但我无法思考如何解决这个问题。当我读入文件时,我尝试删除所有 \r 和 \n 字符,但它似乎没有删除它们,因为我继续收到相同的错误。即使我能够以这种方式移除它们,influx 怎么会知道下一批插入的开始位置?它需要知道批量插入的下一行从哪里开始,但不喜欢换行符。
任何帮助都会很棒!
【问题讨论】:
标签: node.js windows influxdb carriage-return