Documentation

使用InfluxDB JavaScript客户端库写入数据

使用InfluxDB JavaScript client library将数据从Node.js环境写入InfluxDB。

JavaScript客户端库包括以下便捷功能用于向InfluxDB写入数据:

  • 将默认标签应用于数据点。
  • 将缓冲区点分成批次以优化数据传输。
  • 在失败时自动重试请求。
  • 为您的网络设置可选的HTTP代理地址。

在开始之前

使用客户端库写入数据

  1. 实例化一个 InfluxDB 客户端。提供您的 InfluxDB URL 和 API 令牌。

    import {InfluxDB, Point} from '@influxdata/influxdb-client'
    
    const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})
    

    替换以下内容:

    • YOUR_URL: InfluxDB网址
    • YOUR_API_TOKEN: InfluxDB API 令牌
  2. 使用客户端的 getWriteApi() 方法创建一个 写入客户端。提供您的 InfluxDB 组织 ID 和桶名称。

    const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)
    

    替换以下内容:

    • YOUR_ORG: InfluxDB 组织 ID
    • YOUR_BUCKET: InfluxDB 存储桶名称
  3. 要将一个或多个 tags 应用到所有点,请使用 useDefaultTags() 方法。提供的标签应为键/值对的对象。

    writeApi.useDefaultTags({region: 'west'})
    
  4. 使用 Point() 构造函数来创建一个

    1. Call the constructor and provide a measurement.
    2. To add one or more tags, chain the tag() method to the constructor. Provide a name and value.
    3. To add a field of type float, chain the floatField() method to the constructor. Provide a name and value.
    const point1 = new Point('temperature')
      .tag('sensor_id', 'TLM010')
      .floatField('value', 24)
    
  5. 使用writePoint()方法将点写入您的InfluxDB桶。最后,使用close()方法刷新所有待处理的写入。示例将新数据点以及“WRITE FINISHED”记录到stdout。

    writeApi.writePoint(point1)
    
    writeApi.close().then(() => {
      console.log('WRITE FINISHED')
    })
    

完整示例

curl --request POST \
"http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
  --header "Authorization: Token YOUR_API_TOKEN" \
  --header "Content-Type: text/plain; charset=utf-8" \
  --header "Accept: application/json" \
  --data-binary '
    airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
    airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
    '
'use strict'
/** @module write
 * Writes a data point to InfluxDB using the Javascript client library with Node.js.
**/

import { InfluxDB, Point } from '@influxdata/influxdb-client'

/** Environment variables **/
const url = process.env.INFLUX_URL
const token = process.env.INFLUX_TOKEN
const org = process.env.INFLUX_ORG
const bucket = process.env.INFLUX_BUCKET

/**
 * Instantiate the InfluxDB client
 * with a configuration object.
 **/
const influxDB = new InfluxDB({ url, token })

/**
 * Create a write client from the getWriteApi method.
 * Provide your `org` and `bucket`.
 **/
const writeApi = influxDB.getWriteApi(org, bucket)

/**
 * Apply default tags to all points.
 **/
writeApi.useDefaultTags({ region: 'west' })

/**
 * Create a point and write it to the buffer.
 **/
const point1 = new Point('temperature')
  .tag('sensor_id', 'TLM01')
  .floatField('value', 24.0)
console.log(` ${point1}`)

writeApi.writePoint(point1)

/**
 * Flush pending writes and close writeApi.
 **/
writeApi.close().then(() => {
  console.log('WRITE FINISHED')
})

要从文件运行示例,请设置您的InfluxDB环境变量,并使用 node 来执行JavaScript文件。

export INFLUX_URL=http://localhost:8086 && \
export INFLUX_TOKEN=YOUR_API_TOKEN && \
export INFLUX_ORG=YOUR_ORG && \
export INFLUX_BUCKET=YOUR_BUCKET && \
node write.js

响应代码

有关 InfluxDB API 响应代码 的信息,请参阅 InfluxDB API 写入文档



Flux的未来

Flux 正在进入维护模式。您可以像现在一样继续使用它,而无需对您的代码进行任何更改。

阅读更多

InfluxDB 3 开源版本现已公开Alpha测试

InfluxDB 3 Open Source is now available for alpha testing, licensed under MIT or Apache 2 licensing.

我们将发布两个产品作为测试版的一部分。

InfluxDB 3 核心,是我们新的开源产品。 它是一个用于时间序列和事件数据的实时数据引擎。 InfluxDB 3 企业版是建立在核心基础之上的商业版本,增加了历史查询能力、读取副本、高可用性、可扩展性和细粒度安全性。

有关如何开始的更多信息,请查看: