9.2 KiB
| title | description | weight | menu | influxdb/cloud-dedicated/tags | aliases | related | list_code_example | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Use Python to query data with SQL | Use the `influxdb_client_3` Python module and SQL to query data stored in InfluxDB. | 101 |
|
|
|
|
```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 InfluxDB influxdb_client_3 Python client library module and SQL to query data stored in InfluxDB.
Execute queries and retrieve data over the Flight protocol, and then process data using common Python tools.
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.
Create a Python virtual environment
Python virtual environments 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: The
venvmodule comes standard in Python as of version 3.5. -
Anaconda® Distribution: 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 %}}
Install Python
-
Follow the Python installation instructions to install a recent version of the Python programming language for your system.
-
Check that you can run
pythonandpipcommands.pipis a package manager included in most Python distributions.In your terminal, enter the following commands:
python --versionpip --versionDepending on your system, you may need to use version-specific commands--for example.
python3 --versionpip3 --versionIf neither
pipnorpip<PYTHON_VERSION>works, follow one of the Pypa.io Pip installation methods for your system.
Create a project virtual environment
-
Create a directory for your Python project and change to the new directory--for example:
mkdir ./PROJECT_DIRECTORY && cd $_ -
Use the Python
venvmodule to create a virtual environment--for example:python -m venv envs/virtualenv-1venvcreates the new virtual environment directory in your project. -
To activate the new virtual environment in your terminal, run the
sourcecommand and pass the file path of the virtual environmentactivatescript:source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activateFor example:
source envs/virtualenv-1/bin/activate
{{% /tab-content %}} {{% tab-content %}}
Install Anaconda
-
Follow the Anaconda installation instructions for your system.
-
Check that you can run the
condacommand:conda -
Use
condato create a virtual environment--for example:conda create --prefix envs/virtualenv-1condacreates a virtual environment in a directory named./envs/virtualenv-1. -
To activate the new virtual environment, use the
conda activatecommand and pass the directory path of the virtual environment:conda activate envs/VIRTUAL_ENVIRONMENT_NAMEFor example:
conda activate ./envs/virtualenv-1
{{% /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)"%}}
(virtualenv-1) $ PROJECT_DIRECTORY
{{% /code-callout %}}
Query InfluxDB
Install the influxdb3-python library
The influxdb3-python package provides the influxdb_client_3 module for integrating {{% cloud-name %}} with your Python code.
The module supports writing data to InfluxDB and querying data using SQL or InfluxQL.
{{% note %}} To query data with InfluxQL and Python, see Use InfluxQL with Python. {{% /note %}}
Install the following dependencies:
{{< req type="key" text="Already installed in the Write data section" color="magenta" >}}
influxdb3-python{{< req text="* " color="magenta" >}}: Provides theinfluxdb_client_3module and also installs thepyarrowpackage for working with Arrow data returned from queries.pandas: Provides pandas modules for analyzing and manipulating data.tabulate: Provides thetabulatefunction for formatting tabular data.
Enter the following command in your terminal:
pip install influxdb3-python pandas tabulate
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 the influxdb_client_3
module to instantiate a client configured for an {{% cloud-name %}} bucket.
In your editor, copy and paste the following sample code to a new file--for
example, query-example.py:
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
# query-example.py
from influxdb_client_3 import InfluxDBClient3
import pandas
# 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 {{% cloud-name %}} 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
Example
The following example shows how to use SQL to select all fields in a measurement, and then output the results formatted as a Markdown table.
{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
# 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: