API examples and client guide updates (#3106)

* feat: troubleshoot write errors (#3003). Fix link in Flux reference.

* fix: make placeholders consistent and conform to style guide.

* fix: Node.js client. Delete unused files (#3003)

* Node.js client. Add examples.

* fix: gzip instructions for API with curl.

* fix: Node.js client guide. Query guide fixes. Frontmatter fixes.

* fix: API client frontmatter consistency. Use language for title so <children> list is sorted properly.

* Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* fix: customize example URLs (closes #3113)

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>
pull/2516/head
Jason Stirnaman 2021-09-13 11:08:02 -05:00 committed by GitHub
parent a703908ea3
commit 22cf8ccadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 382 additions and 217 deletions

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Arduino client library title: Arduino client library
seotitle: Use the InfluxDB Arduino client library
list_title: Arduino list_title: Arduino
description: Use the Arduino client library to interact with InfluxDB. description: Use the InfluxDB Arduino client library to interact with InfluxDB.
external_url: https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino external_url: https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino
list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_ list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_
menu: menu:

View File

@ -1,9 +1,9 @@
--- ---
title: Get started with the JavaScript client library for web browsers title: JavaScript client library for web browsers
seotitle: Get started with the InfluxDB JavaScript client library for web browsers seotitle: Use the InfluxDB JavaScript client library for web browsers
list_title: JavaScript for browsers list_title: JavaScript for browsers
description: > description: >
Use the JavaScript client library example app to interact with the InfluxDB API in web browsers. Use the InfluxDB JavaScript client library to interact with InfluxDB in web clients.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: JavaScript for browsers name: JavaScript for browsers

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB C# client library title: C# client library
list_title: C# list_title: C#
description: Use the C# client library to interact with InfluxDB. seotitle: Use the InfluxDB C# client library
description: Use the InfluxDB C# client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-csharp external_url: https://github.com/influxdata/influxdb-client-csharp
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,9 +1,9 @@
--- ---
title: Go client library title: Go client library
seotitle: InfluxDB Go client library seotitle: Use the InfluxDB Go client library
list_title: Go list_title: Go
description: > description: >
Use the Go client library to interact with InfluxDB. Use the InfluxDB Go client library to interact with InfluxDB.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Go name: Go

View File

@ -1,5 +1,6 @@
--- ---
title: InfluxDB Java client library title: Java client library
seotitle: Use the InfluxDB Java client library
list_title: Java list_title: Java
description: Use the Java client library to interact with InfluxDB. description: Use the Java client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java external_url: https://github.com/influxdata/influxdb-client-java

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Kotlin client library title: Kotlin client library
seotitle: Use the Kotlin client library
list_title: Kotlin list_title: Kotlin
description: Use the Kotlin client library to interact with InfluxDB. description: Use the InfluxDB Kotlin client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,9 +1,9 @@
--- ---
title: Node.js JavaScript client library title: Node.js JavaScript client library
seotitle: InfluxDB JavaScript client library seotitle: Use the InfluxDB JavaScript client library
list_title: Node.js list_title: Node.js
description: > description: >
Use the Node.js JavaScript client library to interact with InfluxDB. Use the InfluxDB Node.js JavaScript client library to interact with InfluxDB.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Node.js name: Node.js

View File

@ -1,7 +1,8 @@
--- ---
title: Install the JavaScript client library title: Install the InfluxDB JavaScript client library
seotitle: Install the InfluxDB Node.js JavaScript client library
description: > description: >
Install the Node.js JavaScript client library to interact with the InfluxDB API. Install the JavaScript client library to interact with InfluxDB in Node.js.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Install name: Install

View File

@ -1,7 +1,7 @@
--- ---
title: Query data with the JavaScript client library title: Query with the InfluxDB JavaScript client library
description: > description: >
Use the Node.js JavaScript client library to query data with the InfluxDB API. Use the JavaScript client library to query data with the InfluxDB API in Node.js.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Query name: Query

View File

@ -1,7 +1,7 @@
--- ---
title: Write data with the JavaScript client library title: Write data with the InfluxDB JavaScript client library
description: > description: >
Use the Node.js JavaScript client library to write data with the InfluxDB API. Use the JavaScript client library to write data with the InfluxDB API in Node.js.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Write name: Write
@ -11,6 +11,8 @@ weight: 101
aliases: aliases:
- /influxdb/cloud/reference/api/client-libraries/js/write - /influxdb/cloud/reference/api/client-libraries/js/write
- /influxdb/cloud/reference/api/client-libraries/nodejs/write - /influxdb/cloud/reference/api/client-libraries/nodejs/write
related:
- /influxdb/cloud/write-data/troubleshoot/
--- ---
{{< duplicate-oss >}} {{< duplicate-oss >}}

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB PHP client library title: PHP client library
seotitle: Use the InfluxDB PHP client library
list_title: PHP list_title: PHP
description: Use the PHP client library to interact with InfluxDB. description: Use the InfluxDB PHP client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-php external_url: https://github.com/influxdata/influxdb-client-php
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,8 +1,9 @@
--- ---
title: Python client library title: Python client library
seotitle: Use the InfluxDB Python client library
list_title: Python list_title: Python
description: > description: >
Use the Python client library to interact with InfluxDB. Use the InfluxDB Python client library to interact with InfluxDB.
menu: menu:
influxdb_cloud: influxdb_cloud:
name: Python name: Python

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB client R package title: Ruby client library
list_title: R seotitle: Use the InfluxDB Ruby client library
description: Use the client R package to interact with InfluxDB. list_title: Ruby
description: Use the InfluxDB client R package to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-r external_url: https://github.com/influxdata/influxdb-client-r
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Ruby client library title: Ruby client library
seotitle: Use the InfluxDB Ruby client library
list_title: Ruby list_title: Ruby
description: Use the Ruby client library to interact with InfluxDB. description: Use the InfluxDB Ruby client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-ruby external_url: https://github.com/influxdata/influxdb-client-ruby
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Scala client library title: Scala client library
seotitle: Use the InfluxDB Scala client library
list_title: Scala list_title: Scala
description: Use the Scala client library to interact with InfluxDB. description: Use the InfluxDB Scala client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-scala external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-scala
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Swift client library title: Swift client library
seotitle: Use the InfluxDB Swift client library
list_title: Swift list_title: Swift
description: Use the Swift client library to interact with InfluxDB. description: Use the InfluxDB Swift client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-swift external_url: https://github.com/influxdata/influxdb-client-swift
menu: menu:
influxdb_cloud: influxdb_cloud:

View File

@ -89,14 +89,11 @@ For specific instructions, see the [InfluxDB client libraries documentation](/in
{{% tab-content %}} {{% tab-content %}}
### Use gzip compression with the InfluxDB API ### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, set the `Content-Encoding` When using the InfluxDB API `/write` endpoint to write data,
header to `gzip` to compress the request data. compress the data with `gzip` and set the `Content-Encoding` header to `gzip`.
```sh ```sh
curl -XPOST "https://cloud2.influxdata.com/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
--header "Authorization: Token YOURAUTHTOKEN" \
--header "Content-Encoding: gzip" \
--data-raw "mem,host=host1 used_percent=23.43234543 1556896326"
``` ```
{{% /tab-content %}} {{% /tab-content %}}
{{< /tabs-wrapper >}} {{< /tabs-wrapper >}}

View File

@ -16,51 +16,39 @@ Use the `POST` request method and include the following in your request:
| Organization | Use the `org` query parameter in your request URL. | | Organization | Use the `org` query parameter in your request URL. |
| Bucket | Use the `bucket` query parameter in your request URL. | | Bucket | Use the `bucket` query parameter in your request URL. |
| Precision | Use the [`precision`](/influxdb/cloud/write-data/#timestamp-precision) query parameter in your request URL. Default is `ns` | | Precision | Use the [`precision`](/influxdb/cloud/write-data/#timestamp-precision) query parameter in your request URL. Default is `ns` |
| API token | Use the `Authorization: Token` header. | | API token | Use the `Authorization: Token YOUR_API_TOKEN` header. |
| Line protocol | Pass as plain text in your request body. | | Line protocol | Pass as plain text in your request body. |
#### Example #### Send a write request
The URL in the examples depends on your [InfluxDB Cloud region](/influxdb/cloud/reference/regions/). The URL in the examples depends on your [InfluxDB Cloud region](/influxdb/cloud/reference/regions/).
{{< code-tabs-wrapper >}} {{< code-tabs-wrapper >}}
{{% code-tabs %}} {{% code-tabs %}}
[Curl](#) [Curl](#curl)
[Node.js](#) [Node.js](#nodejs)
{{% /code-tabs %}} {{% /code-tabs %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```sh ```sh
curl -XPOST "https://cloud2.influxdata.com/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{< get-shared-text "api/v2.0/write/write.sh" >}}
--header "Authorization: Token YOURAUTHTOKEN" \
--data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```js ```js
{{< api/v2dot0/nodejs/write >}} {{< get-shared-text "api/v2.0/write/write.sh" >}}
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{< /code-tabs-wrapper >}} {{< /code-tabs-wrapper >}}
{{% note %}} {{% note %}}
To compress data when writing to InfluxDB, set the `Content-Encoding` header to `gzip`. ##### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, compress the data with `gzip` and set the `Content-Encoding`
header to `gzip`.
Compression reduces network bandwidth, but increases server-side load. Compression reduces network bandwidth, but increases server-side load.
```bash ```sh
curl -XPOST "https://cloud2.influxdata.com/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
--header "Authorization: Token YOURAUTHTOKEN" \
--header "Content-Encoding: gzip" \
--data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"
``` ```
{{% /note %}} {{% /note %}}

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Arduino client library title: Arduino client library
seotitle: Use the InfluxDB Arduino client library
list_title: Arduino list_title: Arduino
description: Use the Arduino client library to interact with InfluxDB. description: Use the InfluxDB Arduino client library to interact with InfluxDB.
external_url: https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino external_url: https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino
list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_ list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_
menu: menu:

View File

@ -1,15 +1,15 @@
--- ---
title: Get started with the JavaScript client library for web browsers title: JavaScript client library for web browsers
seotitle: Get started with the InfluxDB JavaScript client library for web browsers seotitle: Use the InfluxDB JavaScript client library for web browsers
list_title: JavaScript for browsers list_title: JavaScript for browsers
description: > description: >
Use the JavaScript client library example app to interact with the InfluxDB API in web browsers. Use the InfluxDB JavaScript client library to interact with InfluxDB in web clients.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: JavaScript for browsers name: JavaScript for browsers
identifier: client_js_browsers identifier: client_js_browsers
parent: Client libraries parent: Client libraries
influxdb/v2.0/tags: [client libraries, JavaScript] influxdb/v2.0/tags: [client libraries, JavaScript]
weight: 201 weight: 201
aliases: aliases:
- /influxdb/v2.0/reference/api/client-libraries/browserjs/ - /influxdb/v2.0/reference/api/client-libraries/browserjs/
@ -35,8 +35,7 @@ If you're just getting started with InfluxDB, see [Get started with InfluxDB](/{
* [Before you begin](#before-you-begin) * [Before you begin](#before-you-begin)
* [Use with module bundlers](#use-with-module-bundlers) * [Use with module bundlers](#use-with-module-bundlers)
* [Use bundled distributions with browsers and module loaders * [Use bundled distributions with browsers and module loaders](#use-bundled-distributions-with-browsers-and-module-loaders)
](#use-bundled-distributions-with-browsers-and-module-loaders)
* [Get started with the example app](#get-started-with-the-example-app) * [Get started with the example app](#get-started-with-the-example-app)
## Before you begin ## Before you begin
@ -48,7 +47,8 @@ If you're just getting started with InfluxDB, see [Get started with InfluxDB](/{
## Use with module bundlers ## Use with module bundlers
If you use a module bundler like Webpack or Parcel, install `@influxdata/influxdb-client-browser`. For more information and examples, see [Node.js](/{{% latest "influxdb" %}}/api-guide/client-libraries/nodejs/). If you use a module bundler like Webpack or Parcel, install `@influxdata/influxdb-client-browser`.
For more information and examples, see [Node.js](/{{% latest "influxdb" %}}/api-guide/client-libraries/nodejs/).
## Use bundled distributions with browsers and module loaders ## Use bundled distributions with browsers and module loaders

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB C# client library title: C# client library
list_title: C# list_title: C#
description: Use the C# client library to interact with InfluxDB. seotitle: Use the InfluxDB C# client library
description: Use the InfluxDB C# client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-csharp external_url: https://github.com/influxdata/influxdb-client-csharp
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,9 +1,9 @@
--- ---
title: Go client library title: Go client library
seotitle: InfluxDB Go client library seotitle: Use the InfluxDB Go client library
list_title: Go list_title: Go
description: > description: >
Use the Go client library to interact with InfluxDB. Use the InfluxDB Go client library to interact with InfluxDB.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Go name: Go

View File

@ -1,5 +1,6 @@
--- ---
title: InfluxDB Java client library title: Java client library
seotitle: Use the InfluxDB Java client library
list_title: Java list_title: Java
description: Use the Java client library to interact with InfluxDB. description: Use the Java client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java external_url: https://github.com/influxdata/influxdb-client-java

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Kotlin client library title: Kotlin client library
seotitle: Use the Kotlin client library
list_title: Kotlin list_title: Kotlin
description: Use the Kotlin client library to interact with InfluxDB. description: Use the InfluxDB Kotlin client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,9 +1,9 @@
--- ---
title: Node.js JavaScript client library title: Node.js JavaScript client library
seotitle: InfluxDB JavaScript client library seotitle: Use the InfluxDB JavaScript client library
list_title: Node.js list_title: Node.js
description: > description: >
Use the Node.js JavaScript client library to interact with InfluxDB. Use the InfluxDB Node.js JavaScript client library to interact with InfluxDB.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Node.js name: Node.js

View File

@ -1,7 +1,8 @@
--- ---
title: Install the JavaScript client library title: Install the InfluxDB JavaScript client library
seotitle: Install the InfluxDB Node.js JavaScript client library
description: > description: >
Install the Node.js JavaScript client library to interact with the InfluxDB API. Install the JavaScript client library to interact with the InfluxDB API in Node.js.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Install name: Install
@ -13,7 +14,7 @@ aliases:
--- ---
## Install ## Install Node.js
1. Install [Node.js](https://nodejs.org/en/download/package-manager/). 1. Install [Node.js](https://nodejs.org/en/download/package-manager/).
@ -27,18 +28,32 @@ aliases:
npm init -y influx-node-app npm init -y influx-node-app
``` ```
## Install dependencies ## Install TypeScript
The JavaScript client contains two packages. Many of the client library examples use [TypeScript](https://www.typescriptlang.org/). Follow these steps to initialize the TypeScript project.
Add both as dependencies of your project.
1. Change to your project directory: 1. Install TypeScript and type definitions for Node.js.
```sh ```sh
cd influx-node-app npm i -g typescript && npm i --save-dev @types/node
```
2. Create a TypeScript configuration with default values.
```sh
tsc --init
```
3. Run the TypeScript compiler. To recompile your code automatically as you make changes, pass the `watch` flag to the compiler.
```sh
tsc -w -p
``` ```
2. Install `@influxdata/influxdb-client` for querying and writing data: ## Install dependencies
The JavaScript client library contains two packages: `@influxdata/influxdb-client` and `@influxdata/influxdb-client-apis`.
Add both as dependencies of your project.
1. Open a new terminal window and install `@influxdata/influxdb-client` for querying and writing data:
```sh ```sh
npm install --save @influxdata/influxdb-client npm install --save @influxdata/influxdb-client
@ -50,19 +65,29 @@ Add both as dependencies of your project.
npm install --save @influxdata/influxdb-client-apis npm install --save @influxdata/influxdb-client-apis
``` ```
## Next steps
Once you've installed the Javascript client library, you're ready to [write data](/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md) to InfluxDB or [get started](#get-started-with-examples) with other examples from the client library.
## Get started with examples ## Get started with examples
{{% note %}} {{% note %}}
The client examples include an [`env`](https://github.com/influxdata/influxdb-client-js/blob/master/examples/env.js) module for accessing your InfluxDB properties from environment variables or from `env.js`. The client examples include an [`env`](https://github.com/influxdata/influxdb-client-js/blob/master/examples/env.js) module for accessing your InfluxDB properties from environment variables or from `env.js`.
The examples will use these to interact with the InfluxDB API. The examples use these properties to interact with the InfluxDB API.
{{% /note %}} {{% /note %}}
1. Set environment variables or update `env.js` with 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). 1. Set environment variables or update `env.js` with 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).
```sh ```sh
# Environment variables export INFLUX_URL=http://localhost:8086
{{< api/v2dot0/env >}} export INFLUX_TOKEN=YOUR_API_TOKEN
export INFLUX_ORG=YOUR_ORG
export INFLUX_BUCKET=YOUR_BUCKET
``` ```
Replace the following:
- *`YOUR_API_TOKEN`*: InfluxDB API token
- *`YOUR_ORG`*: InfluxDB organization ID
- *`YOUR_BUCKET`*: InfluxDB bucket name
2. Run an example script. 2. Run an example script.

View File

@ -1,7 +1,7 @@
--- ---
title: Query data with the JavaScript client library title: Query data with the InfluxDB JavaScript client library
description: > description: >
Use the Node.js JavaScript client library to query data with the InfluxDB API. Use the JavaScript client library to query data with the InfluxDB API in Node.js.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Query name: Query
@ -12,64 +12,83 @@ aliases:
- /influxdb/v2.0/reference/api/client-libraries/nodejs/query - /influxdb/v2.0/reference/api/client-libraries/nodejs/query
--- ---
Use the Javascript library to query data from InfluxDB. Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) in a Node.js environment to query InfluxDB.
The following example sends a Flux query to an InfluxDB bucket and outputs rows from an observable table.
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`. ## Before you begin
2. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. - [Install the client library and other dependencies](/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md).
Provide your InfluxDB `org`.
```js ## Query InfluxDB
const queryApi = influxDB.getQueryApi(org)
1. Change to your new project directory and create a file for your query module.
```sh
cd influx-node-app && touch query.js
``` ```
3. Create a Flux query (including your `bucket` parameter). 2. Instantiate an `InfluxDB` client. Provide your InfluxDB URL and API token.
Use the `getQueryApi()` method of the client.
Provide your InfluxDB organization ID to create a configured **query client**.
```js
import { InfluxDB, Point } from '@influxdata/influxdb-client'
const queryApi = new InfluxDB({YOUR_URL, YOUR_API_TOKEN}).getQueryApi(YOUR_ORG)
```
Replace the following:
- *`YOUR_URL`*: InfluxDB URL
- *`YOUR_API_TOKEN`*: InfluxDB API token
- *`YOUR_ORG`*: InfluxDB organization ID
3. Create a Flux query for your InfluxDB bucket. Store the query as a string variable.
{{% warn %}}
To prevent SQL injection attacks, avoid concatenating unsafe user input with queries.
{{% /warn %}}
```js ```js
const fluxQuery = const fluxQuery =
'from(bucket:"<my-bucket>") 'from(bucket: "YOUR_BUCKET")
|> range(start: 0) |> range(start: 0)
|> filter(fn: (r) => r._measurement == "temperature")' |> filter(fn: (r) => r._measurement == "temperature")'
``` ```
Replace *`YOUR_BUCKET`* with the name of your InfluxDB bucket.
The **query client** sends the Flux query to InfluxDB and returns line table metadata and rows. 4. Use the `queryRows()` method of the query client to query InfluxDB.
`queryRows()` takes a Flux query and an [RxJS **Observer**](http://reactivex.io/rxjs/manual/overview.html#observer) object.
4. Use the `next` method to iterate over the rows. The client returns [table](/{{% latest "influxdb" %}}/reference/syntax/annotated-csv/#tables) metadata and rows as an [RxJS **Observable**](http://reactivex.io/rxjs/manual/overview.html#observable).
`queryRows()` subscribes your observer to the observable.
Finally, the observer logs the rows from the response to the terminal.
```js ```js
queryApi.queryRows(fluxQuery, { const observer = {
next(row: string[], tableMeta: FluxTableMetaData) { next(row, tableMeta) {
const o = tableMeta.toObject(row) const o = tableMeta.toObject(row)
// console.log(JSON.stringify(o, null, 2))
console.log( console.log(
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}` `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
) )
} }
} }
queryApi.queryRows(fluxQuery, observer)
``` ```
### Complete example query script ### Complete example
```js ```js
// performs query and receive line table metadata and rows {{% get-shared-text "api/v2.0/query/query.mjs" %}}
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/ ```
queryApi.queryRows(fluxQuery, {
next(row: string[], tableMeta: FluxTableMetaData) { To run the example from a file, set your InfluxDB environment variables and use `node` to execute the JavaScript file.
const o = tableMeta.toObject(row)
// console.log(JSON.stringify(o, null, 2)) ```sh
console.log( export INFLUX_URL=http://localhost:8086 && \
'${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}` export INFLUX_TOKEN=YOUR_API_TOKEN && \
) export INFLUX_ORG=YOUR_ORG && \
}, node query.js
error(error: Error) {
console.error(error)
console.log('\nFinished ERROR')
},
complete() {
console.log('\nFinished SUCCESS')
},
})
``` ```
{{% api/v2dot0/nodejs/learn-more %}} {{% api/v2dot0/nodejs/learn-more %}}

View File

@ -1,7 +1,7 @@
--- ---
title: Write data with the JavaScript client library title: Write data with the InfluxDB JavaScript client library
description: > description: >
Use the Node.js JavaScript client library to write data with the InfluxDB API. Use the JavaScript client library to write data with the InfluxDB API in Node.js.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Write name: Write
@ -10,31 +10,75 @@ influxdb/v2.0/tags: [client libraries, JavaScript]
weight: 101 weight: 101
aliases: aliases:
- /influxdb/v2.0/reference/api/client-libraries/nodejs/write - /influxdb/v2.0/reference/api/client-libraries/nodejs/write
related:
- /influxdb/v2.0/write-data/troubleshoot/
--- ---
Use the Javascript library to write data to InfluxDB in a Node.js environment. Use the [InfluxDB Javascript client library](https://github.com/influxdata/influxdb-client-js) to write data from a Node.js environment to InfluxDB.
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`. The Javascript client library includes the following convenient features for writing data to InfluxDB:
2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`. - Apply default tags to data points.
- Buffer points into batches to optimize data transfer.
- Automatically retry requests on failure.
- Set an optional HTTP proxy address for your network.
### Before you begin
- [Install the client library](/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md).
### Write data with the client library
1. Instantiate an `InfluxDB` client. Provide your InfluxDB URL and API token.
```js ```js
import {InfluxDB, Point} from '@influxdata/influxdb-client' import {InfluxDB, Point} from '@influxdata/influxdb-client'
const influxDB = new InfluxDB({url, token}) const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})
const writeApi = influxDB.getWriteApi(org, bucket)
``` ```
Replace the following:
- *`YOUR_URL`*: InfluxDB URL
- *`YOUR_API_TOKEN`*: InfluxDB API token
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. 2. Use the `getWriteApi()` method of the client to create a **write client**.
Provide your InfluxDB organization ID and bucket name.
```js ```js
writeApi.useDefaultTags({location: 'browser'}) const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)
const point1 = new Point('temperature') ```
.tag('example', 'index.html') Replace the following:
.floatField('value', 24) - *`YOUR_ORG`*: InfluxDB organization ID
console.log(`${point1}`) - *`YOUR_BUCKET`*: InfluxDB bucket name
3. To apply one or more [tags](/influxdb/v2.0/reference/glossary/#tag) to all points, use the `useDefaultTags()` method.
Provide tags as an object of key/value pairs.
```js
writeApi.useDefaultTags({region: 'west'})
```
4. Use the `Point()` constructor to create a [point](/influxdb/v2.0/reference/glossary/#point).
1. Call the constructor and provide a [measurement](/influxdb/v2.0/reference/glossary/#measurement).
2. To add one or more tags, chain the `tag()` method to the constructor.
Provide a `name` and `value`.
3. To add a field of type `float`, chain the `floatField()` method to the constructor.
Provide a `name` and `value`.
```js
const point1 = new Point('temperature')
.tag('sensor_id', 'TLM010')
.floatField('value', 24)
```
5. Use the `writePoint()` method to write the point to your InfluxDB bucket.
Finally, use the `close()` method to flush all pending writes.
The example logs the new data point followed by "WRITE FINISHED" to stdout.
```js
writeApi.writePoint(point1) writeApi.writePoint(point1)
writeApi.close()
writeApi.close().then(() => {
console.log('WRITE FINISHED')
})
``` ```
### Complete example ### Complete example
@ -45,26 +89,30 @@ Use the Javascript library to write data to InfluxDB in a Node.js environment.
[Node.js](#nodejs) [Node.js](#nodejs)
{{% /code-tabs %}} {{% /code-tabs %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```sh ```sh
{{< api/v2dot0/curl/write >}} {{< get-shared-text "api/v2.0/write/write.sh" >}}
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```js ```js
{{< api/v2dot0/nodejs/write >}} {{< get-shared-text "api/v2.0/write/write.mjs" >}}
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{< /code-tabs-wrapper >}} {{< /code-tabs-wrapper >}}
To run the example from a file, set your InfluxDB environment variables and use `node` to execute the JavaScript file.
```sh
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
```
### Response codes ### Response codes
_For information about **InfluxDB API response codes**, see _For information about **InfluxDB API response codes**, see
[InfluxDB API Write documentation](/influxdb/cloud/api/#operation/PostWrite)._ [InfluxDB API Write documentation](/influxdb/cloud/api/#operation/PostWrite)._
### Compressing data
To compress data when writing to InfluxDB, set the `Content-Encoding` header to `gzip`.
Compression reduces network bandwidth, but increases server-side load.
```sh
{{< api/v2dot0/curl/write-compressed >}}
```

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB PHP client library title: PHP client library
seotitle: Use the InfluxDB PHP client library
list_title: PHP list_title: PHP
description: Use the PHP client library to interact with InfluxDB. description: Use the InfluxDB PHP client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-php external_url: https://github.com/influxdata/influxdb-client-php
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,8 +1,9 @@
--- ---
title: Python client library title: Python client library
seotitle: Use the InfluxDB Python client library
list_title: Python list_title: Python
description: > description: >
Use the Python client library to interact with InfluxDB. Use the InfluxDB Python client library to interact with InfluxDB.
menu: menu:
influxdb_2_0: influxdb_2_0:
name: Python name: Python

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB client R package title: R package client library
list_title: R list_title: R
description: Use the client R package to interact with InfluxDB. seotitle: Use the InfluxDB client R package
description: Use the InfluxDB client R package to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-r external_url: https://github.com/influxdata/influxdb-client-r
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Ruby client library title: Ruby client library
seotitle: Use the InfluxDB Ruby client library
list_title: Ruby list_title: Ruby
description: Use the Ruby client library to interact with InfluxDB. description: Use the InfluxDB Ruby client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-ruby external_url: https://github.com/influxdata/influxdb-client-ruby
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Scala client library title: Scala client library
seotitle: Use the InfluxDB Scala client library
list_title: Scala list_title: Scala
description: Use the Scala client library to interact with InfluxDB. description: Use the InfluxDB Scala client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-scala external_url: https://github.com/influxdata/influxdb-client-java/tree/master/client-scala
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -1,7 +1,8 @@
--- ---
title: InfluxDB Swift client library title: Swift client library
seotitle: Use the InfluxDB Swift client library
list_title: Swift list_title: Swift
description: Use the Swift client library to interact with InfluxDB. description: Use the InfluxDB Swift client library to interact with InfluxDB.
external_url: https://github.com/influxdata/influxdb-client-swift external_url: https://github.com/influxdata/influxdb-client-swift
menu: menu:
influxdb_2_0: influxdb_2_0:

View File

@ -57,7 +57,7 @@ _Specify timestamp precision when [writing to InfluxDB](/influxdb/v2.0/write-dat
## Use gzip compression ## Use gzip compression
Use gzip compression to speed up writes to InfluxDB. Use gzip compression to speed up writes to InfluxDB and reduce network bandwidth.
Benchmarks have shown up to a 5x speed improvement when data is compressed. Benchmarks have shown up to a 5x speed improvement when data is compressed.
{{< tabs-wrapper >}} {{< tabs-wrapper >}}
@ -67,6 +67,7 @@ Benchmarks have shown up to a 5x speed improvement when data is compressed.
[InfluxDB API](#) [InfluxDB API](#)
{{% /tabs %}} {{% /tabs %}}
{{% tab-content %}} {{% tab-content %}}
### Enable gzip compression in Telegraf ### Enable gzip compression in Telegraf
In the `influxdb_v2` output plugin configuration in your `telegraf.conf`, set the In the `influxdb_v2` output plugin configuration in your `telegraf.conf`, set the
@ -80,6 +81,7 @@ In the `influxdb_v2` output plugin configuration in your `telegraf.conf`, set th
``` ```
{{% /tab-content %}} {{% /tab-content %}}
{{% tab-content %}} {{% tab-content %}}
### Enable gzip compression in InfluxDB client libraries ### Enable gzip compression in InfluxDB client libraries
Each [InfluxDB client library](/influxdb/v2.0/api-guide/client-libraries/) provides Each [InfluxDB client library](/influxdb/v2.0/api-guide/client-libraries/) provides
@ -88,16 +90,14 @@ The method for enabling compression is different for each library.
For specific instructions, see the [InfluxDB client libraries documentation](/influxdb/v2.0/api-guide/client-libraries/). For specific instructions, see the [InfluxDB client libraries documentation](/influxdb/v2.0/api-guide/client-libraries/).
{{% /tab-content %}} {{% /tab-content %}}
{{% tab-content %}} {{% tab-content %}}
### Use gzip compression with the InfluxDB API ### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, set the `Content-Encoding` When using the InfluxDB API `/write` endpoint to write data, compress the data with `gzip` and set the `Content-Encoding`
header to `gzip` to compress the request data. header to `gzip`.
```sh ```sh
curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
--header "Authorization: Token YOURAUTHTOKEN" \
--header "Content-Encoding: gzip" \
--data-raw "mem,host=host1 used_percent=23.43234543 1556896326"
``` ```
{{% /tab-content %}} {{% /tab-content %}}
{{< /tabs-wrapper >}} {{< /tabs-wrapper >}}

View File

@ -16,48 +16,42 @@ Use the `POST` request method and include the following in your request:
| Organization | Use the `org` query parameter in your request URL. | | Organization | Use the `org` query parameter in your request URL. |
| Bucket | Use the `bucket` query parameter in your request URL. | | Bucket | Use the `bucket` query parameter in your request URL. |
| Timestamp precision | Use the [`precision`](/influxdb/v2.0/write-data/#timestamp-precision) query parameter in your request URL. Default is `ns`. | | Timestamp precision | Use the [`precision`](/influxdb/v2.0/write-data/#timestamp-precision) query parameter in your request URL. Default is `ns`. |
| API token | Use the `Authorization: Token <API token>` header. | | API token | Use the `Authorization: Token YOUR_API_TOKEN` header. |
| Line protocol | Pass as plain text in your request body. | | Line protocol | Pass as plain text in your request body. |
#### Example API write request #### Send a write request
Below is an example API write request using `curl`. The URL in the examples depends on the version and location of your InfluxDB 2.0 instance.
The URL depends on the version and location of your InfluxDB 2.0 instance _(see [InfluxDB URLs](/{{< latest "influxdb" >}}/reference/urls/))_. <a href="#" class="url-trigger">Customize URLs in examples</a>
To compress data when writing to InfluxDB, set the `Content-Encoding` header to `gzip`.
Compressing write requests reduces network bandwidth, but increases server-side load.
{{< code-tabs-wrapper >}} {{< code-tabs-wrapper >}}
{{% code-tabs %}} {{% code-tabs %}}
[Uncompressed](#) [Curl](#curl)
[Compressed](#) [Node.js](#nodejs)
{{% /code-tabs %}} {{% /code-tabs %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```sh ```sh
curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{< get-shared-text "api/v2.0/write/write.sh" >}}
--header "Authorization: Token YOURAUTHTOKEN" \
--data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```bash ```js
curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \ {{< get-shared-text "api/v2.0/write/write.sh" >}}
--header "Authorization: Token YOURAUTHTOKEN" \
--header "Content-Encoding: gzip" \
--data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{< /code-tabs-wrapper >}} {{< /code-tabs-wrapper >}}
{{% note %}}
##### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, compress the data with `gzip` and set the `Content-Encoding`
header to `gzip`.
Compression reduces network bandwidth, but increases server-side load.
```sh
{{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
```
{{% /note %}}
_For information about **InfluxDB API response codes**, see _For information about **InfluxDB API response codes**, see
[InfluxDB API Write documentation](/influxdb/v2.0/api/#operation/PostWrite)._ [InfluxDB API Write documentation](/influxdb/v2.0/api/#operation/PostWrite)._

View File

@ -1,4 +0,0 @@
export INFLUX_URL=http://localhost:8086
export INFLUX_TOKEN=influxdbauthtoken
export INFLUX_ORG=emailaddress@yourorg.org
export INFLUX_BUCKET=your_data_bucket

View File

@ -0,0 +1,41 @@
'use strict'
/** @module query
* Queries a data point in 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 || ''
/**
* Instantiate the InfluxDB client
* with a configuration object.
*
* Get a query client configured for your org.
**/
const queryApi = new InfluxDB({url, token}).getQueryApi(org)
/** To avoid SQL injection, use a string literal for the query. */
const fluxQuery = 'from(bucket:"air_sensor") |> range(start: 0) |> filter(fn: (r) => r._measurement == "temperature")'
const fluxObserver = {
next(row, tableMeta) {
const o = tableMeta.toObject(row)
console.log(
`${o._time} ${o._measurement} in ${o.region} (${o.sensor_id}): ${o._field}=${o._value}`
)
},
error(error) {
console.error(error)
console.log('\nFinished ERROR')
},
complete() {
console.log('\nFinished SUCCESS')
}
}
/** Execute a query and receive line table metadata and rows. */
queryApi.queryRows(fluxQuery, fluxObserver)

View File

@ -0,0 +1,9 @@
curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
--header "Authorization: Token YOURAUTHTOKEN" \
--header "Content-Encoding: gzip" \
--data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"

View File

@ -0,0 +1,10 @@
echo "airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630525358
airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630525358" | gzip > air-sensors.gzip
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-Encoding: gzip" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary @air-sensors.gzip

View File

@ -1,13 +1,13 @@
"use strict" 'use strict'
/** @module write /** @module write
* Writes a data point to InfluxDB using the Node.js client. * Writes a data point to InfluxDB using the Javascript client library with Node.js.
**/ **/
import { InfluxDB, Point } from "@influxdata/influxdb-client" import { InfluxDB, Point } from '@influxdata/influxdb-client'
/** Environment variables **/ /** Environment variables **/
const url = process.env.INFLUX_URL const url = process.env.INFLUX_URL
const token = process.env.INFLUX_TOKEN const token = process.env.INFLUX_API_TOKEN
const org = process.env.INFLUX_ORG const org = process.env.INFLUX_ORG
const bucket = process.env.INFLUX_BUCKET const bucket = process.env.INFLUX_BUCKET
@ -16,6 +16,7 @@ const bucket = process.env.INFLUX_BUCKET
* with a configuration object. * with a configuration object.
**/ **/
const influxDB = new InfluxDB({ url, token }) const influxDB = new InfluxDB({ url, token })
/** /**
* Create a write client from the getWriteApi method. * Create a write client from the getWriteApi method.
* Provide your `org` and `bucket`. * Provide your `org` and `bucket`.
@ -23,12 +24,16 @@ const influxDB = new InfluxDB({ url, token })
const writeApi = influxDB.getWriteApi(org, bucket) const writeApi = influxDB.getWriteApi(org, bucket)
/** /**
* Setup default tags for all writes. * Apply default tags to all points.
**/ **/
writeApi.useDefaultTags({ location: "browser" }) writeApi.useDefaultTags({ region: 'west' })
const point1 = new Point("temperature")
.tag("example", "index.html") /**
.floatField("value", 24) * 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}`) console.log(` ${point1}`)
writeApi.writePoint(point1) writeApi.writePoint(point1)
@ -37,5 +42,5 @@ writeApi.writePoint(point1)
* Flush pending writes and close writeApi. * Flush pending writes and close writeApi.
**/ **/
writeApi.close().then(() => { writeApi.close().then(() => {
console.log("WRITE FINISHED") console.log('WRITE FINISHED')
}) })

View File

@ -0,0 +1,10 @@
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
'