Documentation

Use Python to query data

Limited availability

InfluxDB Clustered is currently only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Use the InfluxDB influxdb_client_3 Python client library module and SQL or InfluxQL to query data stored in InfluxDB. Execute queries and retrieve data over the Flight+gRPC protocol, and then process data using common Python tools.

Get started using Python to query InfluxDB

This guide assumes the following prerequisites:

To learn how to set up InfluxDB and write data, see the Setup instructions in the Get Started tutorial.

Create a Python virtual environment

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. 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 venv module 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.

    Install Python

    1. Follow the Python installation instructions 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:

      python --version
      
      pip --version
      

      Depending on your system, you may need to use version-specific commands–for example.

      python3 --version
      
      pip3 --version
      

      If neither pip nor pip<PYTHON_VERSION> works, follow one of the Pypa.io Pip installation methods for your system.

    Create a project virtual environment

    1. Create a directory for your Python project and change to the new directory–for example:

      mkdir ./PROJECT_DIRECTORY && cd $_
      
    2. Use the Python venv module to create a virtual environment–for example:

      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 path of the virtual environment activate script:

      source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activate
      

      For example:

      source envs/virtualenv-1/bin/activate
      

    Install Anaconda

    1. Follow the Anaconda installation instructions for your system.

    2. Check that you can run the conda command:

      conda
      
    3. Use conda to create a virtual environment–for example:

      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:

      conda activate envs/VIRTUAL_ENVIRONMENT_NAME
      

      For example:

      conda activate ./envs/virtualenv-1
      

When a virtual environment is activated, the name displays at the beginning of your terminal command line–for example:

(virtualenv-1) $ PROJECT_DIRECTORY

Query InfluxDB

  1. Install the influxdb3-python library
  2. Create an InfluxDB client
  3. Execute a query

Install the influxdb3-python library

The influxdb3-python package provides the influxdb_client_3 module for integrating InfluxDB Clustered with your Python code. The module supports writing data to InfluxDB and querying data using SQL or InfluxQL.

Install the following dependencies:

* Already installed in the Write data section

  • influxdb3-python *: Provides the influxdb_client_3 module and also installs the pyarrow package for working with Arrow data returned from queries.
  • pandas: Provides pandas modules for analyzing and manipulating data.
  • tabulate: Provides the tabulate function 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 InfluxDB Clustered database.

In your editor, copy and paste the following sample code to a new file–for example, query-example.py:

# query-example.py

from influxdb_client_3 import InfluxDBClient3

# Instantiate an InfluxDBClient3 client configured for your database
client = InfluxDBClient3(
    host='cluster-host.com',
    token='
DATABASE_TOKEN
'
,
database='
DATABASE_NAME
'
)

Important: If using Windows, specify the Windows certificate path

Replace the following configuration values:

  • database: the name of the InfluxDB Clustered database to query
  • token: a database token with read access to the specified database. Store this in a secret store or environment variable to avoid exposing the raw token string.

Execute a query

To execute a query, call the following client method:

query(query,language) method

and specify the following arguments:

  • query: A string. The SQL or InfluxQL query to execute.
  • language: A string ("sql" or "influxql"). The query language.

Example

The following examples shows how to use SQL or InfluxQL to select all fields in a measurement, and then output the results formatted as a Markdown table.

# query-example.py

from influxdb_client_3 import InfluxDBClient3

client = InfluxDBClient3(
    host='cluster-host.com',
    token='
DATABASE_TOKEN
'
,
database='
DATABASE_NAME
'
) # Execute the query and return an Arrow table table = client.query( query="SELECT * FROM home", language="sql" ) print("\n#### View Schema information\n") print(table.schema) print(table.schema.names) print(table.schema.types) print(table.field('room').type) print(table.schema.field('time').metadata) print("\n#### View column types (timestamp, tag, and field) and data types\n") print(table.schema.field('time').metadata[b'iox::column::type']) print(table.schema.field('room').metadata[b'iox::column::type']) print(table.schema.field('temp').metadata[b'iox::column::type']) print("\n#### Use PyArrow to read the specified columns\n") print(table.column('temp')) print(table.select(['room', 'temp'])) print(table.select(['time', 'room', 'temp'])) print("\n#### Use PyArrow compute functions to aggregate data\n") print(table.group_by('hum').aggregate([])) print(table.group_by('room').aggregate([('temp', 'mean')]))
# query-example.py

from influxdb_client_3 import InfluxDBClient3

client = InfluxDBClient3(
    host='cluster-host.com',
    token='
DATABASE_TOKEN
'
,
database='
DATABASE_NAME
'
) # Execute the query and return an Arrow table table = client.query( query="SELECT * FROM home", language="influxql" ) print("\n#### View Schema information\n") print(table.schema) print(table.schema.names) print(table.schema.types) print(table.field('room').type) print(table.schema.field('time').metadata) print("\n#### View column types (timestamp, tag, and field) and data types\n") print(table.schema.field('time').metadata[b'iox::column::type']) print(table.schema.field('room').metadata[b'iox::column::type']) print(table.schema.field('temp').metadata[b'iox::column::type']) print("\n#### Use PyArrow to read the specified columns\n") print(table.column('temp')) print(table.select(['room', 'temp'])) print(table.select(['time', 'room', 'temp'])) print("\n#### Use PyArrow compute functions to aggregate data\n") print(table.group_by('hum').aggregate([])) print(table.group_by('room').aggregate([('temp', 'mean')]))

Replace the following configuration values:

  • database: the name of the InfluxDB Clustered database to query
  • token: a database token with read access to the specified database. Store this in a secret store or environment variable to avoid exposing the raw token string.

Next, learn how to use Python tools to work with time series data:


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following: