From 5669ab17a36663bd50cddd9937ed2b494e2d0bfc Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 14 Jun 2021 10:27:23 -0500 Subject: [PATCH] update: divided JS client library page into separate. * reorganized api shortcodes --- .../cloud/write-data/developer-tools/api.md | 2 +- .../api-guide/client-libraries/js/_index.md | 159 +----------------- .../api-guide/client-libraries/js/install.md | 56 ++++++ .../api-guide/client-libraries/js/query.md | 72 ++++++++ .../api-guide/client-libraries/js/write.md | 66 ++++++++ .../shortcodes/api/{ => cloud}/curl/auth.sh | 0 layouts/shortcodes/api/v2dot0/curl/auth.sh | 4 + .../api/v2dot0/nodejs/learn-more.md | 2 + .../api/{ => v2dot0}/nodejs/write.mjs | 0 9 files changed, 204 insertions(+), 157 deletions(-) create mode 100644 content/influxdb/v2.0/api-guide/client-libraries/js/install.md create mode 100644 content/influxdb/v2.0/api-guide/client-libraries/js/query.md create mode 100644 content/influxdb/v2.0/api-guide/client-libraries/js/write.md rename layouts/shortcodes/api/{ => cloud}/curl/auth.sh (100%) create mode 100644 layouts/shortcodes/api/v2dot0/curl/auth.sh create mode 100644 layouts/shortcodes/api/v2dot0/nodejs/learn-more.md rename layouts/shortcodes/api/{ => v2dot0}/nodejs/write.mjs (100%) diff --git a/content/influxdb/cloud/write-data/developer-tools/api.md b/content/influxdb/cloud/write-data/developer-tools/api.md index a6257e97d..58245b320 100644 --- a/content/influxdb/cloud/write-data/developer-tools/api.md +++ b/content/influxdb/cloud/write-data/developer-tools/api.md @@ -44,7 +44,7 @@ mem,host=host2 used_percent=27.18294630 1556896336 {{% /code-tab-content %}} {{% code-tab-content %}} ```js -{{< api/nodejs/write >}} +{{< api/v2dot0/nodejs/write >}} ``` {{% /code-tab-content %}} {{< /code-tabs-wrapper >}} diff --git a/content/influxdb/v2.0/api-guide/client-libraries/js/_index.md b/content/influxdb/v2.0/api-guide/client-libraries/js/_index.md index 1e21f051d..b49a433f9 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/js/_index.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/js/_index.md @@ -16,161 +16,8 @@ aliases: Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into your Node.js application. -In this guide, you'll start a Node.js project from scratch and code some simple API operations. See the [client repo](https://github.com/influxdata/influxdb-client-js) for more [complete examples](https://github.com/influxdata/influxdb-client-js/tree/master/examples). +In this guide, you'll start a Node.js project from scratch and code some simple API operations. -## Install +{{< children >}} -1. Install [Node.js](https://nodejs.org/en/download/package-manager/). - -2. 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](/influxdb/v2.0/reference/urls/). - -3. Start a new Node.js project: - - ```sh - npm init influx-node-app - ``` - - Answer or `Enter` through the prompts. - -## Install dependencies -The JavaScript client contains two packages. Add both as dependencies of your project. - -{{% note %}} -`npm` package manager is included with Node.js. -{{% /note %}} - -1. Change to your project directory: - - ```sh - cd influx-node-app - ``` - -2. Install `@influxdata/influxdb-client` for querying and writing data: - - ```sh - npm install --save @influxdata/influxdb-client - ``` - -3. Install `@influxdata/influxdb-client-apis` for access to the InfluxDB management APIs: - - ```sh - npm install --save @influxdata/influxdb-client-apis - ``` - - - -## Configure your environment -1. Configure environment variables. Update your `./env` with the name of your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and [url](/influxdb/v2.0/urls). - -## Write data to InfluxDB with JavaScript -Use the Javascript library to write data to InfluxDB in a Node.js environment. - -{{% note %}} -This library supports browser and server-side (Node.js) Javascript environments. Use `@influxdata/influxdb-client` in your Node.js project. -{{% /note %}} - -1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`. -2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`. - - ```js - import {InfluxDB, Point} from '@influxdata/influxdb-client' - - const influxDB = new InfluxDB({url, token}) - const writeApi = influxDB.getWriteApi(org, bucket) - ``` - - The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/influxdb/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 - writeApi.useDefaultTags({location: 'browser'}) - const point1 = new Point('temperature') - .tag('example', 'index.html') - .floatField('value', 24) - console.log(`${point1}`) - - writeApi.writePoint(point1) - writeApi.close() - ``` - -### Complete example write script - -```js -const influxDB = 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) -console.log(` ${point1}`) - -writeApi.writePoint(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. - -1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`. - - ```js - const queryApi = influxDB.getQueryApi(org) - ``` - -2. Create a Flux query (including your `bucket` parameter). - - ```js - const fluxQuery = - 'from(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. - -3. Use the `next` method to iterate over the rows. - - ```js - 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 - -```js -// 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](https://github.com/influxdata/influxdb-client-js). +{{% api/v2dot0/nodejs/learn-more %}} diff --git a/content/influxdb/v2.0/api-guide/client-libraries/js/install.md b/content/influxdb/v2.0/api-guide/client-libraries/js/install.md new file mode 100644 index 000000000..9c4396f22 --- /dev/null +++ b/content/influxdb/v2.0/api-guide/client-libraries/js/install.md @@ -0,0 +1,56 @@ +--- +title: Install the JavaScript client library +description: > + Install the Node.js JavaScript client library to interact with the InfluxDB API. +menu: + influxdb_2_0: + name: Install + parent: Node.js +influxdb/v2.0/tags: [client libraries, JavaScript] +weight: 201 +aliases: + - /influxdb/v2.0/reference/api/client-libraries/js/install +--- + +## Install + +1. Install [Node.js](https://nodejs.org/en/download/package-manager/). + +{{% note %}} +`npm` package manager is included with Node.js. +{{% /note %}} + +2. 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](/influxdb/v2.0/reference/urls/). + +3. Start a new Node.js project. + + ```sh + npm init influx-node-app + ``` + + Answer or `Enter` through the prompts. + +## Install dependencies +The JavaScript client contains two packages. Add both as dependencies of your project. + +1. Change to your project directory: + + ```sh + cd influx-node-app + ``` + +2. Install `@influxdata/influxdb-client` for querying and writing data: + + ```sh + npm install --save @influxdata/influxdb-client + ``` + +3. Install `@influxdata/influxdb-client-apis` for access to the InfluxDB management APIs: + + ```sh + npm install --save @influxdata/influxdb-client-apis + ``` + +## Configure your environment +Configure environment variables. Update your `./env` file with the name of your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and [url](/influxdb/v2.0/urls). diff --git a/content/influxdb/v2.0/api-guide/client-libraries/js/query.md b/content/influxdb/v2.0/api-guide/client-libraries/js/query.md new file mode 100644 index 000000000..07ed01068 --- /dev/null +++ b/content/influxdb/v2.0/api-guide/client-libraries/js/query.md @@ -0,0 +1,72 @@ +--- +title: Query data with the JavaScript client library +description: > + Use the Node.js JavaScript client library to query data with the InfluxDB API. +menu: + influxdb_2_0: + name: Query + parent: Node.js +influxdb/v2.0/tags: [client libraries, JavaScript] +weight: 201 +aliases: + - /influxdb/v2.0/reference/api/client-libraries/js/nodejs/query +--- + +## Query data from InfluxDB with JavaScript +Use the Javascript library to query data from InfluxDB. + +1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`. + + ```js + const queryApi = influxDB.getQueryApi(org) + ``` + +2. Create a Flux query (including your `bucket` parameter). + + ```js + const fluxQuery = + 'from(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. + +3. Use the `next` method to iterate over the rows. + + ```js + 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 + +```js +// 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') + }, +}) +``` + +{{% api/v2dot0/nodejs/learn-more %}} diff --git a/content/influxdb/v2.0/api-guide/client-libraries/js/write.md b/content/influxdb/v2.0/api-guide/client-libraries/js/write.md new file mode 100644 index 000000000..1396ffb16 --- /dev/null +++ b/content/influxdb/v2.0/api-guide/client-libraries/js/write.md @@ -0,0 +1,66 @@ +--- +title: Write data with the JavaScript client library +description: > + Use the Node.js JavaScript client library to write data with the InfluxDB API. +menu: + influxdb_2_0: + name: Write + parent: Node.js +influxdb/v2.0/tags: [client libraries, JavaScript] +weight: 201 +aliases: + - /influxdb/v2.0/reference/api/client-libraries/js/nodejs/write +--- + +## Write data to InfluxDB with JavaScript +Use the Javascript library to write data to InfluxDB in a Node.js environment. + +{{% note %}} +This library supports browser and server-side (Node.js) Javascript environments. Use `@influxdata/influxdb-client` in your Node.js project. +{{% /note %}} + +1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`. +2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`. + + ```js + import {InfluxDB, Point} from '@influxdata/influxdb-client' + + const influxDB = new InfluxDB({url, token}) + const writeApi = influxDB.getWriteApi(org, bucket) + ``` + + The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/influxdb/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 + writeApi.useDefaultTags({location: 'browser'}) + const point1 = new Point('temperature') + .tag('example', 'index.html') + .floatField('value', 24) + console.log(`${point1}`) + + writeApi.writePoint(point1) + writeApi.close() + ``` + +### Complete example write script + +```js +const influxDB = 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) +console.log(` ${point1}`) + +writeApi.writePoint(point1) + +// flush pending writes and close writeApi +writeApi + .close() + .then(() => { + console.log('WRITE FINISHED') + }) +``` diff --git a/layouts/shortcodes/api/curl/auth.sh b/layouts/shortcodes/api/cloud/curl/auth.sh similarity index 100% rename from layouts/shortcodes/api/curl/auth.sh rename to layouts/shortcodes/api/cloud/curl/auth.sh diff --git a/layouts/shortcodes/api/v2dot0/curl/auth.sh b/layouts/shortcodes/api/v2dot0/curl/auth.sh new file mode 100644 index 000000000..cc94d9461 --- /dev/null +++ b/layouts/shortcodes/api/v2dot0/curl/auth.sh @@ -0,0 +1,4 @@ +curl --request POST http://localhost:8086/api/v2/api/v2/write \ + --header "Authorization: Token YOURAUTHTOKEN" \ + --data-urlencode "org=myorg" \ + --data-urlencode "bucket=example-bucket" diff --git a/layouts/shortcodes/api/v2dot0/nodejs/learn-more.md b/layouts/shortcodes/api/v2dot0/nodejs/learn-more.md new file mode 100644 index 000000000..9faef8c61 --- /dev/null +++ b/layouts/shortcodes/api/v2dot0/nodejs/learn-more.md @@ -0,0 +1,2 @@ + +For more examples and information, see the [JavaScript client on GitHub](https://github.com/influxdata/influxdb-client-js). diff --git a/layouts/shortcodes/api/nodejs/write.mjs b/layouts/shortcodes/api/v2dot0/nodejs/write.mjs similarity index 100% rename from layouts/shortcodes/api/nodejs/write.mjs rename to layouts/shortcodes/api/v2dot0/nodejs/write.mjs