Documentation

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

使用 InfluxDB 3 客户端

/api/v2/query API 端点以及相关工具,例如 InfluxDB v2 客户端库和 influx CLI,无法 查询 InfluxDB Cloud Serverless 集群。

InfluxDB 3 客户端库Flight SQL 客户端 可用,能够与您的代码集成,以便写入和查询存储在 InfluxDB Cloud Serverless 中的数据。

InfluxDB 3 支持多种不同的工具用于 写入查询 数据。 比较您可以使用的工具 与 InfluxDB Cloud Serverless 进行交互。

使用InfluxDB v2 JavaScript 客户端库将数据从 Node.js 环境写入 InfluxDB。

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

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

开始之前

使用客户端库写入数据

  1. 通过调用 new InfluxDB() 构造函数并传入您的 InfluxDB URL 和 API 令牌(您已经在 安装部分 设置的环境变量)来实例化客户端。

    import {InfluxDB, Point} from '@influxdata/influxdb-client'
    
    const influxDB = new InfluxDB({url: process.env.INFLUX_URL,
                                   token: process.env.INFLUX_TOKEN})
    
  2. 使用客户端的 getWriteApi() 方法创建一个 写客户端。提供您的 InfluxDB 组织 ID 和桶名称。

const writeApi = influxDB.getWriteApi(process.env.INFLUX_ORG, process.env.INFLUX_BUCKET)


3. To apply one or more [tags](/influxdb3/cloud-serverless/reference/glossary/#tag) to all points, use the `useDefaultTags()` method.
Provide tags as an object of key/value pairs.

 ```js
 writeApi.useDefaultTags({region: 'west'})
 ```

4. Use the `Point()` constructor to create a [point](/influxdb3/cloud-serverless/reference/glossary/#point).
1. Call the constructor and provide a [measurement](/influxdb3/cloud-serverless/reference/glossary/#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`.

 ```js
 const point1 = new Point('temperature')
   .tag('sensor_id', 'TLM010')
   .floatField('value', 24.0)
 ```

5. Use the `writePoint()` method to write the point to your InfluxDB bucket.
Finally, use the `close()` method to flush all pending writes.
The example logs the new data point followed by "WRITE FINISHED" to stdout.

 ```js
 writeApi.writePoint(point1)

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

### Complete example

```js
'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'

/**
* Instantiate the InfluxDB client.
* Provide your InfluxDB URL and API token.
**/
const influxDB = new InfluxDB({url: process.env.INFLUX_URL,
                           token: process.env.INFLUX_TOKEN})

/**
* Create a write client from the getWriteApi method.
* Provide your org and bucket.
**/
const writeApi = influxDB.getWriteApi(process.env.INFLUX_ORG,
                                   process.env.INFLUX_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')
})

在您的终端中,使用 环境变量或 env.js 设置,运行以下命令以执行 JavaScript 文件:

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 企业版是建立在核心基础之上的商业版本,增加了历史查询能力、读取副本、高可用性、可扩展性和细粒度安全性。

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

InfluxDB 云端无服务器