Python Client Library
taospy
is the official Python connector for TDengine. taospy
provides a rich set of APIs that allow Python applications to easily use TDengine.
The source code for the Python connector is hosted on GitHub.
Connection Methods
taospy
mainly provides three forms of connectors. Generally, we recommend using WebSocket connection.
- Native connection, corresponding to the
taos
module of thetaospy
package. It connects to the TDengine instance through the TDengine client driver (taosc), supporting data writing, querying, data subscription, schemaless interfaces, and parameter binding interfaces, among other functions. - REST connection, corresponding to the
taosrest
module of thetaospy
package. It connects to the TDengine instance via the HTTP interface provided by taosAdapter and does not support features like schemaless and data subscriptions. - WebSocket connection, corresponding to the
taos-ws-py
package, which can be installed optionally. It connects to the TDengine instance via the WebSocket interface provided by taosAdapter, with a few functional differences compared to the native connection.
For a detailed introduction to the connection methods, please refer to: Connection Methods
In addition to wrapping the native and REST interfaces, taospy
also provides a programming interface that conforms to the Python Database API Specification (PEP 249). This makes taospy
easy to integrate with many third-party tools, such as SQLAlchemy and pandas.
The method of establishing a connection directly with the server using the native interface provided by the client driver is referred to as "native connection" in the following text; while using the REST or WebSocket interface provided by taosAdapter to establish a connection with the server is referred to as "REST connection" or "WebSocket connection."
Supported Platforms
- The platforms supported by the native connection are consistent with the platforms supported by the TDengine client.
- The REST connection supports all platforms that can run Python.
Supported Features
- The native connection supports all core functionalities of TDengine, including: connection management, executing SQL, parameter binding, subscriptions, and schemaless writing.
- The features supported by the REST connection include: connection management and executing SQL. (By executing SQL, it can: manage databases, manage tables and supertables, write data, query data, create continuous queries, etc.)
Version History
Regardless of which version of TDengine is used, it is recommended to use the latest version of taospy
.
Python Connector Version | Major Changes |
---|---|
2.7.15 | Added support for VARBINARY and GEOMETRY types |
2.7.14 | Fixed known issues |
2.7.13 | Added tmq synchronous commit offset interface |
2.7.12 | 1. Added support for varbinary type (STMT does not support varbinary) 2. Improved query performance (thanks to contributor hadrianl) |
2.7.9 | Data subscription supports fetching consumption progress and resetting consumption progress |
2.7.8 | Added execute_many |
Python WebSocket Connector Version | Major Changes |
---|---|
0.3.2 | Optimized WebSocket SQL query and insert performance, modified README and documentation, fixed known issues |
0.2.9 | Fixed known issues |
0.2.5 | 1. Data subscription supports fetching consumption progress and resetting consumption progress 2. Supports schemaless 3. Supports STMT |
0.2.4 | Added unsubscribe method for data subscription |
Handling Exceptions
The Python connector may produce four types of exceptions:
- Exceptions specific to the Python connector
- Exceptions from the native connection method
- Exceptions from the WebSocket connection method
- Exceptions related to data subscription
- Errors from other functionality modules of TDengine, please refer to Error Codes
Error Type | Description | Suggested Actions |
---|---|---|
InterfaceError | The version of taosc is too low and does not support the interface used | Please check the TDengine client version |
ConnectionError | Database connection error | Please check the TDengine server status and connection parameters |
DatabaseError | Database error | Please check the TDengine server version and upgrade the Python connector to the latest version |
OperationalError | Operational error | API usage error, please check the code |
ProgrammingError | Interface call error | Please check if the submitted data is correct |
StatementError | stmt related exceptions | Please check if the bound parameters match the SQL |
ResultError | Data operation error | Please check if the data being operated matches the data type in the database |
SchemalessError | Schemaless related exceptions | Please check if the data format and corresponding protocol type are correct |
TmqError | tmq related exceptions | Please check if the Topic and consumer configurations are correct |
In Python, exceptions are usually handled using try-except. For more information on exception handling, please refer to the Python Errors and Exceptions documentation.
For errors from other functionality modules of TDengine, please refer to Error Codes.
All database operations of the Python Connector will directly raise exceptions if an error occurs, and it is the responsibility of the application to handle these exceptions. For example:
import taos
try:
conn = taos.connect()
conn.execute("CREATE TABLE 123") # wrong sql
except taos.Error as e:
print(e)
print("exception class: ", e.__class__.__name__)
print("error number:", e.errno)
print("error message:", e.msg)
except BaseException as other:
print("exception occur")
print(other)
# output:
# [0x0216]: syntax error near 'Incomplete SQL statement'
# exception class: ProgrammingError
# error number: -2147483114
# error message: syntax error near 'Incomplete SQL statement'
Data Type Mapping
TDengine currently supports timestamp, numeric, character, and boolean types. The corresponding type conversion to Python is as follows:
TDengine DataType | Python DataType |
---|---|
TIMESTAMP | datetime |
INT | int |
BIGINT | int |
FLOAT | float |
DOUBLE | int |
SMALLINT | int |
TINYINT | int |
BOOL | bool |
BINARY | str |
NCHAR | str |
JSON | str |
GEOMETRY | bytearray |
VARBINARY | bytearray |
Example Program Summary
Example Program Link | Example Program Content |
---|---|
bind_multi.py | Parameter binding, binding multiple rows at once |
bind_row.py | Parameter binding, binding one row at a time |
insert_lines.py | InfluxDB line protocol writing |
json_tag.py | Using JSON type tags |
tmq_consumer.py | TMQ subscription |
native_all_type_query.py | Example supporting all types |
native_all_type_stmt.py | Example for parameter binding supporting all types |
For source code of example programs, please refer to:
About Nanoseconds (nanosecond)
Due to the current inadequacy of Python's support for nanoseconds (see links below), the current implementation returns integers when the precision is nanoseconds, rather than the datetime type returned for milliseconds and microseconds. Application developers need to handle this themselves and are advised to use pandas
's to_datetime()
. If Python officially supports nanoseconds in the future, the Python connector may modify the relevant interfaces. For more information, see https://www.python.org/dev/peps/pep-0564/.