1

我刚开始尝试使用 influxDB 和 influxDB 节点模块。

我有以下代码,每秒插入一些随机数据。我没有收到任何错误,但没有数据添加到我的时间序列中。

代码是:

var influxdb = require('influxdb');
var sleep = require('sleep');
var connection = influxdb('172.21.5.67', 8086);
connection.auth({ name: 'root', password: 'root' });

var db;
var ISCSIDataSeries;

function random (low, high) {
    return Math.floor(Math.random() * (high - low) + low);
}

function doInsert(i) {
    if (db == undefined) {
        db = connection.database('test');
        console.log('established the db connection');
    }

    if (ISCSIDataSeries == undefined) {
        ISCSIDataSeries = db.series('SCSIData');
        console.log('the series SCSIData is established');
    }

    var reads = random(1000, 10000);
    var writes = random(2000, 20000);
    var IOS = random(100000, 1000000);

    ISCSIDataSeries.writePoints({
        'columns': ['Volume', 'Reads', 'Writes'],
        'points':  [reads, writes, IOS]
    });

    db.save();
}

var i = 0;
while (i < 10) {
    sleep.sleep(1);
    doInsert(i);
    i ++;
}

console.log('so long folks');

在运行结束时,我没有看到任何数据输入。有这个包的经验吗?

4

1 回答 1

7

我是 InfluxDB 的维护者之一。我们不使用节点,所以我不熟悉这个库,但我尝试了你的代码片段,确实它不起作用。事实证明,该influxdb库不是最新的,最后一次更新是在四个月前,在此期间 InfluxDB api 经历了重大变化。我建议你改用influx 包,它似乎得到了更积极的维护。我修改了您的代码片段以与其他包一起使用,并且它成功运行:

var influxdb = require('influx');
var sleep = require('sleep');

var root = new influxdb.InfluxDB('localhost', 8086, 'root', 'root');
root.createDatabase('SCSIData', function(err) {
  if (err && err.message.indexOf("exist") == -1) {
    console.log("Cannot create db", err);
    process.exit(1);
  };

  var client = new influxdb.InfluxDB('localhost', 8086, 'root', 'root', 'SCSIData');

  function random (low, high) { return Math.floor(Math.random() * (high - low) + low); }

  function doInsert(i) {
    var reads = random(1000, 10000);
    var writes = random(2000, 20000);
    var IOS = random(100000, 1000000);


    client.writePoint("series.name", {
      'Volume': IOS,
      'Reads': reads,
      'Writes': writes
    }, function(err) {
      if (err) {
        console.log("Cannot write data", err);
        process.exit(1);
      }
    });
  }

  var i = 0;
  while (i < 10) {
    doInsert(i);
    i++;
  }

  client.query("select count(Reads) from series.name", function(err, result) {
    if (err) {
      console.log("Cannot write data", err);
    }

    console.log("result", result)
    console.log("Number of points: ", result[0].points[0][1]);
  })

});

console.log('so long folks');
于 2014-05-19T21:08:22.623 回答