Documentation

使用Python查询数据

使用 InfluxDB influxdb_client_3 Python 客户端库模块和 SQL 或 InfluxQL 查询存储在 InfluxDB 中的数据。通过 Flight+gRPC 协议执行查询并检索数据,然后使用常见的 Python 工具处理数据。

开始使用Python查询InfluxDB

本指南假设以下前提条件:

要学习如何设置InfluxDB和写入数据,请参阅Get Started教程中的设置说明

创建一个Python虚拟环境

本指南遵循使用 Python 虚拟环境 的推荐实践。如果您不想使用虚拟环境并且已安装 Python,请继续阅读 查询 InfluxDB。Python 虚拟环境 使您的项目的 Python 解释器和依赖项自包含并与其他项目隔离。

要安装Python并创建一个虚拟环境,请选择以下选项之一:

  • Python venv: 自版本 3.5 起,venv 模块 在 Python 中是标准配置。

  • Anaconda® Distribution: 一个提供Python和conda包和环境管理器的Python/R数据科学发行版。

    安装 Python

    1. 请遵循 Python 安装说明 来为您的系统安装最新版本的 Python 编程语言。

    2. 检查您是否可以运行 pythonpip 命令。 pip 是包含在大多数 Python 发行版中的一个包管理器。

      在您的终端中,输入以下命令:

      python --version
      
      pip --version
      

      根据您的系统,您可能需要使用特定版本的命令–例如。

      python3 --version
      
      pip3 --version
      

      如果 pippip 都不起作用,请按照适合您系统的 Pypa.io Pip 安装 方法进行操作。

    创建项目虚拟环境

    1. 为你的Python项目创建一个目录并切换到新目录—例如:

      mkdir ./PROJECT_DIRECTORY && cd $_
      
    2. 使用 Python venv 模块创建虚拟环境——例如:

      python -m venv envs/virtualenv-1
      

      venv 在您的项目中创建新的虚拟环境目录。

    3. 要在终端中激活新的虚拟环境,请运行source命令并传递虚拟环境activate脚本的路径:

      source envs/VIRTUAL_ENVIRONMENT_NAME/bin/activate
      

      例如:

      source envs/virtualenv-1/bin/activate
      

    安装 Anaconda

    1. 请遵循Anaconda安装说明,适用于您的系统。

    2. 检查您是否可以运行 conda 命令:

      conda
      
    3. 使用 conda 创建虚拟环境——例如:

      conda create --prefix envs/virtualenv-1 
      

      conda 在名为 ./envs/virtualenv-1 的目录中创建一个虚拟环境。

    4. 要激活新的虚拟环境,请使用 conda activate 命令并传递虚拟环境的目录路径:

      conda activate envs/VIRTUAL_ENVIRONMENT_NAME
      

      例如:

      conda activate ./envs/virtualenv-1
      

当虚拟环境被激活时,名称会显示在你的终端命令行的开头–例如:

(virtualenv-1) $ PROJECT_DIRECTORY

查询 InfluxDB

  1. 安装 influxdb3-python 库
  2. 创建一个InfluxDB客户端
  3. 执行查询

安装 influxdb3-python 库

influxdb3-python 包提供了 influxdb_client_3 模块,用于将 InfluxDB Clustered 与您的 Python 代码集成。该模块支持将数据写入 InfluxDB 并使用 SQL 或 InfluxQL 查询数据。

安装以下依赖:

* 已经安装在写入数据部分

  • influxdb3-python *: 提供了influxdb_client_3模块,并且还安装了pyarrow,以便处理从查询返回的Arrow数据。
  • pandas: 提供pandas模块用于分析和处理数据。
  • tabulate: 提供了tabulate函数用于格式化表格数据。

在您的终端中输入以下命令:

pip install influxdb3-python pandas tabulate

安装了 influxdb3-pythonpyarrow 后,您可以查询和分析存储在 InfluxDB 数据库中的数据。

创建一个 InfluxDB 客户端

以下示例展示了如何使用 Python 与 influxdb_client_3 模块来实例化一个配置为 InfluxDB 集群数据库的客户端。

在您的编辑器中,将以下示例代码复制并粘贴到一个新文件中,例如, 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
'
)

重要: 如果使用 Windows,请指定 Windows 证书路径

替换以下配置值:

  • database: 要查询的InfluxDB 集群数据库的名称
  • token: 一个数据库令牌,具有对指定数据库的读取访问权限。 将其存储在秘密存储或环境变量中,以避免暴露原始令牌字符串。

执行查询

要执行查询,请调用以下客户端方法:

query(query,language) 方法

并指定以下参数:

  • query: 一个字符串。要执行的SQL或InfluxQL查询。
  • 语言: 一个字符串 ("sql""influxql")。query 语言。

示例

以下示例展示了如何使用 SQL 或 InfluxQL 选择测量中的所有字段,然后将结果格式化为 Markdown 表格。

# 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')]))

替换以下配置值:

  • database: 要查询的 InfluxDB 集群数据库 的名称
  • token: 一个 数据库令牌,具有对指定数据库的读取访问权限。 将此存储在秘密存储或环境变量中,以避免暴露原始令牌字符串。

接下来,学习如何使用Python工具处理时间序列数据:



Flux的未来

Flux 正在进入维护模式。您可以像现在一样继续使用它,而无需对您的代码进行任何更改。

阅读更多

InfluxDB 3 开源版本现已公开Alpha测试

InfluxDB 3 Open Source is now available for alpha testing, licensed under MIT or Apache 2 licensing.

我们将发布两个产品作为测试版的一部分。

InfluxDB 3 核心,是我们新的开源产品。 它是一个用于时间序列和事件数据的实时数据引擎。 InfluxDB 3 企业版是建立在核心基础之上的商业版本,增加了历史查询能力、读取副本、高可用性、可扩展性和细粒度安全性。

有关如何开始的更多信息,请查看: