Search code examples
node.jswindowsinfluxdbcarriage-return

Insert data into influxdb from node / windows causes problems


I'm trying to insert data into InfluxDb using node/windows but there is a (seemingly well-know) bug that I am having trouble wrapping my mind around. I am trying to do a batch insert into my local db by loading data from a file and sending to my database using a POST. Here is the data that I am trying to insert:

test_data.txt

test_measurement test=val1 1516665684
test_measurement price=val2 1516665685
test_measurement price=val3 1516665686

Using the code:

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

And I get the error

{"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"}

From my research, I understand that windows uses \r\n characters as a carriage return, but I'm having trouble wrapping my mind around how to fix this. I've made attempts to remove all \r and \n characters when I read the file in, but it doesn't seem to remove them, as I continue to get the same error. Even if I was able to remove them this way, how would influx know where the next insert of the batch began? It needs to know where the next line starts for a batch insert, but doesn't like the new line characters.

Any help would be great!!


Solution

  • Double quote string field values. Do not double quote floats, integers, or booleans.

    UPDATE (after the actual response appears there in description)

    Then it apparently \r.

    Your file were created by Windows editor, I presume - that's it got both symbols at the end.

    So you just turn it into Unix format, there are pleny of tools for it, perhaps, Notepad++ is most popular.

    Or, better off, just add some humble string pre-processing into your code, it's supereasy with JS:

    req.write(data.replace("\r",""));