From 22cf8ccadc1373ffcb56ed7f154e8ceaa7501bb5 Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 13 Sep 2021 11:08:02 -0500 Subject: [PATCH] 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 list is sorted properly. * Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md Co-authored-by: Scott Anderson * Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md Co-authored-by: Scott Anderson * Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md Co-authored-by: Scott Anderson * Update content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md Co-authored-by: Scott Anderson * fix: customize example URLs (closes #3113) Co-authored-by: Scott Anderson --- .../api-guide/client-libraries/arduino.md | 5 +- .../api-guide/client-libraries/browserjs.md | 6 +- .../api-guide/client-libraries/csharp.md | 5 +- .../cloud/api-guide/client-libraries/go.md | 6 +- .../cloud/api-guide/client-libraries/java.md | 3 +- .../api-guide/client-libraries/kotlin.md | 5 +- .../client-libraries/nodejs/_index.md | 4 +- .../client-libraries/nodejs/install.md | 5 +- .../client-libraries/nodejs/query.md | 4 +- .../client-libraries/nodejs/write.md | 6 +- .../cloud/api-guide/client-libraries/php.md | 5 +- .../api-guide/client-libraries/python.md | 3 +- .../cloud/api-guide/client-libraries/r.md | 7 +- .../cloud/api-guide/client-libraries/ruby.md | 5 +- .../cloud/api-guide/client-libraries/scala.md | 5 +- .../cloud/api-guide/client-libraries/swift.md | 5 +- .../best-practices/optimize-writes.md | 9 +- .../cloud/write-data/developer-tools/api.md | 36 ++---- .../api-guide/client-libraries/arduino.md | 5 +- .../api-guide/client-libraries/browserjs.md | 14 +-- .../v2.0/api-guide/client-libraries/csharp.md | 5 +- .../v2.0/api-guide/client-libraries/go.md | 4 +- .../v2.0/api-guide/client-libraries/java.md | 3 +- .../v2.0/api-guide/client-libraries/kotlin.md | 5 +- .../client-libraries/nodejs/_index.md | 4 +- .../client-libraries/nodejs/install.md | 49 ++++++-- .../client-libraries/nodejs/query.md | 91 +++++++++------ .../client-libraries/nodejs/write.md | 106 +++++++++++++----- .../v2.0/api-guide/client-libraries/php.md | 5 +- .../v2.0/api-guide/client-libraries/python.md | 3 +- .../v2.0/api-guide/client-libraries/r.md | 5 +- .../v2.0/api-guide/client-libraries/ruby.md | 5 +- .../v2.0/api-guide/client-libraries/scala.md | 5 +- .../v2.0/api-guide/client-libraries/swift.md | 5 +- .../best-practices/optimize-writes.md | 14 +-- .../v2.0/write-data/developer-tools/api.md | 48 ++++---- layouts/shortcodes/api/v2dot0/env.sh | 4 - shared/text/api/v2.0/query/query.mjs | 41 +++++++ shared/text/api/v2.0/write-compressed.sh | 9 ++ shared/text/api/v2.0/write/write-compress.sh | 10 ++ .../text/api/v2.0/write}/write.mjs | 25 +++-- shared/text/api/v2.0/write/write.sh | 10 ++ 42 files changed, 382 insertions(+), 217 deletions(-) delete mode 100644 layouts/shortcodes/api/v2dot0/env.sh create mode 100644 shared/text/api/v2.0/query/query.mjs create mode 100644 shared/text/api/v2.0/write-compressed.sh create mode 100644 shared/text/api/v2.0/write/write-compress.sh rename {layouts/shortcodes/api/v2dot0/nodejs => shared/text/api/v2.0/write}/write.mjs (55%) create mode 100644 shared/text/api/v2.0/write/write.sh diff --git a/content/influxdb/cloud/api-guide/client-libraries/arduino.md b/content/influxdb/cloud/api-guide/client-libraries/arduino.md index b031863bf..a411af4f9 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/arduino.md +++ b/content/influxdb/cloud/api-guide/client-libraries/arduino.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/browserjs.md b/content/influxdb/cloud/api-guide/client-libraries/browserjs.md index 032f11cb5..71a8d988a 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/browserjs.md +++ b/content/influxdb/cloud/api-guide/client-libraries/browserjs.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/csharp.md b/content/influxdb/cloud/api-guide/client-libraries/csharp.md index 48f90a12d..b60f1b120 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/csharp.md +++ b/content/influxdb/cloud/api-guide/client-libraries/csharp.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/go.md b/content/influxdb/cloud/api-guide/client-libraries/go.md index dddac2dbc..b074bd4f8 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/go.md +++ b/content/influxdb/cloud/api-guide/client-libraries/go.md @@ -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 >}} \ No newline at end of file +{{< duplicate-oss >}} diff --git a/content/influxdb/cloud/api-guide/client-libraries/java.md b/content/influxdb/cloud/api-guide/client-libraries/java.md index ef96742a7..350ad971e 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/java.md +++ b/content/influxdb/cloud/api-guide/client-libraries/java.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/kotlin.md b/content/influxdb/cloud/api-guide/client-libraries/kotlin.md index 7d50fe9b7..c47846680 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/kotlin.md +++ b/content/influxdb/cloud/api-guide/client-libraries/kotlin.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/nodejs/_index.md b/content/influxdb/cloud/api-guide/client-libraries/nodejs/_index.md index f5b942665..dbbe9862c 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/nodejs/_index.md +++ b/content/influxdb/cloud/api-guide/client-libraries/nodejs/_index.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/nodejs/install.md b/content/influxdb/cloud/api-guide/client-libraries/nodejs/install.md index 24c229d50..16fe4f30c 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/nodejs/install.md +++ b/content/influxdb/cloud/api-guide/client-libraries/nodejs/install.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/nodejs/query.md b/content/influxdb/cloud/api-guide/client-libraries/nodejs/query.md index 4e51bd497..8a57f6fe2 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/nodejs/query.md +++ b/content/influxdb/cloud/api-guide/client-libraries/nodejs/query.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/nodejs/write.md b/content/influxdb/cloud/api-guide/client-libraries/nodejs/write.md index 8e9f9ea3c..961742dd3 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/nodejs/write.md +++ b/content/influxdb/cloud/api-guide/client-libraries/nodejs/write.md @@ -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 >}} diff --git a/content/influxdb/cloud/api-guide/client-libraries/php.md b/content/influxdb/cloud/api-guide/client-libraries/php.md index b29c29e2e..ca70c3a03 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/php.md +++ b/content/influxdb/cloud/api-guide/client-libraries/php.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/python.md b/content/influxdb/cloud/api-guide/client-libraries/python.md index e210975be..601c48562 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/python.md +++ b/content/influxdb/cloud/api-guide/client-libraries/python.md @@ -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 diff --git a/content/influxdb/cloud/api-guide/client-libraries/r.md b/content/influxdb/cloud/api-guide/client-libraries/r.md index 3c8bbc32a..f7681bedd 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/r.md +++ b/content/influxdb/cloud/api-guide/client-libraries/r.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/ruby.md b/content/influxdb/cloud/api-guide/client-libraries/ruby.md index 544c34953..198e74cf7 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/ruby.md +++ b/content/influxdb/cloud/api-guide/client-libraries/ruby.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/scala.md b/content/influxdb/cloud/api-guide/client-libraries/scala.md index bc3a931b7..d6bc90ea9 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/scala.md +++ b/content/influxdb/cloud/api-guide/client-libraries/scala.md @@ -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: diff --git a/content/influxdb/cloud/api-guide/client-libraries/swift.md b/content/influxdb/cloud/api-guide/client-libraries/swift.md index 3a1997ff6..f0ccbc96d 100644 --- a/content/influxdb/cloud/api-guide/client-libraries/swift.md +++ b/content/influxdb/cloud/api-guide/client-libraries/swift.md @@ -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: diff --git a/content/influxdb/cloud/write-data/best-practices/optimize-writes.md b/content/influxdb/cloud/write-data/best-practices/optimize-writes.md index 1ee84f5b0..6d5cccfb2 100644 --- a/content/influxdb/cloud/write-data/best-practices/optimize-writes.md +++ b/content/influxdb/cloud/write-data/best-practices/optimize-writes.md @@ -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 >}} diff --git a/content/influxdb/cloud/write-data/developer-tools/api.md b/content/influxdb/cloud/write-data/developer-tools/api.md index 52d22c175..abff97b98 100644 --- a/content/influxdb/cloud/write-data/developer-tools/api.md +++ b/content/influxdb/cloud/write-data/developer-tools/api.md @@ -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 %}} diff --git a/content/influxdb/v2.0/api-guide/client-libraries/arduino.md b/content/influxdb/v2.0/api-guide/client-libraries/arduino.md index 8945805bb..a00a2b1d6 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/arduino.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/arduino.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/browserjs.md b/content/influxdb/v2.0/api-guide/client-libraries/browserjs.md index 829094873..39de27652 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/browserjs.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/browserjs.md @@ -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 diff --git a/content/influxdb/v2.0/api-guide/client-libraries/csharp.md b/content/influxdb/v2.0/api-guide/client-libraries/csharp.md index 8eb90a7cb..0ae51b58e 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/csharp.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/csharp.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/go.md b/content/influxdb/v2.0/api-guide/client-libraries/go.md index a32225228..6a3e0744a 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/go.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/go.md @@ -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 diff --git a/content/influxdb/v2.0/api-guide/client-libraries/java.md b/content/influxdb/v2.0/api-guide/client-libraries/java.md index 01e67b67a..57b0324e9 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/java.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/java.md @@ -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 diff --git a/content/influxdb/v2.0/api-guide/client-libraries/kotlin.md b/content/influxdb/v2.0/api-guide/client-libraries/kotlin.md index 8337421c9..47fc45e4d 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/kotlin.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/kotlin.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/_index.md b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/_index.md index adf8abff5..e1a2f1853 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/_index.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/_index.md @@ -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 diff --git a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md index f39e9bb6b..f823c0539 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/install.md @@ -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. diff --git a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md index 499da6559..f6276b77f 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/query.md @@ -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:"") + '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 %}} diff --git a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md index 0a532e847..bc4e892fa 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/nodejs/write.md @@ -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 >}} -``` diff --git a/content/influxdb/v2.0/api-guide/client-libraries/php.md b/content/influxdb/v2.0/api-guide/client-libraries/php.md index ae5d9f03c..7f4098d2b 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/php.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/php.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/python.md b/content/influxdb/v2.0/api-guide/client-libraries/python.md index ef1ea9996..19b42dbf5 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/python.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/python.md @@ -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 diff --git a/content/influxdb/v2.0/api-guide/client-libraries/r.md b/content/influxdb/v2.0/api-guide/client-libraries/r.md index f82b4f63f..d4a98a2e8 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/r.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/r.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/ruby.md b/content/influxdb/v2.0/api-guide/client-libraries/ruby.md index eefd98d84..f17e5cafe 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/ruby.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/ruby.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/scala.md b/content/influxdb/v2.0/api-guide/client-libraries/scala.md index 150c124fe..235c617c2 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/scala.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/scala.md @@ -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: diff --git a/content/influxdb/v2.0/api-guide/client-libraries/swift.md b/content/influxdb/v2.0/api-guide/client-libraries/swift.md index 72a0f27d3..1433227f0 100644 --- a/content/influxdb/v2.0/api-guide/client-libraries/swift.md +++ b/content/influxdb/v2.0/api-guide/client-libraries/swift.md @@ -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: diff --git a/content/influxdb/v2.0/write-data/best-practices/optimize-writes.md b/content/influxdb/v2.0/write-data/best-practices/optimize-writes.md index c8c09ff95..b42850b42 100644 --- a/content/influxdb/v2.0/write-data/best-practices/optimize-writes.md +++ b/content/influxdb/v2.0/write-data/best-practices/optimize-writes.md @@ -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 >}} diff --git a/content/influxdb/v2.0/write-data/developer-tools/api.md b/content/influxdb/v2.0/write-data/developer-tools/api.md index 119fefbf9..7b5f0a537 100644 --- a/content/influxdb/v2.0/write-data/developer-tools/api.md +++ b/content/influxdb/v2.0/write-data/developer-tools/api.md @@ -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 ` 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. +Customize URLs in examples {{< 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)._ diff --git a/layouts/shortcodes/api/v2dot0/env.sh b/layouts/shortcodes/api/v2dot0/env.sh deleted file mode 100644 index 7878b9a65..000000000 --- a/layouts/shortcodes/api/v2dot0/env.sh +++ /dev/null @@ -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 diff --git a/shared/text/api/v2.0/query/query.mjs b/shared/text/api/v2.0/query/query.mjs new file mode 100644 index 000000000..c72409948 --- /dev/null +++ b/shared/text/api/v2.0/query/query.mjs @@ -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) diff --git a/shared/text/api/v2.0/write-compressed.sh b/shared/text/api/v2.0/write-compressed.sh new file mode 100644 index 000000000..a71fd3e60 --- /dev/null +++ b/shared/text/api/v2.0/write-compressed.sh @@ -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 +" diff --git a/shared/text/api/v2.0/write/write-compress.sh b/shared/text/api/v2.0/write/write-compress.sh new file mode 100644 index 000000000..ce8478396 --- /dev/null +++ b/shared/text/api/v2.0/write/write-compress.sh @@ -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 diff --git a/layouts/shortcodes/api/v2dot0/nodejs/write.mjs b/shared/text/api/v2.0/write/write.mjs similarity index 55% rename from layouts/shortcodes/api/v2dot0/nodejs/write.mjs rename to shared/text/api/v2.0/write/write.mjs index 578bb808d..f80aa4556 100644 --- a/layouts/shortcodes/api/v2dot0/nodejs/write.mjs +++ b/shared/text/api/v2.0/write/write.mjs @@ -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') }) diff --git a/shared/text/api/v2.0/write/write.sh b/shared/text/api/v2.0/write/write.sh new file mode 100644 index 000000000..521fad233 --- /dev/null +++ b/shared/text/api/v2.0/write/write.sh @@ -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 + ' +