Dedicated influxql (#4976)

* Restructure Dedicated query docs (#4967)

* restructure dedicated query docs for influxql, add execute influxql doc

* updated code examples

* Update content/influxdb/cloud-dedicated/query-data/influxql/execute-queries/influxdb-v1-api.md

---------

Co-authored-by: Jason Stirnaman <stirnamanj@gmail.com>

* added code-placeholder-key shortcoded, updated code placholder styles

* add note about authing with user/pass

* updated basic auth credential list

* Dedicated InfluxQL Python instructions (#4972)

* add python influxql instructions, update python sql instructions

* Apply suggestions from code review

Co-authored-by: Jason Stirnaman <stirnamanj@gmail.com>

---------

Co-authored-by: Jason Stirnaman <stirnamanj@gmail.com>

---------

Co-authored-by: Jason Stirnaman <stirnamanj@gmail.com>
pull/4975/head^2
Scott Anderson 2023-06-07 12:55:09 -06:00 committed by GitHub
parent 0eb12c1a62
commit 35788f34ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 776 additions and 349 deletions

View File

@ -75,7 +75,8 @@ pre {
@import "code-api-methods";
}
// Styles for calling out specific code in code blocks
////////////// Styles for calling out specific code in code blocks /////////////
///
span.code-callout, .code-placeholder {
font-weight:bold;
margin:0 .15rem;
@ -85,6 +86,7 @@ span.code-callout, .code-placeholder {
&.orange {color: $r-curacao;}
}
/////////////// Styles for code placeholders that can be updated ///////////////
.code-placeholder-wrapper {
display: inline;
position: relative;
@ -95,7 +97,8 @@ span.code-callout, .code-placeholder {
padding: 0;
line-height: 1.4em;
color: $code-placeholder;
font-weight:bold;
font-weight: normal;
font-style: normal;
border-bottom: 1px dotted;
transition: color .2s;
cursor: pointer;
@ -104,7 +107,7 @@ span.code-callout, .code-placeholder {
font-style: normal;
&::before {
margin-left: .2rem;
font-size: .9em;
font-size: .8em;
transition: opacity .2s;
opacity: .65;
z-index: -1;
@ -132,7 +135,7 @@ span.code-callout, .code-placeholder {
background-color: $sidebar-search-bg;
padding: 2px 20px 2px 6px;
color: $code-placeholder-hover;
font-weight: bold;
font-weight: normal;
&:focus {
outline: none;
border-color: rgba($code-placeholder-hover, .75);
@ -145,6 +148,7 @@ span.code-callout, .code-placeholder {
&:after {
content: "\e937";
color: $code-placeholder-hover;
font-family: 'icomoon-v4';
display: block;
position: absolute;
@ -153,6 +157,7 @@ span.code-callout, .code-placeholder {
}
}
}
.code-placeholder-key code {color: $code-placeholder}
////////////////////////////////////////////////////////////////////////////////

View File

@ -217,7 +217,7 @@ $diagram-arrow: $g6-smoke;
$flux-water-process-img: url('/img/flux/0-x-water-process-dark.svg');
// Code placeholder colors
$code-placeholder: #d43587;
$code-placeholder: #e659a2;
$code-placeholder-hover: $br-teal;
@import "dark/telegraf",

View File

@ -1,15 +0,0 @@
---
title: Execute queries
seotitle: Execute queries for data stored in an InfluxDB database
description: Use tools and libraries to query data stored in an InfluxDB database.
weight: 103
menu:
influxdb_cloud_dedicated:
name: Execute queries
parent: Query data
influxdb/cloud-dedicated/tags: [query]
---
There are multiple ways to execute queries with InfluxDB. Choose from the following options:
{{< children >}}

View File

@ -1,24 +0,0 @@
---
title: Query with Flight SQL
seotitle: Query InfluxDB with Arrow Flight SQL
description: >
Query your data using Apache Arrow Flight SQL protocol and clients.
weight: 201
menu:
influxdb_cloud_dedicated:
name: Query with Flight SQL
parent: Execute queries
influxdb/cloud-dedicated/tags: [query, flightsql]
---
Use [Apache Arrow Flight SQL](https://arrow.apache.org/) to query data
stored in an InfluxDB database.
> Arrow Flight SQL is a protocol for interacting with SQL databases using the [Arrow in-memory format](https://arrow.apache.org/docs/format/Columnar.html) and the [Flight RPC](https://arrow.apache.org/docs/format/Flight.html) framework.
>
> {{% caption %}}[Apache Arrow Flight SQL documentation](https://arrow.apache.org/docs/format/FlightSql.html){{% /caption %}}
Data platforms and clients that support the Flight SQL protocol can query data stored in an InfluxDB database.
Learn how to connect to InfluxDB and query your data using the following tools:
{{< children >}}

View File

@ -1,302 +0,0 @@
---
title: Use Python and the Flight SQL library to query data
description: >
Use Python and the `flightsql-dbapi` Flight SQL library to query data
stored in InfluxDB.
weight: 101
menu:
influxdb_cloud_dedicated:
parent: Query with Flight SQL
name: Use Python
identifier: query_with_python
influxdb/cloud-dedicated/tags: [query, flightsql, python]
related:
- /influxdb/cloud-dedicated/query-data/tools/pandas/
- /influxdb/cloud-dedicated/query-data/tools/pyarrow/
- /influxdb/cloud-dedicated/query-data/sql/
list_code_example: |
```py
from flightsql import FlightSQLClient
client = FlightSQLClient(host='cluster-id.influxdb.io',
token='INFLUX_READ_WRITE_TOKEN',
metadata={'database': 'INFLUX_DATABASE'},
features={'metadata-reflection': 'true'})
info = client.execute("SELECT * FROM home")
ticket = info.endpoints[0].ticket
reader = client.do_get(ticket)
```
---
Use Python and the Flight SQL library to query data stored in InfluxDB.
- [Get started using Python to query InfluxDB](#get-started-using-python-to-query-influxdb)
- [Create a Python virtual environment](#create-a-python-virtual-environment)
- [Install Python](#install-python)
- [Create a project virtual environment](#venv-install)
- [Install Anaconda](#conda-install)
- [Query InfluxDB using Flight SQL](#query-influxdb-using-flight-sql)
- [Install the Flight SQL Python Library](#install-the-flight-sql-python-library)
- [Create a query client](#create-a-query-client)
- [Execute a query](#execute-a-query)
- [Retrieve data for Flight SQL query results](#retrieve-data-for-flight-sql-query-results)
## Get started using Python to query InfluxDB
This guide follows the recommended practice of using Python _virtual environments_.
If you don't want to use virtual environments and you have Python installed,
continue to [Query InfluxDB using Flight SQL](#query-influxdb-using-flight-sql).
## Create a Python virtual environment
Python [virtual environments](https://docs.python.org/3/library/venv.html) keep
the Python interpreter and dependencies for your project self-contained and isolated from other projects.
To install Python and create a virtual environment, choose one of the following options:
- [Python venv](?t=venv#venv-install): The [`venv` module](https://docs.python.org/3/library/venv.html) comes standard in Python as of version 3.5.
- [Anaconda® Distribution](?t=Anaconda#conda-install): A Python/R data science distribution that provides Python and the **conda** package and environment manager.
{{< tabs-wrapper >}}
{{% tabs "small" %}}
[venv]()
[Anaconda]()
{{% /tabs %}}
{{% tab-content %}}
<!--------------------------------- Begin venv -------------------------------->
### Install Python
1. Follow the [Python installation instructions](https://wiki.python.org/moin/BeginnersGuide/Download)
to install a recent version of the Python programming language for your system.
2. Check that you can run `python` and `pip` commands.
`pip` is a package manager included in most Python distributions.
In your terminal, enter the following commands:
```sh
python --version
```
```sh
pip --version
```
Depending on your system, you may need to use version-specific commands--for example.
```sh
python3 --version
```
```sh
pip3 --version
```
If neither `pip` nor `pip<PYTHON_VERSION>` works, follow one of the [Pypa.io Pip installation](https://pip.pypa.io/en/stable/installation/) methods for your system.
### Create a project virtual environment {#venv-install}
1. Create a directory for your Python project and change to the new directory--for example:
```sh
mkdir ./PROJECT_DIRECTORY && cd $_
```
2. Use the Python `venv` module to create a virtual environment--for example:
```sh
python -m venv envs/virtualenv-1
```
`venv` creates the new virtual environment directory in your project.
3. To activate the new virtual environment in your terminal, run the `source` command and pass the file path of the virtual environment `activate` script:
```sh
source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activate
```
For example:
```sh
source envs/virtualenv-1/bin/activate
```
<!---------------------------------- End venv --------------------------------->
{{% /tab-content %}}
{{% tab-content %}}
<!-------------------------------- Begin conda -------------------------------->
### Install Anaconda {#conda-install}
1. Follow the [Anaconda installation instructions](https://docs.continuum.io/anaconda/install/) for your system.
2. Check that you can run the `conda` command:
```sh
conda
```
3. Use `conda` to create a virtual environment--for example:
```sh
conda create --prefix envs/virtualenv-1
```
`conda` creates a virtual environment in a directory named `./envs/virtualenv-1`.
4. To activate the new virtual environment, use the `conda activate` command and pass the directory path of the virtual environment:
```sh
conda activate envs/VIRTUAL_ENVIRONMENT_NAME
```
For example:
```sh
conda activate ./envs/virtualenv-1
```
<!--------------------------------- END conda --------------------------------->
{{% /tab-content %}}
{{< /tabs-wrapper >}}
When a virtual environment is activated, the name displays at the beginning of your terminal command line--for example:
{{% code-callout "(virtualenv-1)"%}}
```sh
(virtualenv-1) $ PROJECT_DIRECTORY
```
{{% /code-callout %}}
## Query InfluxDB using Flight SQL
1. [Install the Flight SQL Python Library](#install-the-flight-sql-python-library)
2. [Create a query client](#create-a-query-client)
3. [Execute a query](#execute-a-query)
### Install the Flight SQL Python Library
The [`flightsql-dbapi`](https://github.com/influxdata/flightsql-dbapi) Flight SQL library for Python provides a
[DB API 2](https://peps.python.org/pep-0249/) interface and
[SQLAlchemy](https://www.sqlalchemy.org/) dialect for
[Flight SQL](https://arrow.apache.org/docs/format/FlightSql.html).
Installing `flightsql-dbapi` also installs the [`pyarrow`](https://arrow.apache.org/docs/python/index.html) library that you'll use for working with Arrow data.
In your terminal, use `pip` to install `flightsql-dbapi`:
```sh
pip install flightsql-dbapi
```
With `flightsql-dbapi` and `pyarrow` installed, you're ready to query and analyze data stored in an InfluxDB database.
### Create a query client
The following example shows how to use Python with `flightsql-dbapi`
and the _DB API 2_ interface to instantiate a Flight SQL client configured for an InfluxDB database.
1. In your editor, copy and paste the following sample code to a new file--for example, `query-example.py`:
```py
# query-example.py
from flightsql import FlightSQLClient
# Instantiate a FlightSQLClient configured for your database
client = FlightSQLClient(host='cluster-id.influxdb.io',
token='INFLUX_READ_WRITE_TOKEN',
metadata={'database': 'INFLUX_DATABASE'},
features={'metadata-reflection': 'true'})
```
2. Replace the following configuration values:
- **`INFLUX_READ_WRITE_TOKEN`**: Your InfluxDB token with read permissions on the databases you want to query.
- **`INFLUX_DATABASE`**: The name of your InfluxDB database.
### Execute a query
To execute an SQL query, call the query client's `execute(query)` method and pass the query as a string.
#### Syntax {#execute-query-syntax}
```py
execute(query: str, call_options: Optional[FlightSQLCallOptions] = None)
```
#### Example {#execute-query-example}
```py
# query-example.py
from flightsql import FlightSQLClient
client = FlightSQLClient(host='cluster-id.influxdb.io',
token='INFLUX_READ_WRITE_TOKEN',
metadata={'database': 'INFLUX_DATABASE'},
features={'metadata-reflection': 'true'})
# Execute the query
info = client.execute("SELECT * FROM home")
```
The response contains a `flight.FlightInfo` object that contains metadata and an `endpoints: [...]` list. Each endpoint contains the following:
- A list of addresses where you can retrieve the data.
- A `ticket` value that identifies the data to retrieve.
Next, use the ticket to [retrieve data for Flight SQL query results](#retrieve-data-for-flight-sql-query-results)
### Retrieve data for Flight SQL query results
To retrieve Arrow data for a query result, call the client's `do_get(ticket)` method.
#### Syntax {#retrieve-data-syntax}
```py
do_get(ticket, call_options: Optional[FlightSQLCallOptions] = None)
```
#### Example {#retrieve-data-example}
The following sample shows how to use Python with `flightsql-dbapi` and `pyarrow` to query InfluxDB and retrieve data.
```py
# query-example.py
from flightsql import FlightSQLClient
# Instantiate a FlightSQLClient configured for a database
client = FlightSQLClient(host='cluster-id.influxdb.io',
token='INFLUX_READ_WRITE_TOKEN',
metadata={'database': 'INFLUX_DATABASE'},
features={'metadata-reflection': 'true'})
# Execute the query to retrieve FlightInfo
info = client.execute("SELECT * FROM home")
# Extract the token for retrieving data
ticket = info.endpoints[0].ticket
# Use the ticket to request the Arrow data stream.
# Return a FlightStreamReader for streaming the results.
reader = client.do_get(ticket)
# Read all data to a pyarrow.Table
table = reader.read_all()
```
`do_get(ticket)` returns a [`pyarrow.flight.FlightStreamReader`](https://arrow.apache.org/docs/python/generated/pyarrow.flight.FlightStreamReader.html) for streaming Arrow [record batches](https://arrow.apache.org/docs/python/data.html#record-batches).
To read data from the stream, call one of the following `FlightStreamReader` methods:
- `read_all()`: Read all record batches as a [`pyarrow.Table`](https://arrow.apache.org/docs/python/generated/pyarrow.Table.html).
- `read_chunk()`: Read the next RecordBatch and metadata.
- `read_pandas()`: Read all record batches and convert them to a [`pandas.DataFrame`](https://pandas.pydata.org/docs/reference/frame.html).
Next, learn how to use Python tools to work with time series data:
- [Use PyArrow](/influxdb/cloud-dedicated/query-data/tools/pyarrow/)
- [Use pandas](/influxdb/cloud-dedicated/query-data/tools/pandas/)

View File

@ -0,0 +1,19 @@
---
title: Query data with InfluxQL
description: >
Learn to use InfluxQL to query data stored in InfluxDB Cloud Dedicated.
menu:
influxdb_cloud_dedicated:
name: Query with InfluxQL
parent: Query data
weight: 102
influxdb/cloud-dedicated/tags: [query, influxql]
---
Learn to use InfluxQL to query data stored in InfluxDB Cloud Dedicated.
{{< children type="anchored-list" >}}
---
{{< children readmore=true hr=true >}}

View File

@ -0,0 +1,17 @@
---
title: Execute InfluxQL queries
description: >
Use tools and libraries to query data with InfluxQL stored in InfluxDB Cloud Dedicated.
weight: 201
menu:
influxdb_cloud_dedicated:
name: Execute InfluxQL queries
parent: Query with InfluxQL
identifier: influxql-execute-queries
influxdb/cloud-dedicated/tags: [query, influxql]
---
There are multiple ways to execute InfluxQL queries with {{< cloud-name >}}.
Choose from the following options:
{{< children readmore=true hr=true >}}

View File

@ -0,0 +1,126 @@
---
title: Use the InfluxDB v1 HTTP query API to query with InfluxQL
seotitle: Use InfluxQL and InfluxDB v1 HTTP query API
description: >
Use the InfluxDB v1 HTTP query API to query data in InfluxDB Cloud Dedicated
with InfluxQL.
weight: 401
menu:
influxdb_cloud_dedicated:
parent: influxql-execute-queries
name: Use the v1 query API
influxdb/cloud-dedicated/tags: [query, influxql, python]
list_code_example: |
```sh
curl --get https://cluster-id.influxdb.io/query \
--header "Authorization: Token DATABASE_TOKEN" \
--data-urlencode "db=DATABASE_NAME" \
--data-urlencode "q=SELECT * FROM home"
```
---
Use the InfluxDB v1 HTTP query API to query data in {{< cloud-name >}}
with InfluxQL.
The examples below use **cURL** to send HTTP requests to the InfluxDB v1 HTTP API,
but you can use any HTTP client.
{{% warn %}}
#### InfluxQL feature support
InfluxQL is being rearchitected to work with the InfluxDB IOx storage engine.
This process is ongoing and some InfluxQL features are still being implemented.
For information about the current implementation status of InfluxQL features,
see [InfluxQL feature support](/influxdb/cloud-dedicated/reference/influxql/feature-support/).
{{% /warn %}}
Use the v1 `/query` endpoint and the `GET` request method to query data with InfluxQL:
{{< api-endpoint endpoint="https://cluster-id.influxdb.io/query" method="get" api-ref="/influxdb/cloud-dedicated/api/#tag/Query" >}}
Provide the following with your request:
- **Headers:**
- **Authorization:** `Bearer DATABASE_TOKEN`
- **Query parameters:**
- **db**: Database to query
- **rp**: _(Optional)_ Retention policy to query
- **q**: URL-encoded InfluxQL query
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```sh
curl --get https://cluster-id.influxdb.io/query \
--header "Authorization: Token DATABASE_TOKEN" \
--data-urlencode "db=DATABASE_NAME" \
--data-urlencode "q=SELECT * FROM home"
```
{{% /code-placeholders %}}
Replace the following:
- {{% code-placeholder-key %}}`DATABASE_NAME`{{% /code-placeholder-key %}}:
Name of the database to query
- {{% code-placeholder-key %}}`DATABASE_TOKEN`{{% /code-placeholder-key %}}:
{{% note %}}
#### Authenticate with username and password
If using basic authentication or query string authentication (username and password)
to interact with the v1 HTTP query API, provide the following credentials:
- **username**: Arbitrary string _({{< cloud-name >}} ignores the username)_
- **password**: [Database token](/influxdb/cloud-dedicated/admin/tokens/) with read permission
on the database you want to query
{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[Basic Auth](#)
[Query String Auth](#)
{{% /code-tabs %}}
{{% code-tab-content %}}
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```sh
curl --get https://cluster-id.influxdb.io/query \
--header "Authorization: Basic ignored:DATABASE_TOKEN" \
--data-urlencode "db=DATABASE_NAME" \
--data-urlencode "q=SELECT * FROM home"
```
{{% /code-placeholders %}}
{{% /code-tab-content %}}
{{% code-tab-content %}}
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```sh
curl --get https://cluster-id.influxdb.io/query \
--data-urlencode "u=ignored" \
--data-urlencode "p=DATABASE_TOKEN" \
--data-urlencode "db=DATABASE_NAME" \
--data-urlencode "q=SELECT * FROM home"
```
{{% /code-placeholders %}}
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}
{{% /note %}}
## Return results as JSON or CSV
By default, the `/query` endpoint returns results in **JSON**, but it can also
return results in **CSV**. To return results as CSV, include the `Accept` header
with the `application/csv` or `text/csv` MIME type:
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```sh
curl --get https://cluster-id.influxdb.io/query \
--header "Authorization: Token DATABASE_TOKEN" \
--header "Accept: application/csv" \
--data-urlencode "db=DATABASE_NAME" \
--data-urlencode "q=SELECT * FROM home"
```
{{% /code-placeholders %}}
<!--
TO-DO: Explain how DBRP mappings work with bucket names
-->

View File

@ -0,0 +1,285 @@
---
title: Use Python to query data with InfluxQL
description: >
Use Python and the `influxdb3-python` library to query data stored in InfluxDB
with InfluxQL.
weight: 401
menu:
influxdb_cloud_dedicated:
parent: influxql-execute-queries
name: Use Python
identifier: query-with-python-influxql
influxdb/cloud-dedicated/tags: [query, influxql, python]
related:
- /influxdb/cloud-dedicated/query-data/tools/pandas/
- /influxdb/cloud-dedicated/query-data/tools/pyarrow/
- /influxdb/cloud-dedicated/reference/influxql/
list_code_example: |
```py
from influxdb_client_3 import InfluxDBClient3
# Instantiate an InfluxDB client
client = InfluxDBClient3(
host='cluster-id.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
# Execute the query and return an Arrow table
table = client.query(
query="SELECT * FROM home",
language="influxql"
)
# Return query results as a markdown table
print(table.to_pandas().to_markdown())
```
---
Use the `influxdb3-python` client library to query data stored in InfluxDB with InfluxQL.
The `influxdb3-client` uses Flight SQL to query data from InfluxDB and return
results in Apache Arrow format.
- [Get started using Python to query InfluxDB](#get-started-using-python-to-query-influxdb)
- [Create a Python virtual environment](#create-a-python-virtual-environment)
- [Install Python](#install-python)
- [Create a project virtual environment](#venv-install)
- [Install Anaconda](?t=Anaconda#conda-install)
- [Query InfluxDB](#query-influxdb)
- [Install the influxdb3-python library](#install-the-influxdb3-python-library)
- [Create an InfluxDB client](#create-an-influxdb-client)
- [Execute a query](#execute-a-query)
{{% warn %}}
#### InfluxQL feature support
InfluxQL is being rearchitected to work with the InfluxDB IOx storage engine.
This process is ongoing and some InfluxQL features are still being implemented.
For information about the current implementation status of InfluxQL features,
see [InfluxQL feature support](/influxdb/cloud-dedicated/reference/influxql/feature-support/).
{{% /warn %}}
## Get started using Python to query InfluxDB
This guide follows the recommended practice of using Python _virtual environments_.
If you don't want to use virtual environments and you have Python installed,
continue to [Query InfluxDB](#query-influxdb).
## Create a Python virtual environment
Python [virtual environments](https://docs.python.org/3/library/venv.html) keep
the Python interpreter and dependencies for your project self-contained and isolated from other projects.
To install Python and create a virtual environment, choose one of the following options:
- [Python venv](?t=venv#venv-install): The [`venv` module](https://docs.python.org/3/library/venv.html) comes standard in Python as of version 3.5.
- [Anaconda® Distribution](?t=Anaconda#conda-install): A Python/R data science distribution that provides Python and the **conda** package and environment manager.
{{< tabs-wrapper >}}
{{% tabs "small" %}}
[venv]()
[Anaconda]()
{{% /tabs %}}
{{% tab-content %}}
<!--------------------------------- Begin venv -------------------------------->
### Install Python
1. Follow the [Python installation instructions](https://wiki.python.org/moin/BeginnersGuide/Download)
to install a recent version of the Python programming language for your system.
2. Check that you can run `python` and `pip` commands.
`pip` is a package manager included in most Python distributions.
In your terminal, enter the following commands:
```sh
python --version
```
```sh
pip --version
```
Depending on your system, you may need to use version-specific commands--for example.
```sh
python3 --version
```
```sh
pip3 --version
```
If neither `pip` nor `pip<PYTHON_VERSION>` works, follow one of the [Pypa.io Pip installation](https://pip.pypa.io/en/stable/installation/) methods for your system.
### Create a project virtual environment {#venv-install}
1. Create a directory for your Python project and change to the new directory--for example:
```sh
mkdir ./PROJECT_DIRECTORY && cd $_
```
2. Use the Python `venv` module to create a virtual environment--for example:
```sh
python -m venv envs/virtualenv-1
```
`venv` creates the new virtual environment directory in your project.
3. To activate the new virtual environment in your terminal, run the `source` command and pass the file path of the virtual environment `activate` script:
```sh
source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activate
```
For example:
```sh
source envs/virtualenv-1/bin/activate
```
<!---------------------------------- End venv --------------------------------->
{{% /tab-content %}}
{{% tab-content %}}
<!-------------------------------- Begin conda -------------------------------->
### Install Anaconda {#conda-install}
1. Follow the [Anaconda installation instructions](https://docs.continuum.io/anaconda/install/) for your system.
2. Check that you can run the `conda` command:
```sh
conda
```
3. Use `conda` to create a virtual environment--for example:
```sh
conda create --prefix envs/virtualenv-1
```
`conda` creates a virtual environment in a directory named `./envs/virtualenv-1`.
4. To activate the new virtual environment, use the `conda activate` command and pass the directory path of the virtual environment:
```sh
conda activate envs/VIRTUAL_ENVIRONMENT_NAME
```
For example:
```sh
conda activate ./envs/virtualenv-1
```
<!--------------------------------- END conda --------------------------------->
{{% /tab-content %}}
{{< /tabs-wrapper >}}
When a virtual environment is activated, the name displays at the beginning of your terminal command line--for example:
{{% code-callout "(virtualenv-1)"%}}
```sh
(virtualenv-1) $ PROJECT_DIRECTORY
```
{{% /code-callout %}}
## Query InfluxDB
1. [Install the influxdb3-python library](#install-the-influxdb3-python-library)
2. [Create an InfluxDB client](#create-an-influxdb-client)
3. [Execute a query](#execute-a-query)
### Install the influxdb3-python library
The `influxdb_client_3` module provides a simple and convenient way to interact
with {{< cloud-name >}} using Python. This module supports both writing data to
InfluxDB and querying data using SQL or InfluxQL queries.
Installing `inflxudb3-python` also installs the
[`pyarrow`](https://arrow.apache.org/docs/python/index.html) library that you'll
use for working with Arrow data returned from queries.
In your terminal, use `pip` to install `influxdb3-python`:
```sh
pip install influxdb3-python
```
With `influxdb3-python` and `pyarrow` installed, you're ready to query and
analyze data stored in an InfluxDB database.
### Create an InfluxDB client
The following example shows how to use Python with `influxdb3-python`
and to instantiate a client configured for an InfluxDB database.
In your editor, copy and paste the following sample code to a new file--for
example, `query-example.py`:
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```py
# query-example.py
from influxdb_client_3 import InfluxDBClient3
# Instantiate an InfluxDBClient3 client configured for your database
client = InfluxDBClient3(
host='cluster-id.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
```
{{% /code-placeholders %}}
Replace the following configuration values:
- {{% code-placeholder-key %}}`DATABASE_TOKEN`{{% /code-placeholder-key %}}:
Your InfluxDB token with read permissions on the databases you want to query.
- {{% code-placeholder-key %}}`DATABASE_NAME`{{% /code-placeholder-key %}}:
The name of your InfluxDB database.
### Execute a query
To execute an InfluxQL query, call the client's `query(query,language)` method
and specify the following arguments:
- **query**: InfluxQL query string to execute.
- **language**: `influxql`
#### Syntax {#execute-query-syntax}
```py
query(query: str, language: str)
```
#### Example {#execute-query-example}
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```py
# query-example.py
from influxdb_client_3 import InfluxDBClient3
client = InfluxDBClient3(
host='cluster-id.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
# Execute the query and return an Arrow table
table = client.query(
query="SELECT * FROM home",
language="influxql"
)
# Return query results as a markdown table
print(table.to_pandas().to_markdown())
```
{{% /code-placeholders %}}
Next, learn how to use Python tools to work with time series data:
- [Use PyArrow](/influxdb/cloud-dedicated/query-data/tools/pyarrow/)
- [Use pandas](/influxdb/cloud-dedicated/query-data/tools/pandas/)

View File

@ -0,0 +1,30 @@
---
title: Execute SQL queries
description: >
Use tools and libraries to query data with SQL stored in InfluxDB Cloud Dedicated.
weight: 201
menu:
influxdb_cloud_dedicated:
name: Execute SQL queries
parent: Query with SQL
identifier: sql-execute-queries
influxdb/cloud-dedicated/tags: [query, sql]
aliases:
- /influxdb/cloud-dedicated/query-data/execute-queries/
---
The InfluxDB SQL implementation uses [Arrow DataFusion](https://arrow.apache.org/datafusion/)
and the [Apache Arrow Flight SQL](https://arrow.apache.org/) protocol to query
data stored in an InfluxDB database.
> Arrow Flight SQL is a protocol for interacting with SQL databases using the
> [Arrow in-memory format](https://arrow.apache.org/docs/format/Columnar.html)
> and the [Flight RPC](https://arrow.apache.org/docs/format/Flight.html) framework.
>
> {{% caption %}}[Apache Arrow Flight SQL documentation](https://arrow.apache.org/docs/format/FlightSql.html){{% /caption %}}
Data platforms and clients that support the Flight SQL protocol can query data
stored in an InfluxDB database.
Learn how to connect to InfluxDB and query your data using the following tools:
{{< children readmore=true hr=true hlevel="h2" >}}

View File

@ -0,0 +1,283 @@
---
title: Use Python to query data with SQL
description: >
Use Python and the `influxdb3-python` library to query data stored in InfluxDB
with SQL.
weight: 401
menu:
influxdb_cloud_dedicated:
parent: sql-execute-queries
name: Use Python
identifier: query-with-python-sql
influxdb/cloud-dedicated/tags: [query, flightsql, python]
aliases:
- /influxdb/cloud-dedicated/query-data/execute-queries/flight-sql/python/
related:
- /influxdb/cloud-dedicated/query-data/tools/pandas/
- /influxdb/cloud-dedicated/query-data/tools/pyarrow/
- /influxdb/cloud-dedicated/reference/sql/
list_code_example: |
```py
from influxdb_client_3 import InfluxDBClient3
# Instantiate an InfluxDB client
client = InfluxDBClient3(
host='cluster-id.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
# Execute the query and return an Arrow table
table = client.query(
query="SELECT * FROM home",
language="sql"
)
# Return query results as a markdown table
print(table.to_pandas().to_markdown())
```
---
Use the `influxdb3-python` client library to query data stored in InfluxDB with SQL.
The `influxdb3-client` uses Flight SQL to query data from InfluxDB and return
results in Apache Arrow format.
- [Get started using Python to query InfluxDB](#get-started-using-python-to-query-influxdb)
- [Create a Python virtual environment](#create-a-python-virtual-environment)
- [Install Python](#install-python)
- [Create a project virtual environment](#venv-install)
- [Install Anaconda](?t=Anaconda#conda-install)
- [Query InfluxDB](#query-influxdb)
- [Install the influxdb3-python library](#install-the-influxdb3-python-library)
- [Create an InfluxDB client](#create-an-influxdb-client)
- [Execute a query](#execute-a-query)
## Get started using Python to query InfluxDB
This guide follows the recommended practice of using Python _virtual environments_.
If you don't want to use virtual environments and you have Python installed,
continue to [Query InfluxDB](#query-influxdb).
## Create a Python virtual environment
Python [virtual environments](https://docs.python.org/3/library/venv.html) keep
the Python interpreter and dependencies for your project self-contained and isolated from other projects.
To install Python and create a virtual environment, choose one of the following options:
- [Python venv](?t=venv#venv-install): The [`venv` module](https://docs.python.org/3/library/venv.html) comes standard in Python as of version 3.5.
- [Anaconda® Distribution](?t=Anaconda#conda-install): A Python/R data science distribution that provides Python and the **conda** package and environment manager.
{{< tabs-wrapper >}}
{{% tabs "small" %}}
[venv]()
[Anaconda]()
{{% /tabs %}}
{{% tab-content %}}
<!--------------------------------- Begin venv -------------------------------->
### Install Python
1. Follow the [Python installation instructions](https://wiki.python.org/moin/BeginnersGuide/Download)
to install a recent version of the Python programming language for your system.
2. Check that you can run `python` and `pip` commands.
`pip` is a package manager included in most Python distributions.
In your terminal, enter the following commands:
```sh
python --version
```
```sh
pip --version
```
Depending on your system, you may need to use version-specific commands--for example.
```sh
python3 --version
```
```sh
pip3 --version
```
If neither `pip` nor `pip<PYTHON_VERSION>` works, follow one of the [Pypa.io Pip installation](https://pip.pypa.io/en/stable/installation/) methods for your system.
### Create a project virtual environment {#venv-install}
1. Create a directory for your Python project and change to the new directory--for example:
```sh
mkdir ./PROJECT_DIRECTORY && cd $_
```
2. Use the Python `venv` module to create a virtual environment--for example:
```sh
python -m venv envs/virtualenv-1
```
`venv` creates the new virtual environment directory in your project.
3. To activate the new virtual environment in your terminal, run the `source` command and pass the file path of the virtual environment `activate` script:
```sh
source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activate
```
For example:
```sh
source envs/virtualenv-1/bin/activate
```
<!---------------------------------- End venv --------------------------------->
{{% /tab-content %}}
{{% tab-content %}}
<!-------------------------------- Begin conda -------------------------------->
### Install Anaconda {#conda-install}
1. Follow the [Anaconda installation instructions](https://docs.continuum.io/anaconda/install/) for your system.
2. Check that you can run the `conda` command:
```sh
conda
```
3. Use `conda` to create a virtual environment--for example:
```sh
conda create --prefix envs/virtualenv-1
```
`conda` creates a virtual environment in a directory named `./envs/virtualenv-1`.
4. To activate the new virtual environment, use the `conda activate` command and pass the directory path of the virtual environment:
```sh
conda activate envs/VIRTUAL_ENVIRONMENT_NAME
```
For example:
```sh
conda activate ./envs/virtualenv-1
```
<!--------------------------------- END conda --------------------------------->
{{% /tab-content %}}
{{< /tabs-wrapper >}}
When a virtual environment is activated, the name displays at the beginning of your terminal command line--for example:
{{% code-callout "(virtualenv-1)"%}}
```sh
(virtualenv-1) $ PROJECT_DIRECTORY
```
{{% /code-callout %}}
## Query InfluxDB
1. [Install the influxdb3-python library](#install-the-influxdb3-python-library)
2. [Create an InfluxDB client](#create-an-influxdb-client)
3. [Execute a query](#execute-a-query)
### Install the influxdb3-python library
The `influxdb_client_3` module provides a simple and convenient way to interact
with {{< cloud-name >}} using Python. This module supports both writing data to
InfluxDB and querying data using SQL or InfluxQL queries.
{{% note %}}
_To query data with **InfluxQL** and Python, see
[Use InfluxQL with Python](/influxdb/cloud-dedicated/query-data/influxql/execute-queries/python/)._
{{% /note %}}
Installing `inflxudb3-python` also installs the
[`pyarrow`](https://arrow.apache.org/docs/python/index.html) library that you'll
use for working with Arrow data returned from queries.
In your terminal, use `pip` to install `influxdb3-python`:
```sh
pip install influxdb3-python
```
With `influxdb3-python` and `pyarrow` installed, you're ready to query and
analyze data stored in an InfluxDB database.
### Create an InfluxDB client
The following example shows how to use Python with `influxdb3-python`
and to instantiate a client configured for an InfluxDB database.
In your editor, copy and paste the following sample code to a new file--for
example, `query-example.py`:
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```py
# query-example.py
from influxdb_client_3 import InfluxDBClient3
# Instantiate an InfluxDBClient3 client configured for your database
client = InfluxDBClient3(
host='b0c7cce5-8dbc-428e-98c6-7f996fb96467.a.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
```
{{% /code-placeholders %}}
Replace the following configuration values:
- {{% code-placeholder-key %}}`DATABASE_TOKEN`{{% /code-placeholder-key %}}:
Your InfluxDB token with read permissions on the databases you want to query.
- {{% code-placeholder-key %}}`DATABASE_NAME`{{% /code-placeholder-key %}}:
The name of your InfluxDB database.
### Execute a query
To execute an SQL query, call the client's `query(query,language)` method and
specify the following arguments:
- **query**: SQL query string to execute.
- **language**: `sql`
#### Syntax {#execute-query-syntax}
```py
query(query: str, language: str)
```
#### Example {#execute-query-example}
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
```py
# query-example.py
from influxdb_client_3 import InfluxDBClient3
client = InfluxDBClient3(
host='cluster-id.influxdb.io',
token='DATABASE_TOKEN',
database='DATABASE_NAME'
)
# Execute the query and return an Arrow table
table = client.query(
query="SELECT * FROM home",
language="sql"
)
# Return query results as a markdown table
print(table.to_pandas().to_markdown())
```
{{% /code-placeholders %}}
Next, learn how to use Python tools to work with time series data:
- [Use PyArrow](/influxdb/cloud-dedicated/query-data/tools/pyarrow/)
- [Use pandas](/influxdb/cloud-dedicated/query-data/tools/pandas/)

View File

@ -4,13 +4,15 @@ seotitle: Use Apache Superset to query data stored in InfluxDB Cloud Dedicated
description: >
Install and run [Apache Superset](https://superset.apache.org/)
to query data stored in InfluxDB.
weight: 101
weight: 401
menu:
influxdb_cloud_dedicated:
parent: Query with Flight SQL
parent: sql-execute-queries
name: Use Superset
identifier: query_with_superset
identifier: query-with-superset
influxdb/cloud-dedicated/tags: [query, flightsql, superset]
aliases:
- /influxdb/cloud-dedicated/query-data/execute-queries/flight-sql/superset/
related:
- /influxdb/cloud-dedicated/visualize-data/superset/
---

View File

@ -0,0 +1 @@
<span class="code-placeholder-key">{{.Inner}}</span>