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
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
list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_
menu:

View File

@ -1,9 +1,9 @@
---
title: Get started with the JavaScript client library for web browsers
seotitle: Get started with the InfluxDB JavaScript client library for web browsers
title: JavaScript client library for web browsers
seotitle: Use the InfluxDB JavaScript client library for web browsers
list_title: JavaScript for browsers
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:
influxdb_cloud:
name: JavaScript for browsers

View File

@ -1,7 +1,8 @@
---
title: InfluxDB C# client library
title: C# client library
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
menu:
influxdb_cloud:

View File

@ -1,9 +1,9 @@
---
title: Go client library
seotitle: InfluxDB Go client library
seotitle: Use the InfluxDB Go client library
list_title: Go
description: >
Use the Go client library to interact with InfluxDB.
Use the InfluxDB Go client library to interact with InfluxDB.
menu:
influxdb_cloud:
name: Go
@ -14,4 +14,4 @@ aliases:
- /influxdb/cloud/reference/api/client-libraries/go/
---
{{< duplicate-oss >}}
{{< duplicate-oss >}}

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
description: Use the Java client library to interact with InfluxDB.
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
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
menu:
influxdb_cloud:

View File

@ -1,9 +1,9 @@
---
title: Node.js JavaScript client library
seotitle: InfluxDB JavaScript client library
seotitle: Use the InfluxDB JavaScript client library
list_title: Node.js
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:
influxdb_cloud:
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: >
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:
influxdb_cloud:
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: >
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:
influxdb_cloud:
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: >
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:
influxdb_cloud:
name: Write
@ -11,6 +11,8 @@ weight: 101
aliases:
- /influxdb/cloud/reference/api/client-libraries/js/write
- /influxdb/cloud/reference/api/client-libraries/nodejs/write
related:
- /influxdb/cloud/write-data/troubleshoot/
---
{{< 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
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
menu:
influxdb_cloud:

View File

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

View File

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

View File

@ -89,14 +89,11 @@ For specific instructions, see the [InfluxDB client libraries documentation](/in
{{% tab-content %}}
### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, set the `Content-Encoding`
header to `gzip` to compress the request data.
When using the InfluxDB API `/write` endpoint to write data,
compress the data with `gzip` and set the `Content-Encoding` header to `gzip`.
```sh
curl -XPOST "https://cloud2.influxdata.com/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"
{{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
```
{{% /tab-content %}}
{{< /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. |
| 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` |
| 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. |
#### Example
#### Send a write request
The URL in the examples depends on your [InfluxDB Cloud region](/influxdb/cloud/reference/regions/).
{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[Curl](#)
[Node.js](#)
[Curl](#curl)
[Node.js](#nodejs)
{{% /code-tabs %}}
{{% code-tab-content %}}
```sh
curl -XPOST "https://cloud2.influxdata.com/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
--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
"
{{< get-shared-text "api/v2.0/write/write.sh" >}}
```
{{% /code-tab-content %}}
{{% code-tab-content %}}
```js
{{< api/v2dot0/nodejs/write >}}
{{< get-shared-text "api/v2.0/write/write.sh" >}}
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}
{{% 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.
```bash
curl -XPOST "https://cloud2.influxdata.com/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
"
```sh
{{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
```
{{% /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
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
list_note: _ contributed by [tobiasschuerg](https://github.com/tobiasschuerg)_
menu:

View File

@ -1,15 +1,15 @@
---
title: Get started with the JavaScript client library for web browsers
seotitle: Get started with the InfluxDB JavaScript client library for web browsers
title: JavaScript client library for web browsers
seotitle: Use the InfluxDB JavaScript client library for web browsers
list_title: JavaScript for browsers
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:
influxdb_2_0:
name: JavaScript for browsers
identifier: client_js_browsers
parent: Client libraries
influxdb/v2.0/tags: [client libraries, JavaScript]
influxdb/v2.0/tags: [client libraries, JavaScript]
weight: 201
aliases:
- /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)
* [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)
## Before you begin
@ -48,7 +47,8 @@ If you're just getting started with InfluxDB, see [Get started with InfluxDB](/{
## 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

View File

@ -1,7 +1,8 @@
---
title: InfluxDB C# client library
title: C# client library
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
menu:
influxdb_2_0:

View File

@ -1,9 +1,9 @@
---
title: Go client library
seotitle: InfluxDB Go client library
seotitle: Use the InfluxDB Go client library
list_title: Go
description: >
Use the Go client library to interact with InfluxDB.
Use the InfluxDB Go client library to interact with InfluxDB.
menu:
influxdb_2_0:
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
description: Use the Java client library to interact with InfluxDB.
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
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
menu:
influxdb_2_0:

View File

@ -1,9 +1,9 @@
---
title: Node.js JavaScript client library
seotitle: InfluxDB JavaScript client library
seotitle: Use the InfluxDB JavaScript client library
list_title: Node.js
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:
influxdb_2_0:
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: >
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:
influxdb_2_0:
name: Install
@ -13,7 +14,7 @@ aliases:
---
## Install
## Install Node.js
1. Install [Node.js](https://nodejs.org/en/download/package-manager/).
@ -27,18 +28,32 @@ aliases:
npm init -y influx-node-app
```
## Install dependencies
## Install TypeScript
The JavaScript client contains two packages.
Add both as dependencies of your project.
Many of the client library examples use [TypeScript](https://www.typescriptlang.org/). Follow these steps to initialize the TypeScript project.
1. Change to your project directory:
1. Install TypeScript and type definitions for Node.js.
```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
npm install --save @influxdata/influxdb-client
@ -50,19 +65,29 @@ Add both as dependencies of your project.
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
{{% 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 examples will use these to interact with the InfluxDB API.
The examples use these properties to interact with the InfluxDB API.
{{% /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).
```sh
# Environment variables
{{< api/v2dot0/env >}}
export INFLUX_URL=http://localhost:8086
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.

View File

@ -1,7 +1,7 @@
---
title: Query data with the JavaScript client library
title: Query data with the InfluxDB JavaScript client library
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:
influxdb_2_0:
name: Query
@ -12,64 +12,83 @@ aliases:
- /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**.
Provide your InfluxDB `org`.
- [Install the client library and other dependencies](/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md).
```js
const queryApi = influxDB.getQueryApi(org)
## Query InfluxDB
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
const fluxQuery =
'from(bucket:"<my-bucket>")
'from(bucket: "YOUR_BUCKET")
|> range(start: 0)
|> 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 `next` method to iterate over the 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.
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
queryApi.queryRows(fluxQuery, {
next(row: string[], tableMeta: FluxTableMetaData) {
const observer = {
next(row, tableMeta) {
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}`
`${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
// 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')
},
})
{{% get-shared-text "api/v2.0/query/query.mjs" %}}
```
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 && \
node query.js
```
{{% 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: >
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:
influxdb_2_0:
name: Write
@ -10,32 +10,76 @@ influxdb/v2.0/tags: [client libraries, JavaScript]
weight: 101
aliases:
- /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`.
2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`.
The Javascript client library includes the following convenient features for writing data to InfluxDB:
- 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.
```js
import {InfluxDB, Point} from '@influxdata/influxdb-client'
### Before you begin
const influxDB = new InfluxDB({url, token})
const writeApi = influxDB.getWriteApi(org, bucket)
```
- [Install the client library](/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md).
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.
### Write data with the client library
1. Instantiate an `InfluxDB` client. Provide your InfluxDB URL and API token.
```js
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
console.log(`${point1}`)
import {InfluxDB, Point} from '@influxdata/influxdb-client'
writeApi.writePoint(point1)
writeApi.close()
const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})
```
Replace the following:
- *`YOUR_URL`*: InfluxDB URL
- *`YOUR_API_TOKEN`*: InfluxDB API token
2. Use the `getWriteApi()` method of the client to create a **write client**.
Provide your InfluxDB organization ID and bucket name.
```js
const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)
```
Replace the following:
- *`YOUR_ORG`*: InfluxDB organization ID
- *`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.close().then(() => {
console.log('WRITE FINISHED')
})
```
### Complete example
@ -45,26 +89,30 @@ Use the Javascript library to write data to InfluxDB in a Node.js environment.
[Node.js](#nodejs)
{{% /code-tabs %}}
{{% code-tab-content %}}
```sh
{{< api/v2dot0/curl/write >}}
{{< get-shared-text "api/v2.0/write/write.sh" >}}
```
{{% /code-tab-content %}}
{{% code-tab-content %}}
```js
{{< api/v2dot0/nodejs/write >}}
{{< get-shared-text "api/v2.0/write/write.mjs" >}}
```
{{% /code-tab-content %}}
{{< /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
_For information about **InfluxDB API response codes**, see
[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
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
menu:
influxdb_2_0:

View File

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

View File

@ -1,7 +1,8 @@
---
title: InfluxDB client R package
title: R package client library
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
menu:
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
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
menu:
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
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
menu:
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
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
menu:
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 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.
{{< tabs-wrapper >}}
@ -67,6 +67,7 @@ Benchmarks have shown up to a 5x speed improvement when data is compressed.
[InfluxDB API](#)
{{% /tabs %}}
{{% tab-content %}}
### Enable gzip compression in Telegraf
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 %}}
### Enable gzip compression in InfluxDB client libraries
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/).
{{% /tab-content %}}
{{% tab-content %}}
### Use gzip compression with the InfluxDB API
When using the InfluxDB API `/write` endpoint to write data, set the `Content-Encoding`
header to `gzip` to compress the request data.
When using the InfluxDB API `/write` endpoint to write data, compress the data with `gzip` and set the `Content-Encoding`
header to `gzip`.
```sh
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"
{{% get-shared-text "api/v2.0/write/write-compress.sh" %}}
```
{{% /tab-content %}}
{{< /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. |
| 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`. |
| 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. |
#### Example API write request
#### Send a write request
Below is an example API write request using `curl`.
The URL depends on the version and location of your InfluxDB 2.0 instance _(see [InfluxDB URLs](/{{< latest "influxdb" >}}/reference/urls/))_.
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.
The URL in the examples depends on the version and location of your InfluxDB 2.0 instance.
<a href="#" class="url-trigger">Customize URLs in examples</a>
{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[Uncompressed](#)
[Compressed](#)
[Curl](#curl)
[Node.js](#nodejs)
{{% /code-tabs %}}
{{% code-tab-content %}}
```sh
curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
--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
"
{{< get-shared-text "api/v2.0/write/write.sh" >}}
```
{{% /code-tab-content %}}
{{% code-tab-content %}}
```bash
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
"
```js
{{< get-shared-text "api/v2.0/write/write.sh" >}}
```
{{% /code-tab-content %}}
{{< /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
[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
* 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 **/
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 bucket = process.env.INFLUX_BUCKET
@ -16,6 +16,7 @@ const bucket = process.env.INFLUX_BUCKET
* with a configuration object.
**/
const influxDB = new InfluxDB({ url, token })
/**
* Create a write client from the getWriteApi method.
* Provide your `org` and `bucket`.
@ -23,12 +24,16 @@ const influxDB = new InfluxDB({ url, token })
const writeApi = influxDB.getWriteApi(org, bucket)
/**
* Setup default tags for all writes.
* Apply default tags to all points.
**/
writeApi.useDefaultTags({ location: "browser" })
const point1 = new Point("temperature")
.tag("example", "index.html")
.floatField("value", 24)
writeApi.useDefaultTags({ region: 'west' })
/**
* 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}`)
writeApi.writePoint(point1)
@ -37,5 +42,5 @@ writeApi.writePoint(point1)
* Flush pending writes and close writeApi.
**/
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
'