From c4b97f9acf0ff070fadbee8de3d25a680dcf8bec Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 30 Oct 2023 16:48:09 -0500 Subject: [PATCH] 5025 fix and test python examples (#5207) * chore(v3): add test replacements * fix(v3): get-started/query samples, make testable --- .../cloud-dedicated/get-started/query.md | 87 ++++++++++----- .../cloud-serverless/get-started/query.md | 105 +++++++++++------- .../influxdb/clustered/get-started/query.md | 77 ++++++++----- test/run-tests.sh | 4 +- 4 files changed, 178 insertions(+), 95 deletions(-) diff --git a/content/influxdb/cloud-dedicated/get-started/query.md b/content/influxdb/cloud-dedicated/get-started/query.md index 4d68841b1..a72ec1406 100644 --- a/content/influxdb/cloud-dedicated/get-started/query.md +++ b/content/influxdb/cloud-dedicated/get-started/query.md @@ -45,14 +45,15 @@ The examples in this section of the tutorial query the {{< req type="key" text="Covered in this tutorial" color="magenta" >}} - [`influx3` data CLI](?t=influx3+CLI#execute-an-sql-query){{< req "\* " >}} -- [InfluxDB v3 client libraries](/influxdb/cloud-dedicated/reference/client-libraries/v3/) -- [Flight clients](/influxdb/cloud-dedicated/reference/client-libraries/flight/){{< req "\* " >}} +- [InfluxDB v3 client libraries](/influxdb/cloud-dedicated/reference/client-libraries/v3/){{< req "\* " >}} +- [Flight clients](/influxdb/cloud-dedicated/reference/client-libraries/flight/) - [Superset](/influxdb/cloud-dedicated/query-data/sql/execute-queries/superset/) - [Grafana](/influxdb/cloud-dedicated/query-data/sql/execute-queries/grafana/) - [InfluxQL with InfluxDB v1 HTTP API](/influxdb/cloud-dedicated/query-data/execute-queries/influxdb-v1-api/) - [Chronograf](/chronograf/v1/) {{% warn %}} + #### /api/v2/query not supported The `/api/v2/query` API endpoint and associated tooling, such as the `influx` CLI and InfluxDB v2 client libraries, **aren’t** supported in {{% product-name %}}. @@ -222,21 +223,24 @@ The following steps include setting up a Python virtual environment already covered in [Get started writing data](/influxdb/cloud-dedicated/get-started/write/?t=Python#write-line-protocol-to-influxdb). _If your project's virtual environment is already running, skip to step 3._ -1. Setup your Python virtual environment. - Inside of your project directory: +1. Create a directory for your project and change into it: ```sh - python -m venv envs/virtual-env + mkdir influx3-query-example && cd $_ ``` -2. Activate the virtual environment. +2. To create and activate a Python virtual environment, run the following command: + + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . envs/virtual-env/bin/activate ``` 3. Install the CLI package (already installed in the [Write data section](/influxdb/cloud-dedicated/get-started/write/?t=Python#write-line-protocol-to-influxdb)). + + ```sh pip install influxdb3-python-cli ``` @@ -246,7 +250,8 @@ _If your project's virtual environment is already running, skip to step 3._ 4. Create the `config.json` configuration. - + + ```sh influx3 config \ --name="config-dedicated" \ @@ -264,6 +269,8 @@ _If your project's virtual environment is already running, skip to step 3._ 5. Enter the `influx3 sql` command and your SQL query statement. + + ```sh influx3 sql "SELECT * FROM home @@ -289,16 +296,19 @@ _If your project's virtual environment is already running, skip to step 3._ 1. Open a terminal in the `influxdb_py_client` module directory you created in the [Write data section](/influxdb/cloud-dedicated/get-started/write/?t=Python#write-line-protocol-to-influxdb): - 1. To create your Python virtual environment, enter the following command in your terminal: + 1. To create and activate your Python virtual environment, enter the following command in your terminal: + + - 2. Activate the virtual environment. + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate ``` 3. Install the following dependencies: @@ -311,6 +321,8 @@ _If your project's virtual environment is already running, skip to step 3._ In your terminal, enter the following command: + + ```sh pip install influxdb3-python pandas tabulate ``` @@ -319,18 +331,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In `query.py`, enter the following sample code: - ```py - from influxdb_client_3 import InfluxDBClient3 + - # INFLUX_TOKEN is an environment variable you assigned to your - # database READ token string - TOKEN = os.getenv('INFLUX_TOKEN') + + + ```python + from influxdb_client_3 import InfluxDBClient3 client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", + host=f"{{< influxdb/host >}}", + token=f"DATABASE_TOKEN", + database=f"get-started", ) sql = ''' @@ -344,6 +359,7 @@ _If your project's virtual environment is already running, skip to step 3._ ''' table = client.query(query=sql) + assert table['room'], "Expect table to have room column." print(table.to_pandas().to_markdown()) ``` @@ -364,24 +380,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In your Python code, import `certifi` and call the `certifi.where()` method to retrieve the certificate path. 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=` option with the certificate path--for example: - ```py + ```python from influxdb_client_3 import InfluxDBClient3, flight_client_options import os import certifi - TOKEN = os.getenv('INFLUX_TOKEN') - fh = open(certifi.where(), "r") cert = fh.read() fh.close() client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", + host=f"{{< influxdb/host >}}", + token=f"DATABASE_TOKEN", + database=f"get-started", flight_client_options=flight_client_options( tls_root_certs=cert)) - ... ``` For more information, see [`influxdb_client_3` query exceptions](/influxdb/cloud-dedicated/reference/client-libraries/v3/python/#query-exceptions). @@ -419,6 +432,8 @@ _If your project's virtual environment is already running, skip to step 3._ 2. Enter the following command to run the program and query your {{% product-name omit=" Clustered" %}} cluster: + + ```sh python query.py ``` @@ -582,6 +597,8 @@ _If your project's virtual environment is already running, skip to step 3._ 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program: + + ```sh go mod tidy && go build && go run influxdb_go_client ``` @@ -601,6 +618,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j [Write data section](/influxdb/cloud-dedicated/get-started/write/?t=Nodejs). 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project: + + ```sh npm install --save @influxdata/influxdb3-client ``` @@ -700,6 +719,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}: + + ```sh node index.mjs ``` @@ -815,8 +836,10 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j } ``` -4. To build and execute the program and query your {{% product-name omit=" Clustered" %}} cluster, - enter the following command in your terminal: +4. To build and execute the program and query {{% product-name %}}, + enter the following commands in your terminal: + + ```sh dotnet run @@ -952,6 +975,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl - `App` defines a `main()` function that calls `Write.writeLineProtocol()` and `Query.querySQL()`. 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example: + + ```sh mvn compile ``` @@ -963,6 +988,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl **Linux/MacOS** + + ```sh export MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED" ``` @@ -975,6 +1002,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven: + + ```sh mvn exec:java -Dexec.mainClass="com.influxdbv3.App" ``` diff --git a/content/influxdb/cloud-serverless/get-started/query.md b/content/influxdb/cloud-serverless/get-started/query.md index f423f6680..640c51a06 100644 --- a/content/influxdb/cloud-serverless/get-started/query.md +++ b/content/influxdb/cloud-serverless/get-started/query.md @@ -46,8 +46,8 @@ The examples in this section of the tutorial query the [**get-started** bucket]( - [InfluxDB user interface (UI)](?t=InfluxDB+UI#execute-an-sql-query){{< req "\* " >}} - [`influx3` data CLI](?t=influx3+CLI#execute-an-sql-query){{< req "\* " >}} -- [InfluxDB v3 client libraries](/influxdb/cloud-serverless/reference/client-libraries/v3/) -- [Flight clients](/influxdb/cloud-serverless/reference/client-libraries/flight/){{< req "\* " >}} +- [InfluxDB v3 client libraries](/influxdb/cloud-serverless/reference/client-libraries/v3/){{< req "\* " >}} +- [Flight clients](/influxdb/cloud-serverless/reference/client-libraries/flight/) - [Superset](/influxdb/cloud-serverless/query-data/sql/execute-queries/superset/) - [Grafana](/influxdb/cloud-serverless/query-data/sql/execute-queries/grafana/) - [InfluxQL with InfluxDB v1 HTTP API](/influxdb/cloud-serverless/query-data/execute-queries/influxdb-v1-api/) @@ -261,21 +261,24 @@ The following steps include setting up a Python virtual environment already covered in [Get started writing data](/influxdb/cloud-serverless/get-started/write/?t=Python#write-line-protocol-to-influxdb). _If your project's virtual environment is already running, skip to step 3._ -1. Setup your Python virtual environment. - Inside of your project directory: +1. Create a directory for your project and change into it: ```sh - python -m venv envs/virtual-env + mkdir influx3-query-example && cd $_ ``` -2. Activate the virtual environment. +2. To create and activate a Python virtual environment, run the following command: + + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . envs/virtual-env/bin/activate ``` 3. Install the CLI package (already installed in the [Write data section](/influxdb/cloud-serverless/get-started/write/?t=Python#write-line-protocol-to-influxdb)). + + ```sh pip install influxdb3-python-cli ``` @@ -285,9 +288,10 @@ _If your project's virtual environment is already running, skip to step 3._ 4. Create the `config.json` configuration. - + + ```sh - influx3 config \ + influx3 config create \ --name="config-serverless" \ --database="get-started" \ --host="{{< influxdb/host >}}" \ @@ -302,16 +306,18 @@ _If your project's virtual environment is already running, skip to step 3._ 5. Enter the `influx3 sql` command and your SQL query statement. - ```sh - influx3 sql "SELECT * - FROM home - WHERE time >= '2022-01-01T08:00:00Z' - AND time <= '2022-01-01T20:00:00Z'" - ``` + -`influx3` displays query results in your terminal. + ```sh + influx3 sql "SELECT * + FROM home + WHERE time >= '2022-01-01T08:00:00Z' + AND time <= '2022-01-01T20:00:00Z'" + ``` - {{% /influxdb/custom-timestamps %}} + `influx3` displays query results in your terminal. + +{{% /influxdb/custom-timestamps %}} {{% /tab-content %}} {{% tab-content %}} @@ -327,19 +333,22 @@ _If your project's virtual environment is already running, skip to step 3._ 1. Open a terminal in the `influxdb_py_client` module directory you created in the [Write data section](/influxdb/cloud-serverless/get-started/write/?t=Python#write-line-protocol-to-influxdb): - 1. To create your Python virtual environment, enter the following command in your terminal: + 1. To create and activate your Python virtual environment, enter the following command in your terminal: + + - 2. Activate the virtual environment. + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate ``` - 3. Install the following dependencies: + 2. Install the following dependencies: {{< req type="key" text="Already installed in the [Write data section](/influxdb/cloud-serverless/get-started/write/?t=Python#write-line-protocol-to-influxdb)" color="magenta" >}} @@ -349,6 +358,8 @@ _If your project's virtual environment is already running, skip to step 3._ In your terminal, enter the following command: + + ```sh pip install influxdb3-python pandas tabulate ``` @@ -357,19 +368,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In `query.py`, enter the following sample code: - ```py - from influxdb_client_3 import InfluxDBClient3 + - # INFLUX_TOKEN is an environment variable you assigned to your - # API READ token string - TOKEN = os.getenv('INFLUX_TOKEN') + + + ```python + from influxdb_client_3 import InfluxDBClient3 client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", - ) + host=f"{{< influxdb/host >}}", + token=f"API_TOKEN", + database=f"get-started") sql = ''' SELECT @@ -382,7 +395,8 @@ _If your project's virtual environment is already running, skip to step 3._ ''' table = client.query(query=sql) - print(reader.to_pandas().to_markdown()) + assert table['room'], "Expect table to have room column." + print(table.to_pandas().to_markdown()) ``` {{< expand-wrapper >}} @@ -402,24 +416,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In your Python code, import `certifi` and call the `certifi.where()` method to retrieve the root certificate path. 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=` option with the certificate path--for example: - ```py + ```python from influxdb_client_3 import InfluxDBClient3, flight_client_options import os import certifi - TOKEN = os.getenv('INFLUX_TOKEN') - fh = open(certifi.where(), "r") cert = fh.read() fh.close() client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", + host=f"{{< influxdb/host >}}", + token=f"API_TOKEN", + database=f"get-started", flight_client_options=flight_client_options( tls_root_certs=cert)) - ... ``` For more information, see [`influxdb_client_3` query exceptions](/influxdb/cloud-serverless/reference/client-libraries/v3/python/#query-exceptions). @@ -456,6 +467,8 @@ _If your project's virtual environment is already running, skip to step 3._ 6. In your terminal, enter the following command to run the program and query {{% product-name %}}: + + ```sh python query.py ``` @@ -619,6 +632,8 @@ _If your project's virtual environment is already running, skip to step 3._ 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program: + + ```sh go mod tidy && go build && go run influxdb_go_client ``` @@ -638,6 +653,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j [Write data section](/influxdb/cloud-serverless/get-started/write/?t=Nodejs). 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project: + + ```sh npm install --save @influxdata/influxdb3-client ``` @@ -737,6 +754,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}: + + ```sh node index.mjs ``` @@ -855,6 +874,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j 4. To build and execute the program and query {{% product-name %}}, enter the following commands in your terminal: + + ```sh dotnet run ``` @@ -989,6 +1010,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl - `App` defines a `main()` function that calls `Write.writeLineProtocol()` and `Query.querySQL()`. 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example: + + ```sh mvn compile ``` @@ -1000,6 +1023,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl **Linux/MacOS** + + ```sh export MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED" ``` @@ -1012,6 +1037,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven: + + ```sh mvn exec:java -Dexec.mainClass="com.influxdbv3.App" ``` diff --git a/content/influxdb/clustered/get-started/query.md b/content/influxdb/clustered/get-started/query.md index 2a0f1a17d..9fe8c9e47 100644 --- a/content/influxdb/clustered/get-started/query.md +++ b/content/influxdb/clustered/get-started/query.md @@ -56,7 +56,6 @@ The examples in this section of the tutorial query the #### /api/v2/query not supported -The InfluxDB API `/api/v2/query` endpoint can't query an {{% product-name omit=" Clustered" %}} cluster. The `/api/v2/query` API endpoint and associated tooling, such as the `influx` CLI and InfluxDB v2 client libraries, **aren’t** supported in {{% product-name %}}. {{% /warn %}} @@ -224,21 +223,24 @@ The following steps include setting up a Python virtual environment already covered in [Get started writing data](/influxdb/clustered/get-started/write/?t=Python#write-line-protocol-to-influxdb). _If your project's virtual environment is already running, skip to step 3._ -1. Setup your Python virtual environment. - Inside of your project directory: +1. Create a directory for your project and change into it: ```sh - python -m venv envs/virtual-env + mkdir influx3-query-example && cd $_ ``` -2. Activate the virtual environment. +2. To create and activate a Python virtual environment, run the following command: + + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . envs/virtual-env/bin/activate ``` 3. Install the CLI package (already installed in the [Write data section](/influxdb/clustered/get-started/write/?t=Python#write-line-protocol-to-influxdb)). + + ```sh pip install influxdb3-python-cli ``` @@ -248,7 +250,8 @@ _If your project's virtual environment is already running, skip to step 3._ 4. Create the `config.json` configuration. - + + ```sh influx3 config \ --name="config-clustered" \ @@ -266,6 +269,8 @@ _If your project's virtual environment is already running, skip to step 3._ 5. Enter the `influx3 sql` command and your SQL query statement. + + ```sh influx3 sql "SELECT * FROM home @@ -291,16 +296,19 @@ _If your project's virtual environment is already running, skip to step 3._ 1. Open a terminal in the `influxdb_py_client` module directory you created in the [Write data section](/influxdb/clustered/get-started/write/?t=Python#write-line-protocol-to-influxdb): - 1. To create your Python virtual environment, enter the following command in your terminal: + 1. To create and activate your Python virtual environment, enter the following command in your terminal: + + - 2. Activate the virtual environment. + ```sh - source ./envs/virtual-env/bin/activate + python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate ``` 3. Install the following dependencies: @@ -313,6 +321,8 @@ _If your project's virtual environment is already running, skip to step 3._ In your terminal, enter the following command: + + ```sh pip install influxdb3-python pandas tabulate ``` @@ -321,18 +331,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In `query.py`, enter the following sample code: - ```py - from influxdb_client_3 import InfluxDBClient3 + - # INFLUX_TOKEN is an environment variable you assigned to your - # database READ token string - TOKEN = os.getenv('INFLUX_TOKEN') + + + ```python + from influxdb_client_3 import InfluxDBClient3 client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", + host=f"{{< influxdb/host >}}", + token=f"DATABASE_TOKEN", + database=f"get-started", ) sql = ''' @@ -346,6 +359,7 @@ _If your project's virtual environment is already running, skip to step 3._ ''' table = client.query(query=sql) + assert table['room'], "Expect table to have room column." print(table.to_pandas().to_markdown()) ``` @@ -366,24 +380,21 @@ _If your project's virtual environment is already running, skip to step 3._ 2. In your Python code, import `certifi` and call the `certifi.where()` method to retrieve the certificate path. 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=` option with the certificate path--for example: - ```py + ```python from influxdb_client_3 import InfluxDBClient3, flight_client_options import os import certifi - TOKEN = os.getenv('INFLUX_TOKEN') - fh = open(certifi.where(), "r") cert = fh.read() fh.close() client = InfluxDBClient3( - host="{{< influxdb/host >}}", - token=TOKEN, - database="get-started", + host=f"{{< influxdb/host >}}", + token=f"DATABASE_TOKEN", + database=f"get-started", flight_client_options=flight_client_options( tls_root_certs=cert)) - ... ``` For more information, see [`influxdb_client_3` query exceptions](/influxdb/clustered/reference/client-libraries/v3/python/#query-exceptions). @@ -421,6 +432,8 @@ _If your project's virtual environment is already running, skip to step 3._ 2. Enter the following command to run the program and query your {{% product-name omit=" Clustered" %}} cluster: + + ```sh python query.py ``` @@ -584,6 +597,8 @@ _If your project's virtual environment is already running, skip to step 3._ 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program: + + ```sh go mod tidy && go build && go run influxdb_go_client ``` @@ -603,6 +618,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j [Write data section](/influxdb/clustered/get-started/write/?t=Nodejs). 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project: + + ```sh npm install --save @influxdata/influxdb3-client ``` @@ -702,6 +719,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}: + + ```sh node index.mjs ``` @@ -820,6 +839,8 @@ _This tutorial assumes you installed Node.js and npm, and created an `influxdb_j 4. To build and execute the program and query your {{% product-name omit=" Clustered" %}} cluster, enter the following command in your terminal: + + ```sh dotnet run ``` @@ -954,6 +975,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl - `App` defines a `main()` function that calls `Write.writeLineProtocol()` and `Query.querySQL()`. 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example: + + ```sh mvn compile ``` @@ -977,6 +1000,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven: + + ```sh mvn exec:java -Dexec.mainClass="com.influxdbv3.App" ``` diff --git a/test/run-tests.sh b/test/run-tests.sh index bd984fe61..5bbfba434 100644 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -22,9 +22,11 @@ for file in `find . -type f` ; do # Shell-specific replacements. sed -i 's/API_TOKEN/$INFLUX_TOKEN/g; + s/ORG_ID/$INFLUX_ORG/g; s/DATABASE_TOKEN/$INFLUX_TOKEN/g; s/BUCKET_NAME/$INFLUX_DATABASE/g; - s/DATABASE_NAME/$INFLUX_DATABASE/g;' \ + s/DATABASE_NAME/$INFLUX_DATABASE/g; + s/get-started/$INFLUX_DATABASE/g;' \ $file fi cat $file