Trino Python Client
The `trino` Python client provides a client interface to query Trino, a distributed SQL engine for interactive and batch big data processing. It offers a low-level client, a DBAPI 2.0 implementation, and a SQLAlchemy adapter. The current version is 0.337.0 and releases are frequent, often several per month, incorporating fixes, features, and dependency updates.
Warnings
- deprecated Python 3.8 support has been dropped as of version 0.331.0. Users on Python 3.8 or older must upgrade their Python environment to a supported version (Python >= 3.9).
- gotcha The client migrated from the built-in `json` module to `orjson` in version 0.336.0 for performance. While largely compatible, applications with strict dependencies on specific `json` module behaviors or those not installing `orjson` as an optional dependency might see subtle behavioral differences or reduced performance if `orjson` isn't available.
- gotcha When using the client's spooling protocol with compression (e.g., `json+lz4`, `json+zstd`), the corresponding compression libraries (`lz4`, `zstandard`) must be installed. As of 0.337.0, the client gracefully handles their absence by disabling compression, which can lead to unexpected performance or larger data transfer if compression was intended.
- gotcha The default `Cursor.arraysize` is 1, meaning `fetchmany()` will only return one row by default. This can be inefficient for fetching multiple rows in a loop.
- gotcha When executing multiple SQL statements in a single `cursor.execute()` call, ensure that individual statements are *not* terminated with a semicolon ';'. The Python client often 'freaks out' or misinterprets queries with trailing semicolons.
- gotcha Version 0.334.0 introduced the ability to 'Allow authentication over insecure channel'. While this can be useful for specific test environments, using it in production without proper TLS (i.e., `http_scheme='https'` and certificate verification) is a significant security risk.
Install
-
pip install trino -
pip install trino[gssapi] -
pip install trino[full]
Imports
- connect
from trino.dbapi import connect
- trino
import trino
- BasicAuthentication
from trino.auth import BasicAuthentication
Quickstart
import os
from trino.dbapi import connect
from trino.auth import BasicAuthentication
TRINO_HOST = os.environ.get('TRINO_HOST', 'localhost')
TRINO_PORT = int(os.environ.get('TRINO_PORT', '8080'))
TRINO_USER = os.environ.get('TRINO_USER', 'your_user')
TRINO_CATALOG = os.environ.get('TRINO_CATALOG', 'system')
TRINO_SCHEMA = os.environ.get('TRINO_SCHEMA', 'runtime')
TRINO_PASSWORD = os.environ.get('TRINO_PASSWORD') # Optional, for BasicAuth
TRINO_HTTP_SCHEME = os.environ.get('TRINO_HTTP_SCHEME', 'http')
auth = None
if TRINO_PASSWORD:
auth = BasicAuthentication(TRINO_USER, TRINO_PASSWORD)
try:
with connect(
host=TRINO_HOST,
port=TRINO_PORT,
user=TRINO_USER,
catalog=TRINO_CATALOG,
schema=TRINO_SCHEMA,
http_scheme=TRINO_HTTP_SCHEME,
auth=auth # Pass auth if not None
) as conn:
with conn.cursor() as cur:
cur.execute('SELECT node_id, state FROM system.runtime.nodes')
rows = cur.fetchall()
for row in rows:
print(row)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure a Trino server is running and accessible at the specified host and port.")
print("For secure connections, ensure 'TRINO_HTTP_SCHEME' is 'https' and provide authentication details.")