5025 fix and test python examples (#5207)

* chore(v3): add test replacements

* fix(v3): get-started/query samples, make testable
pull/5210/head
Jason Stirnaman 2023-10-30 16:48:09 -05:00 committed by GitHub
parent fa399d9fb7
commit c4b97f9acf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 178 additions and 95 deletions

View File

@ -45,14 +45,15 @@ The examples in this section of the tutorial query the
{{< req type="key" text="Covered in this tutorial" color="magenta" >}} {{< req type="key" text="Covered in this tutorial" color="magenta" >}}
- [`influx3` data CLI](?t=influx3+CLI#execute-an-sql-query){{< req "\* " >}} - [`influx3` data CLI](?t=influx3+CLI#execute-an-sql-query){{< req "\* " >}}
- [InfluxDB v3 client libraries](/influxdb/cloud-dedicated/reference/client-libraries/v3/) - [InfluxDB v3 client libraries](/influxdb/cloud-dedicated/reference/client-libraries/v3/){{< req "\* " >}}
- [Flight clients](/influxdb/cloud-dedicated/reference/client-libraries/flight/){{< req "\* " >}} - [Flight clients](/influxdb/cloud-dedicated/reference/client-libraries/flight/)
- [Superset](/influxdb/cloud-dedicated/query-data/sql/execute-queries/superset/) - [Superset](/influxdb/cloud-dedicated/query-data/sql/execute-queries/superset/)
- [Grafana](/influxdb/cloud-dedicated/query-data/sql/execute-queries/grafana/) - [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/) - [InfluxQL with InfluxDB v1 HTTP API](/influxdb/cloud-dedicated/query-data/execute-queries/influxdb-v1-api/)
- [Chronograf](/chronograf/v1/) - [Chronograf](/chronograf/v1/)
{{% warn %}} {{% warn %}}
#### /api/v2/query not supported #### /api/v2/query not supported
The `/api/v2/query` API endpoint and associated tooling, such as the `influx` CLI and InfluxDB v2 client libraries, **arent** supported in {{% product-name %}}. The `/api/v2/query` API endpoint and associated tooling, such as the `influx` CLI and InfluxDB v2 client libraries, **arent** 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). 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._ _If your project's virtual environment is already running, skip to step 3._
1. Setup your Python virtual environment. 1. Create a directory for your project and change into it:
Inside of your project directory:
```sh ```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:
<!--pytest-codeblocks:cont-->
```sh ```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)). 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)).
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python-cli 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. 4. Create the `config.json` configuration.
<!-- code-placeholders breaks when indented here --> <!--pytest-codeblocks:cont-->
```sh ```sh
influx3 config \ influx3 config \
--name="config-dedicated" \ --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. 5. Enter the `influx3 sql` command and your SQL query statement.
<!--pytest-codeblocks:cont-->
```sh ```sh
influx3 sql "SELECT * influx3 sql "SELECT *
FROM home 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 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): [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:
<!-- Run for tests and hide from users.
```sh ```sh
python -m venv envs/virtual-env mkdir -p influxdb_py_client && cd $_
``` ```
-->
2. Activate the virtual environment. <!--pytest-codeblocks:cont-->
```sh ```sh
source ./envs/virtual-env/bin/activate python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate
``` ```
3. Install the following dependencies: 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: In your terminal, enter the following command:
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python pandas tabulate 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: 2. In `query.py`, enter the following sample code:
```py <!-- Import for tests and hide from users.
from influxdb_client_3 import InfluxDBClient3 ```python
import os import os
```
-->
# INFLUX_TOKEN is an environment variable you assigned to your <!--pytest-codeblocks:cont-->
# database READ token string
TOKEN = os.getenv('INFLUX_TOKEN') ```python
from influxdb_client_3 import InfluxDBClient3
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"DATABASE_TOKEN",
database="get-started", database=f"get-started",
) )
sql = ''' sql = '''
@ -344,6 +359,7 @@ _If your project's virtual environment is already running, skip to step 3._
''' '''
table = client.query(query=sql) table = client.query(query=sql)
assert table['room'], "Expect table to have room column."
print(table.to_pandas().to_markdown()) 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. 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=<ROOT_CERT_PATH>` option with the certificate path--for example: 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=<ROOT_CERT_PATH>` option with the certificate path--for example:
```py ```python
from influxdb_client_3 import InfluxDBClient3, flight_client_options from influxdb_client_3 import InfluxDBClient3, flight_client_options
import os import os
import certifi import certifi
TOKEN = os.getenv('INFLUX_TOKEN')
fh = open(certifi.where(), "r") fh = open(certifi.where(), "r")
cert = fh.read() cert = fh.read()
fh.close() fh.close()
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"DATABASE_TOKEN",
database="get-started", database=f"get-started",
flight_client_options=flight_client_options( flight_client_options=flight_client_options(
tls_root_certs=cert)) tls_root_certs=cert))
...
``` ```
For more information, see [`influxdb_client_3` query exceptions](/influxdb/cloud-dedicated/reference/client-libraries/v3/python/#query-exceptions). 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: 2. Enter the following command to run the program and query your {{% product-name omit=" Clustered" %}} cluster:
<!--pytest.mark.skip-->
```sh ```sh
python query.py 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: 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program:
<!--pytest.mark.skip-->
```sh ```sh
go mod tidy && go build && go run influxdb_go_client 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). [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: 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project:
<!--pytest.mark.skip-->
```sh ```sh
npm install --save @influxdata/influxdb3-client 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 %}}: 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}:
<!--pytest.mark.skip-->
```sh ```sh
node index.mjs 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, 4. To build and execute the program and query {{% product-name %}},
enter the following command in your terminal: enter the following commands in your terminal:
<!--pytest.mark.skip-->
```sh ```sh
dotnet run 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()`. - `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: 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example:
<!--pytest.mark.skip-->
```sh ```sh
mvn compile mvn compile
``` ```
@ -963,6 +988,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl
**Linux/MacOS** **Linux/MacOS**
<!--pytest.mark.skip-->
```sh ```sh
export MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED" 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: 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven:
<!--pytest.mark.skip-->
```sh ```sh
mvn exec:java -Dexec.mainClass="com.influxdbv3.App" mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
``` ```

View File

@ -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 "\* " >}} - [InfluxDB user interface (UI)](?t=InfluxDB+UI#execute-an-sql-query){{< req "\* " >}}
- [`influx3` data CLI](?t=influx3+CLI#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/) - [InfluxDB v3 client libraries](/influxdb/cloud-serverless/reference/client-libraries/v3/){{< req "\* " >}}
- [Flight clients](/influxdb/cloud-serverless/reference/client-libraries/flight/){{< req "\* " >}} - [Flight clients](/influxdb/cloud-serverless/reference/client-libraries/flight/)
- [Superset](/influxdb/cloud-serverless/query-data/sql/execute-queries/superset/) - [Superset](/influxdb/cloud-serverless/query-data/sql/execute-queries/superset/)
- [Grafana](/influxdb/cloud-serverless/query-data/sql/execute-queries/grafana/) - [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/) - [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). 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._ _If your project's virtual environment is already running, skip to step 3._
1. Setup your Python virtual environment. 1. Create a directory for your project and change into it:
Inside of your project directory:
```sh ```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:
<!--pytest-codeblocks:cont-->
```sh ```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)). 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)).
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python-cli 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. 4. Create the `config.json` configuration.
<!-- code-placeholders breaks when indented here --> <!--pytest-codeblocks:cont-->
```sh ```sh
influx3 config \ influx3 config create \
--name="config-serverless" \ --name="config-serverless" \
--database="get-started" \ --database="get-started" \
--host="{{< influxdb/host >}}" \ --host="{{< influxdb/host >}}" \
@ -302,6 +306,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. 5. Enter the `influx3 sql` command and your SQL query statement.
<!--pytest-codeblocks:cont-->
```sh ```sh
influx3 sql "SELECT * influx3 sql "SELECT *
FROM home FROM home
@ -309,9 +315,9 @@ _If your project's virtual environment is already running, skip to step 3._
AND time <= '2022-01-01T20:00:00Z'" AND time <= '2022-01-01T20:00:00Z'"
``` ```
`influx3` displays query results in your terminal. `influx3` displays query results in your terminal.
{{% /influxdb/custom-timestamps %}} {{% /influxdb/custom-timestamps %}}
<!--------------------------- END influx3 CONTENT ---------------------------> <!--------------------------- END influx3 CONTENT --------------------------->
{{% /tab-content %}} {{% /tab-content %}}
{{% 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 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): [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:
<!-- Run for tests and hide from users.
```sh ```sh
python -m venv envs/virtual-env mkdir -p influxdb_py_client && cd $_
``` ```
-->
2. Activate the virtual environment. <!--pytest-codeblocks:cont-->
```sh ```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" >}} {{< 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: In your terminal, enter the following command:
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python pandas tabulate 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: 2. In `query.py`, enter the following sample code:
```py <!-- Import for tests and hide from users.
from influxdb_client_3 import InfluxDBClient3 ```python
import os import os
```
-->
# INFLUX_TOKEN is an environment variable you assigned to your <!--pytest-codeblocks:cont-->
# API READ token string
TOKEN = os.getenv('INFLUX_TOKEN') ```python
from influxdb_client_3 import InfluxDBClient3
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"API_TOKEN",
database="get-started", database=f"get-started")
)
sql = ''' sql = '''
SELECT SELECT
@ -382,7 +395,8 @@ _If your project's virtual environment is already running, skip to step 3._
''' '''
table = client.query(query=sql) 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 >}} {{< 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. 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=<ROOT_CERT_PATH>` option with the certificate path--for example: 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=<ROOT_CERT_PATH>` option with the certificate path--for example:
```py ```python
from influxdb_client_3 import InfluxDBClient3, flight_client_options from influxdb_client_3 import InfluxDBClient3, flight_client_options
import os import os
import certifi import certifi
TOKEN = os.getenv('INFLUX_TOKEN')
fh = open(certifi.where(), "r") fh = open(certifi.where(), "r")
cert = fh.read() cert = fh.read()
fh.close() fh.close()
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"API_TOKEN",
database="get-started", database=f"get-started",
flight_client_options=flight_client_options( flight_client_options=flight_client_options(
tls_root_certs=cert)) tls_root_certs=cert))
...
``` ```
For more information, see [`influxdb_client_3` query exceptions](/influxdb/cloud-serverless/reference/client-libraries/v3/python/#query-exceptions). 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 %}}: 6. In your terminal, enter the following command to run the program and query {{% product-name %}}:
<!--pytest.mark.skip-->
```sh ```sh
python query.py 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: 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program:
<!--pytest.mark.skip-->
```sh ```sh
go mod tidy && go build && go run influxdb_go_client 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). [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: 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project:
<!--pytest.mark.skip-->
```sh ```sh
npm install --save @influxdata/influxdb3-client 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 %}}: 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}:
<!--pytest.mark.skip-->
```sh ```sh
node index.mjs 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 %}}, 4. To build and execute the program and query {{% product-name %}},
enter the following commands in your terminal: enter the following commands in your terminal:
<!--pytest.mark.skip-->
```sh ```sh
dotnet run 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()`. - `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: 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example:
<!--pytest.mark.skip-->
```sh ```sh
mvn compile mvn compile
``` ```
@ -1000,6 +1023,8 @@ _This tutorial assumes using Maven version 3.9, Java version >= 15, and an `infl
**Linux/MacOS** **Linux/MacOS**
<!--pytest.mark.skip-->
```sh ```sh
export MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED" 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: 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven:
<!--pytest.mark.skip-->
```sh ```sh
mvn exec:java -Dexec.mainClass="com.influxdbv3.App" mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
``` ```

View File

@ -56,7 +56,6 @@ The examples in this section of the tutorial query the
#### /api/v2/query not supported #### /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, **arent** supported in {{% product-name %}}. The `/api/v2/query` API endpoint and associated tooling, such as the `influx` CLI and InfluxDB v2 client libraries, **arent** supported in {{% product-name %}}.
{{% /warn %}} {{% /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). 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._ _If your project's virtual environment is already running, skip to step 3._
1. Setup your Python virtual environment. 1. Create a directory for your project and change into it:
Inside of your project directory:
```sh ```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:
<!--pytest-codeblocks:cont-->
```sh ```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)). 3. Install the CLI package (already installed in the [Write data section](/influxdb/clustered/get-started/write/?t=Python#write-line-protocol-to-influxdb)).
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python-cli 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. 4. Create the `config.json` configuration.
<!-- code-placeholders breaks when indented here --> <!--pytest-codeblocks:cont-->
```sh ```sh
influx3 config \ influx3 config \
--name="config-clustered" \ --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. 5. Enter the `influx3 sql` command and your SQL query statement.
<!--pytest-codeblocks:cont-->
```sh ```sh
influx3 sql "SELECT * influx3 sql "SELECT *
FROM home 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 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): [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:
<!-- Run for tests and hide from users.
```sh ```sh
python -m venv envs/virtual-env mkdir -p influxdb_py_client && cd $_
``` ```
-->
2. Activate the virtual environment. <!--pytest-codeblocks:cont-->
```sh ```sh
source ./envs/virtual-env/bin/activate python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate
``` ```
3. Install the following dependencies: 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: In your terminal, enter the following command:
<!--pytest-codeblocks:cont-->
```sh ```sh
pip install influxdb3-python pandas tabulate 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: 2. In `query.py`, enter the following sample code:
```py <!-- Import for tests and hide from users.
from influxdb_client_3 import InfluxDBClient3 ```python
import os import os
```
-->
# INFLUX_TOKEN is an environment variable you assigned to your <!--pytest-codeblocks:cont-->
# database READ token string
TOKEN = os.getenv('INFLUX_TOKEN') ```python
from influxdb_client_3 import InfluxDBClient3
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"DATABASE_TOKEN",
database="get-started", database=f"get-started",
) )
sql = ''' sql = '''
@ -346,6 +359,7 @@ _If your project's virtual environment is already running, skip to step 3._
''' '''
table = client.query(query=sql) table = client.query(query=sql)
assert table['room'], "Expect table to have room column."
print(table.to_pandas().to_markdown()) 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. 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=<ROOT_CERT_PATH>` option with the certificate path--for example: 3. When instantiating the client, pass the `flight_client_options.tls_root_certs=<ROOT_CERT_PATH>` option with the certificate path--for example:
```py ```python
from influxdb_client_3 import InfluxDBClient3, flight_client_options from influxdb_client_3 import InfluxDBClient3, flight_client_options
import os import os
import certifi import certifi
TOKEN = os.getenv('INFLUX_TOKEN')
fh = open(certifi.where(), "r") fh = open(certifi.where(), "r")
cert = fh.read() cert = fh.read()
fh.close() fh.close()
client = InfluxDBClient3( client = InfluxDBClient3(
host="{{< influxdb/host >}}", host=f"{{< influxdb/host >}}",
token=TOKEN, token=f"DATABASE_TOKEN",
database="get-started", database=f"get-started",
flight_client_options=flight_client_options( flight_client_options=flight_client_options(
tls_root_certs=cert)) tls_root_certs=cert))
...
``` ```
For more information, see [`influxdb_client_3` query exceptions](/influxdb/clustered/reference/client-libraries/v3/python/#query-exceptions). 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: 2. Enter the following command to run the program and query your {{% product-name omit=" Clustered" %}} cluster:
<!--pytest.mark.skip-->
```sh ```sh
python query.py 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: 4. In your terminal, enter the following command to install the necessary packages, build the module, and run the program:
<!--pytest.mark.skip-->
```sh ```sh
go mod tidy && go build && go run influxdb_go_client 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). [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: 2. If you haven't already, install the `@influxdata/influxdb3-client` JavaScript client library as a dependency to your project:
<!--pytest.mark.skip-->
```sh ```sh
npm install --save @influxdata/influxdb3-client 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 %}}: 9. In your terminal, execute `index.mjs` to write to and query {{% product-name %}}:
<!--pytest.mark.skip-->
```sh ```sh
node index.mjs 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, 4. To build and execute the program and query your {{% product-name omit=" Clustered" %}} cluster,
enter the following command in your terminal: enter the following command in your terminal:
<!--pytest.mark.skip-->
```sh ```sh
dotnet run 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()`. - `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: 4. In your terminal or editor, use Maven to to install dependencies and compile the project code--for example:
<!--pytest.mark.skip-->
```sh ```sh
mvn compile 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: 6. To run the app to write to and query {{% product-name %}}, execute `App.main()`--for example, using Maven:
<!--pytest.mark.skip-->
```sh ```sh
mvn exec:java -Dexec.mainClass="com.influxdbv3.App" mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
``` ```

View File

@ -22,9 +22,11 @@ for file in `find . -type f` ; do
# Shell-specific replacements. # Shell-specific replacements.
sed -i 's/API_TOKEN/$INFLUX_TOKEN/g; sed -i 's/API_TOKEN/$INFLUX_TOKEN/g;
s/ORG_ID/$INFLUX_ORG/g;
s/DATABASE_TOKEN/$INFLUX_TOKEN/g; s/DATABASE_TOKEN/$INFLUX_TOKEN/g;
s/BUCKET_NAME/$INFLUX_DATABASE/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 $file
fi fi
cat $file cat $file