TDengine/docs/zh/05-programming/06-client-libraries/01-python.md

99 lines
5.2 KiB
Markdown
Raw Permalink Normal View History

2023-03-10 04:07:10 +00:00
---
sidebar_label: Python
title: TDengine Python 连接器
2025-12-04 10:25:02 +00:00
description: "taospy 是 TDengine 的官方 Python 连接器。taospy 提供了丰富的 API使得 Python 应用可以很方便地使用 TDengine。tasopy 对 TDengine 的原生接口和 REST 接口都进行了封装,分别对应 tasopy 的两个子模块taos 和 taosrest。除了对原生接口和 REST 接口的封装taospy 还提供了符合 Python 数据访问规范 (PEP 249) 的编程接口。这使得 taospy 和很多第三方工具集成变得简单,比如 SQLAlchemy 和 pandas"
2023-03-10 04:07:10 +00:00
---
2025-12-04 10:25:02 +00:00
`taospy` 是 TDengine 的官方 Python 连接器。`taospy` 提供了丰富的 API使得 Python 应用可以很方便地使用 TDengine。
`taospy` 对 REST 接口进行了封装,还提供了符合 [Python 数据访问规范 (PEP 249)](https://peps.python.org/pep-0249/) 的编程接口。这使得 `taospy` 和很多第三方工具集成变得简单,比如 [SQLAlchemy](https://www.sqlalchemy.org/) 和 [pandas](https://pandas.pydata.org/)。
2023-03-10 04:07:10 +00:00
Python 连接器的源码托管在 [GitHub](https://github.com/taosdata/taos-connector-python)。
2024-11-19 09:36:20 +00:00
## 连接方式
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
`taospy`主要提供三种形式的连接器。TDengine Cloud 支持 REST 连接和 WebSocket 连接两种方式
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
* 原生连接,对应 taospy 包的 taos 模块。通过 TDengine 客户端驱动程序taosc原生连接 TDengine 实例支持数据写入、查询、数据订阅、schemaless 接口和参数绑定接口等功能。
* REST 连接,对应 taospy 包的 taosrest 模块。通过 taosAdapter 提供的 HTTP 接口连接 TDengine 实例,不支持 schemaless 和数据订阅等特性。
* WebSocket 连接,对应 taos-ws-py 包,可以选装。通过 taosAdapter 提供的 WebSocket 接口连接 TDengine 实例WebSocket 连接实现的功能集合和原生连接有少量不同。
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
:::note IMPORTANT
2023-03-10 04:07:10 +00:00
2025-12-04 10:25:02 +00:00
1. 使用客户端驱动提供的原生接口直接与服务端建立的连接,下文中称为“原生连接”.
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
2. 使用 taosAdapter 提供的 REST 接口或 WebSocket 接口与服务端建立的连接的方式下文中称为“REST 连接”或“WebSocket 连接”。
:::
2023-03-10 04:07:10 +00:00
2025-12-04 10:25:02 +00:00
关于如何建立连接的详细介绍请参考:[开发指南 - 建立连接-Python](../01-connect/01-python.md)
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
## 示例程序
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
下面以智能电表为例,展示如何使用 Python 连接器在名为 power 的数据库中,创建一个名为 meters 的超级表STABLE插入并查询数据。meters 表结构包含时间戳、电流、电压、相位等列,以及分组 ID 和位置作为标签。
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
:::note IMPORTANT
2025-12-04 10:25:02 +00:00
2024-11-19 09:36:20 +00:00
1. 在执行下面样例代码的之前,您必须先在 [TDengine Cloud - 数据浏览器](https://cloud.taosdata.com/explorer) 页面创建一个名为 power 的数据库
2025-12-04 10:25:02 +00:00
2. 如何在代码中建立和 TDengine Cloud 的连接,请参考 [开发指南 - 建立连接](../../connect/)。
2024-11-19 09:36:20 +00:00
:::
2023-03-10 04:07:10 +00:00
### 使用 TaosRestConnection 类
```python
{{#include docs/examples/python/reference_connection.py:example}}
```
### 使用 TaosRestCursor 类
`TaosRestCursor` 类是对 PEP249 Cursor 接口的实现。
```python
{{#include docs/examples/python/reference_cursor.py:basic}}
```
2025-12-04 10:25:02 +00:00
* `cursor.execute` :用来执行任意 SQL 语句。
* `cursor.rowcount`:对于写入操作返回写入成功记录数。对于查询操作,返回结果集行数。
* `cursor.description` :返回字段的描述信息。关于描述信息的具体格式请参考[TaosRestCursor](https://docs.taosdata.com/api/taospy/taosrest/cursor.html)。
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
#### 使用 RestClient 类
2023-03-10 04:07:10 +00:00
2025-12-04 10:25:02 +00:00
`RestClient` 类是对于 [REST API](../rest-api) 的直接封装。它只包含一个 `sql()` 方法用于执行任意 SQL 语句,并返回执行结果。
2023-03-10 04:07:10 +00:00
```python
{{#include docs/examples/python/reference_rest_client.py}}
```
2025-12-04 10:25:02 +00:00
对于 `sql()` 方法更详细的介绍,请参考 [RestClient](https://docs.taosdata.com/api/taospy/taosrest/restclient.html)。
2023-03-10 04:07:10 +00:00
## 其它说明
### 异常处理
所有数据库操作如果出现异常,都会直接抛出来。由应用程序负责异常处理。比如:
```python
{{#include docs/examples/python/handle_exception.py}}
```
### 关于纳秒 (nanosecond)
2025-12-04 10:25:02 +00:00
由于目前 Python 对 nanosecond 支持的不完善 (见下面的链接),目前的实现方式是在 nanosecond 精度时返回整数,而不是 ms 和 us 返回的 datetime 类型,应用开发者需要自行处理,建议使用 pandas 的 to_datetime()。未来如果 Python 正式完整支持了纳秒Python 连接器可能会修改相关接口。
2023-03-10 04:07:10 +00:00
2025-12-04 10:25:02 +00:00
1. [parsing-datetime-strings-containing-nanoseconds](https://stackoverflow.com/questions/10611328/parsing-datetime-strings-containing-nanoseconds)
2. [PEP 564 -- Support for the Python nanosecond timestamp](https://www.python.org/dev/peps/pep-0564/)
2023-03-10 04:07:10 +00:00
## 重要更新
[**Release Notes**](https://github.com/taosdata/taos-connector-python/releases)
## API 参考
2025-12-04 10:25:02 +00:00
* [连接器-Python-Api 参考](https://docs.taosdata.com/reference/connector/python/#api-参考)
* [taos](https://docs.taosdata.com/api/taospy/taos/)
* [taosrest](https://docs.taosdata.com/api/taospy/taosrest)
2023-03-10 04:07:10 +00:00
2024-11-19 09:36:20 +00:00
更多关于 Python 连接器的详细介绍请参考[连接器-Python](https://docs.taosdata.com/reference/connector/python)
2023-03-10 04:07:10 +00:00
## 常见问题
欢迎[提问或报告问题](https://github.com/taosdata/taos-connector-python/issues)。