文档

使用 PyArrow 库分析数据

使用 PyArrow 读取和分析 InfluxDB 集群查询结果。PyArrow 库提供了高效的计算、聚合、序列化和转换 Arrow 格式数据的工具。

Apache Arrow 是一个内存分析开发平台。它包含一系列技术,使大数据系统能够快速存储、处理和移动数据。

Arrow Python 绑定(也称为“PyArrow”)与 NumPy、pandas 和内置 Python 对象具有一流集成。它们基于 Arrow 的 C++ 实现。

安装先决条件

本指南中的示例假设使用 Python 虚拟环境和 InfluxDB v3 influxdb3-python Python 客户端库。有关更多信息,请参阅如何使用 Python 查询 InfluxDB 入门

安装 influxdb3-python 还会安装提供 Apache Arrow Python 绑定的 pyarrow 库。

使用 PyArrow 读取查询结果

以下示例展示了如何使用 influxdb3-pythonpyarrow 查询 InfluxDB 并以 PyArrow Table 的形式查看 Arrow 数据。

  1. 在您的编辑器中,将以下示例代码复制并粘贴到一个新文件中——例如,pyarrow-example.py

    # pyarrow-example.py
    
    from influxdb_client_3 import InfluxDBClient3
    import pandas
    
    def querySQL():
      
      # Instantiate an InfluxDB client configured for a database
      client = InfluxDBClient3(
        "https://cluster-host.com",
        database="
    DATABASE_NAME
    "
    ,
    token="
    DATABASE_TOKEN
    "
    )
    # Execute the query to retrieve all record batches in the stream formatted as a PyArrow Table. table = client.query( '''SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' ) client.close() print(querySQL())
  2. 替换以下配置值

  3. 在您的终端中,使用Python解释器运行该文件

    python pyarrow-example.py
    

InfluxDBClient3.query()方法发送查询请求,然后返回一个包含响应流中所有Arrow记录批次的pyarrow.Table

接下来,使用PyArrow分析数据

使用 PyArrow 分析数据

分组和聚合数据

使用pyarrow.Table,您可以使用列中的值作为进行分组。

以下示例展示了如何查询InfluxDB,然后使用PyArrow对表数据进行分组并计算每个组的汇总值

# pyarrow-example.py

from influxdb_client_3 import InfluxDBClient3
import pandas

def querySQL():
  
  # Instantiate an InfluxDB client configured for a database
  client = InfluxDBClient3(
    "https://cluster-host.com",
    database="
DATABASE_NAME
"
,
token="
DATABASE_TOKEN
"
)
# Execute the query to retrieve data # formatted as a PyArrow Table table = client.query( '''SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' ) client.close() return table table = querySQL() # Use PyArrow to aggregate data print(table.group_by('room').aggregate([('temp', 'mean')]))

替换以下

查看示例结果

有关更多详细信息示例,请参阅PyArrow文档Apache Arrow Python食谱


这个页面有帮助吗?

感谢您的反馈!


Flux的未来

Flux将进入维护模式。您可以在不更改代码的情况下继续像现在一样使用它。

阅读更多

InfluxDB v3增强功能和InfluxDB Clustered现已正式发布

新功能包括更快的查询性能和管理工具,推进了InfluxDB v3产品线。InfluxDB Clustered现已正式发布。

InfluxDB v3性能和功能

InfluxDB v3产品线在查询性能方面取得了显著提升,并提供了新的管理工具。这些增强功能包括用于监控InfluxDB集群健康状况的操作仪表板,InfluxDB Cloud Dedicated中的单点登录(SSO)支持,以及针对令牌和数据库的新管理API。

了解新的v3增强功能


InfluxDB Clustered正式发布

InfluxDB Clustered现已正式发布,为您在自管理的堆栈中提供了InfluxDB v3的功能。

与我们谈谈InfluxDB Clustered