add numbering and alignment

pull/945/head
kelseiv 2020-08-17 22:31:51 -07:00 committed by GitHub
parent 7a3bfee9ff
commit e4612cd6c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 52 deletions

View File

@ -28,8 +28,8 @@ If just getting started, see [Get started with InfluxDB](/v2.0/get-started/).
1. Clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo. 1. Clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
2. Navigate to the `examples` directory and install dependencies. 2. Navigate to the `examples` directory and install dependencies.
{{< code-tabs-wrapper >}} {{< code-tabs-wrapper >}}
{{% code-tabs %}} {{% code-tabs %}}
[npm](#) [npm](#)
[yarn](#) [yarn](#)
{{% /code-tabs %}} {{% /code-tabs %}}
@ -62,48 +62,47 @@ yarn install
## Boilerplate for the InfluxDB Javascript client library ## Boilerplate for the InfluxDB Javascript client library
Use the Javascript library to write data to and query data from InfluxDB. 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. 1. To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
```js ```js
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs' import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
``` ```
Next, define constants for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `proxy` which relies on a proxy to forward requests to the target InfluxDB instance. 2. Define constants for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `proxy` which relies on a proxy to forward requests to the target InfluxDB instance.
```js
const proxy = '/influx'
const token = 'example-token'
const org = 'example-org'
const bucket = 'example-bucket'
```
```js 3. Instantiate the InfluxDB JavaScript client and pass in the `proxy` and `token` parameters.
const proxy = '/influx'
const token = 'example-token'
const org = 'example-org'
const bucket = 'example-bucket'
```
Instantiate the InfluxDB JavaScript client and pass in the `proxy` and `token` parameters. ```js
const InfluxDB = new InfluxDB({proxy, token})
```js ```
const InfluxDB = new InfluxDB({proxy, token})
```
## Write data to InfluxDB with JavaScript ## Write data to InfluxDB with JavaScript
Use the Javascript library to write data to InfluxDB. 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`. 1. Use the `getWriteApi` method of the InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`.
```js ```js
const writeApi = InfluxDB.getWriteApi(org, bucket) const writeApi = InfluxDB.getWriteApi(org, bucket)
``` ```
The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/v2.0/reference/glossary/#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. The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/v2.0/reference/glossary/#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.
```js ```js
writeApi.useDefaultTags({location: 'browser'}) writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature') const point1 = new Point('temperature')
.tag('example', 'index.html') .tag('example', 'index.html')
.floatField('value', 24) .floatField('value', 24)
writeApi.writePoint(point1) writeApi.writePoint(point1)
console.log(`${point1}`) console.log(`${point1}`)
writeApi.close() writeApi.close()
``` ```
### Complete example write script ### Complete example write script
@ -128,35 +127,36 @@ writeApi
## Query data from InfluxDB with JavaScript ## Query data from InfluxDB with JavaScript
Use the Javascript library to query data from InfluxDB. 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`. 1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`.
```js ```js
const queryApi = influxDB.getQueryApi(org) const queryApi = influxDB.getQueryApi(org)
``` ```
Create a Flux query (including your `bucket` parameter). 2. Create a Flux query (including your `bucket` parameter).
```js ```js
const fluxQuery = const fluxQuery =
'from(bucket:"<my-bucket>") 'from(bucket:"<my-bucket>")
|> range(start: 0) |> range(start: 0)
|> filter(fn: (r) => r._measurement == "temperature")' |> filter(fn: (r) => r._measurement == "temperature")'
``` ```
The **query client** sends the Flux query to InfluxDB and returns line table metadata and rows. 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.
```js 3. Use the `next` method to iterate over the rows.
queryApi.queryRows(fluxQuery, {
next(row: string[], tableMeta: FluxTableMetaData) { ```js
const o = tableMeta.toObject(row) queryApi.queryRows(fluxQuery, {
// console.log(JSON.stringify(o, null, 2)) next(row: string[], tableMeta: FluxTableMetaData) {
console.log( const o = tableMeta.toObject(row)
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}` // 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 ### Complete example query script