Presto Python Client

0.8.4 · active · verified Thu Apr 09

Client for the Presto distributed SQL Engine. It provides both a low-level client and a DBAPI 2.0 implementation. As of version 0.8.4, it officially supports Python 2.7, 3.5, 3.6, 3.7, and pypy, offering standard database connectivity for querying Presto clusters.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Presto server using the DBAPI interface, execute a simple query, and fetch the results. It includes optional basic authentication and uses environment variables for configuration, making it suitable for secure execution in various environments.

import os
from prestodb.dbapi import connect
from prestodb.auth import BasicAuthentication

host = os.environ.get('PRESTO_HOST', 'localhost')
port = int(os.environ.get('PRESTO_PORT', 8080))
user = os.environ.get('PRESTO_USER', 'the-user')
password = os.environ.get('PRESTO_PASSWORD', '') # Only if basic auth is needed
catalog = os.environ.get('PRESTO_CATALOG', 'the-catalog')
schema = os.environ.get('PRESTO_SCHEMA', 'the-schema')

auth = None
if password:
    auth = BasicAuthentication(user, password)

try:
    conn = connect(
        host=host,
        port=port,
        user=user,
        catalog=catalog,
        schema=schema,
        http_scheme=os.environ.get('PRESTO_HTTP_SCHEME', 'http'),
        auth=auth
    )
    cur = conn.cursor()

    cur.execute('SELECT * FROM system.runtime.nodes')
    rows = cur.fetchall()

    print("Successfully connected to Presto and fetched data.")
    for row in rows:
        print(row)

    cur.close()
    conn.close()
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →