使用InfluxDB JavaScript客户端库写入数据
使用InfluxDB JavaScript client library将数据从Node.js环境写入InfluxDB。
JavaScript客户端库包括以下便捷功能用于向InfluxDB写入数据:
- 将默认标签应用于数据点。
- 将缓冲区点分成批次以优化数据传输。
- 在失败时自动重试请求。
- 为您的网络设置可选的HTTP代理地址。
在开始之前
使用客户端库写入数据
实例化一个
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 令牌
使用客户端的
getWriteApi()方法创建一个 写入客户端。提供您的 InfluxDB 组织 ID 和桶名称。const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)替换以下内容:
YOUR_ORG: InfluxDB 组织 IDYOUR_BUCKET: InfluxDB 存储桶名称
要将一个或多个 tags 应用到所有点,请使用
useDefaultTags()方法。提供的标签应为键/值对的对象。writeApi.useDefaultTags({region: 'west'})使用
Point()构造函数来创建一个 点。- Call the constructor and provide a measurement.
- To add one or more tags, chain the
tag()method to the constructor. Provide anameandvalue. - To add a field of type
float, chain thefloatField()method to the constructor. Provide anameandvalue.
const point1 = new Point('temperature') .tag('sensor_id', 'TLM010') .floatField('value', 24)使用
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 写入文档。