Pinot DB-API and SQLAlchemy Dialect
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
- breaking Python 3.9 support was dropped in `pinotdb` version 7.0.0.
- breaking Python 3.8 support was dropped in `pinotdb` version 6.0.0.
- breaking `pinotdb` version 8.0.0 introduced compatibility with SQLAlchemy 2.0. If you are using SQLAlchemy 1.x, you may need to adjust your code or pin to an older `pinotdb` version.
- gotcha Versions prior to 9.1.1 had a potential issue with clear-text logging of sensitive information.
- gotcha `pinotdb` versions >= 0.3.2 use the Pinot SQL API and require Apache Pinot server version >= 0.3.0. Older `pinotdb` versions (0.2.x) use the deprecated PQL API.
Install
-
pip install pinotdb==9.1.1
Imports
- connect
from pinotdb import connect
- create_engine
from sqlalchemy.engine import create_engine
Quickstart
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}")