Trino Python Client

0.337.0 · active · verified Mon Apr 06

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

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Trino server using the DBAPI interface, execute a simple query, and fetch results. It uses environment variables for connection parameters for flexibility and basic authentication if a password is provided. The `with` statement ensures proper resource management for both connection and cursor.

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.")

view raw JSON →