6.0 KiB
title | seotitle | list_title | description | menu | v2.0/tags | weight | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JavaScript client library | InfluxDB JavaScript client library | JavaScript | Use the JavaScript client library to interact with InfluxDB. |
|
|
201 |
Use the InfluxDB JavaScript client library to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB. If just getting started, see Get started with InfluxDB.
Before you begin
-
Install NodeJS.
-
Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see InfluxDB URLs.
Easiest way to get started
-
Clone the examples directory in the influxdb-client-js repo.
-
Navigate to
examples
directory and install dependencies.{{< code-tabs-wrapper >}} {{% code-tabs %}} npm yarn {{% /code-tabs %}} {{% code-tab-content %}}
Navigate into examples directory
cd examples
Install dependencies
npm install
{{% /code-tab-content %}} {{% code-tab-content %}}
sh
Navigate into examples directory
cd examples
Install dependencies
yarn install
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}
3. Update your ./env
and index.html
with the name of your InfluxDB bucket, organization, token, and proxy
which relies upon proxy to forward requests to the target InfluxDB.
4. Run the following command to run the application at http://localhost:3001/examples/index.html
```sh
npm run browser
```
Boilerplate for the InfluxDB Javascript Client Library
Use the Javascript library to write data to and query data from InfluxDB.
To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
Next, define constants for your InfluxDB bucket, organization, token, and proxy
which relies on a proxy to forward requests to the target InfluxDB instance.
const proxy = '/influx'
const token = 'example-token'
const org = 'example-org'
const bucket = 'example-bucket'
Instantiate the InfluxDB JavaScript Client and pass in theproxy
and token
parameters.
const InfluxDB = new InfluxDB({proxy, token})
Write data to InfluxDB with JavaScript
Use the Javascript library to write data to InfluxDB.
Use the getWriteApi
method of the InfluxDB client to create a write client. Provide your InfluxDB org
and bucket
.
const writeApi = InfluxDB.getWriteApi(org, bucket)
The useDefaultTags
method instructs the write api to use default tags when writing points. Create a point and write it to InfluxDB using the writePoint
method. The tag
and floatField
methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
writeApi.writePoint(point1)
console.log(`${point1}`)
writeApi.close()
Complete example write script
const writeApi = new InfluxDB({proxy, token})
const writeApi = influxDB.getWriteApi(org, bucket)
// setup default tags for all writes through this API
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
writeApi.writePoint(point1)
console.log(` ${point1}`)
// flush pending writes and close writeApi
writeApi
.close()
.then(() => {
console.log('WRITE FINISHED')
})
Query data from InfluxDB with JavaScript
Use the Javascript library to query data from InfluxDB.
Use the getQueryApi
method of the InfluxDB
client to create a new query client. Provide your InfluxDB org
.
const queryApi = influxDB.getQueryApi(org)
Create a Flux query (including your bucket
parameter).
const fluxQuery =
'from(bucket:"<my-bucket>")
|> range(start: 0)
|> filter(fn: (r) => r._measurement == "temperature")'
The query client sends the Flux query to InfluxDB and returns line table metadata and rows.
Use the next
method to iterate over the rows.
queryApi.queryRows(fluxQuery, {
next(row: string[], tableMeta: FluxTableMetaData) {
const o = tableMeta.toObject(row)
// console.log(JSON.stringify(o, null, 2))
console.log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
)
}
}
Complete example query script
// performs query and receive line table metadata and rows
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
queryApi.queryRows(fluxQuery, {
next(row: string[], tableMeta: FluxTableMetaData) {
const o = tableMeta.toObject(row)
// console.log(JSON.stringify(o, null, 2))
console.log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
)
},
error(error: Error) {
console.error(error)
console.log('\nFinished ERROR')
},
complete() {
console.log('\nFinished SUCCESS')
},
})
For more information, see the JavaScript client README on GitHub.