文档

Python Flight 客户端

Apache Arrow Python 绑定 与 Python 脚本和应用程序集成,以查询存储在 InfluxDB 中的数据。

使用 InfluxDB v3 客户端库

我们建议使用 influxdb3-python Python 客户端库 将 InfluxDB v3 与您的 Python 应用程序代码集成。

InfluxDB v3 客户端库 包装 Apache Arrow Flight 客户端,并提供方便的方法来 写入查询 和处理存储在 InfluxDB Clustered 中的数据。客户端库可以使用 SQL 或 InfluxQL 进行查询。

以下示例演示如何使用 pyarrow.flightpandas Python 模块查询和格式化存储在 InfluxDB Clustered 数据库中的数据

# Using pyarrow>=12.0.0 FlightClient
from pyarrow.flight import FlightClient, Ticket, FlightCallOptions 
import json
import pandas
import tabulate

# Downsampling query groups data into 2-hour bins
sql="""
  SELECT DATE_BIN(INTERVAL '2 hours', time) AS time,
    room,
    selector_max(temp, time)['value'] AS 'max temp',
    selector_min(temp, time)['value'] AS 'min temp',
    avg(temp) AS 'average temp'
  FROM home
  GROUP BY
    1,
    room
  ORDER BY room, 1"""
  
flight_ticket = Ticket(json.dumps({
  "namespace_name": "
DATABASE_NAME
"
,
"sql_query": sql, "query_type": "sql" })) token = (b"authorization", bytes(f"Bearer
DATABASE_TOKEN
"
.encode('utf-8')))
options = FlightCallOptions(headers=[token]) client = FlightClient(f"grpc+tls://cluster-host.com:443") reader = client.do_get(flight_ticket, options) arrow_table = reader.read_all() # Use pyarrow and pandas to view and analyze data data_frame = arrow_table.to_pandas() print(data_frame.to_markdown())
# Using pyarrow>=12.0.0 FlightClient
from pyarrow.flight import FlightClient, Ticket, FlightCallOptions 
import json
import pandas
import tabulate

# Downsampling query groups data into 2-hour bins
influxql="""
  SELECT FIRST(temp)
  FROM home 
  WHERE room = 'kitchen'
    AND time >= now() - 100d
    AND time <= now() - 10d
  GROUP BY time(2h)"""
  
flight_ticket = Ticket(json.dumps({
  "namespace_name": "
DATABASE_NAME
"
,
"sql_query": influxql, "query_type": "influxql" })) token = (b"authorization", bytes(f"Bearer
DATABASE_TOKEN
"
.encode('utf-8')))
options = FlightCallOptions(headers=[token]) client = FlightClient(f"grpc+tls://cluster-host.com:443") reader = client.do_get(flight_ticket, options) arrow_table = reader.read_all() # Use pyarrow and pandas to view and analyze data data_frame = arrow_table.to_pandas() print(data_frame.to_markdown())

替换以下

  • DATABASE_NAME:您的 InfluxDB Clustered 数据库
  • DATABASE_TOKEN:具有对指定数据库足够权限的 数据库令牌

这个页面有帮助吗?

感谢您的反馈!


Flux 的未来

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

阅读更多

InfluxDB v3 增强功能及 InfluxDB 集群版现已正式发布

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

InfluxDB v3 性能和功能

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

了解新的 v3 增强功能


InfluxDB 集群版正式发布

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

与我们谈论 InfluxDB 集群版