Pinot DB-API and SQLAlchemy Dialect

9.1.1 · active · verified Wed Apr 01

pinotdb is a Python library providing a DB-API 2.0 interface and a SQLAlchemy dialect for Apache Pinot. It enables Python applications to connect to and query Pinot clusters using standard SQL. The library is actively maintained with frequent releases, currently at version 9.1.1, and requires Apache Pinot server version 0.3.0 or later for its SQL API functionality.

Warnings

Install

Imports

Quickstart

This example demonstrates how to connect to an Apache Pinot broker using the DB-API 2.0 interface and execute a simple SQL query. It fetches the count of records from a hypothetical 'baseballStats' table and prints the results and query statistics. Authentication details are optional and can be provided via environment variables.

import os
from pinotdb import connect

# Configure connection details (replace with your Pinot broker host and port)
PINOT_HOST = os.environ.get('PINOT_BROKER_HOST', 'localhost')
PINOT_PORT = int(os.environ.get('PINOT_BROKER_PORT', '8000')) # Default Pinot QuickStart broker port
PINOT_PATH = os.environ.get('PINOT_QUERY_PATH', '/query/sql')
PINOT_SCHEME = os.environ.get('PINOT_SCHEME', 'http')
PINOT_USERNAME = os.environ.get('PINOT_USERNAME', '')
PINOT_PASSWORD = os.environ.get('PINOT_PASSWORD', '')

try:
    if PINOT_USERNAME and PINOT_PASSWORD:
        conn = connect(
            host=PINOT_HOST, 
            port=PINOT_PORT, 
            path=PINOT_PATH, 
            scheme=PINOT_SCHEME,
            username=PINOT_USERNAME,
            password=PINOT_PASSWORD
        )
    else:
        conn = connect(host=PINOT_HOST, port=PINOT_PORT, path=PINOT_PATH, scheme=PINOT_SCHEME)

    curs = conn.cursor()
    curs.execute("""
        SELECT count(*) FROM baseballStats LIMIT 10
    """)

    print("Query Results:")
    for row in curs:
        print(row)

    print(f"Time taken: {curs.timeUsedMs} ms")
    print(f"Query Stats: {curs.query_stats}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure a Pinot broker is running and accessible at {PINOT_SCHEME}://{PINOT_HOST}:{PINOT_PORT}")

view raw JSON →