Query data with the InfluxDB JavaScript client library
The following example sends a Flux query to an InfluxDB bucket and outputs rows from an observable table.
Query InfluxDB
Change to your new project directory and create a file for your query module.
-
import { InfluxDB, Point } from '@influxdata/influxdb-client'
Replace the following:
YOUR_URL
: InfluxDB URLYOUR_API_TOKEN
: InfluxDB API tokenYOUR_ORG
: InfluxDB organization ID
Create a Flux query for your InfluxDB bucket. Store the query as a string variable.
Replace
YOUR_BUCKET
with the name of your InfluxDB bucket.Use the method of the query client to query InfluxDB.
queryRows()
takes a Flux query and an RxJS Observer object. The client returns metadata and rows as an RxJS Observable.queryRows()
subscribes your observer to the observable. Finally, the observer logs the rows from the response to the terminal.const observer = {
next(row, tableMeta) {
const o = tableMeta.toObject(row)
`${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
)
}
queryApi.queryRows(fluxQuery, observer)
export INFLUX_URL=http://localhost:8086 && \
export INFLUX_TOKEN=YOUR_API_TOKEN && \
node query.js
For more examples and information, see the .