如何使用NodeJS将一组测量结果写入InfluxDB?

时间:2019-04-05 16:40:10

标签: node.js influxdb

我正在尝试将一组CPU温度注入InfluxDB实例中。以下代码将只注入集合中的最后一个样本(cpu = 5)。如何将一系列点注入测量中?

const Influx = require("influx");
const os = require("os");

const influx = new Influx.InfluxDB({
    host: "localhost",
    database: "example",
    schema: [
        {
            measurement: 'cpu-temp',
            fields: { temp: Influx.FieldType.FLOAT, cpu: Influx.FieldType.INTEGER, socket: Influx.FieldType.INTEGER },
            tags: [ 'host' ]
        }
    ]
});


function writeTemps() {
    //In real life this would use something `sensors` package to obtain the data and transformed into this structure
    const data = [];
    for (let i = 0; i < 6; i++) {
        data.push({
            measurement: "cpu-temp",
            tags: {
                host: os.hostname()
            },
            fields: {
                cpu: i,
                temp: Math.round(((Math.random() * 24) + 24 * 10)) / 10,
                socket: 1
            }
        });
    }
    influx.writePoints(data).then(() => console.log("worked"), (e) => console.error(e));
}

writeTemps();

测量中的示例数据:

> select * from "cpu-temp"
name: cpu-temp
time                cpu host          socket temp
----                --- ----          ------ ----
1554481960188163700 5   Deckard.local 1      26.2
1554481961157513900 5   Deckard.local 1      24.3
1554481962159479300 5   Deckard.local 1      24.5
1554481963161301300 5   Deckard.local 1      24.9
1554481964166741100 5   Deckard.local 1      24.7
1554481965168176800 5   Deckard.local 1      26.2
1554481966168756700 5   Deckard.local 1      24.9
1554481967140210800 5   Deckard.local 1      25.6
1554481968145122000 5   Deckard.local 1      25.9
1554481969144965800 5   Deckard.local 1      25.9
1554481970150685100 5   Deckard.local 1      24.8
1554481971155935600 5   Deckard.local 1      25.7
1554481972160289200 5   Deckard.local 1      24.2
1554481973167241600 5   Deckard.local 1      26.3
1554481974172369600 5   Deckard.local 1      24.2
1554481975176451200 5   Deckard.local 1      25.1
1554481976179103100 5   Deckard.local 1      24.6
1554481977183923100 5   Deckard.local 1      26.2
1554481978189576000 5   Deckard.local 1      25.9
1554481979193856300 5   Deckard.local 1      24.2
1554481980199759900 5   Deckard.local 1      26.3
1554481981205830500 5   Deckard.local 1      24.4
>

1 个答案:

答案 0 :(得分:1)

您的代码将插入所有记录。但是,您不会插入唯一的点,因此您的点在InfluxDB级别上已“去重复”。如何定义唯一点?

https://docs.influxdata.com/influxdb/v1.7/troubleshooting/frequently-asked-questions/#how-does-influxdb-handle-duplicate-points

  

点由测量名称,标签集和时间戳唯一标识。

因此,更改测量模式,并将cpu插入为tag,而不是field,您将插入唯一的点=>您将在InfluxDB中看到所有记录。 / p>