PyAthenaJDBC: Amazon Athena JDBC driver wrapper for Python DB API 2.0

3.0.1 · active · verified Wed Apr 15

PyAthenaJDBC is a Python DB API 2.0 (PEP 249) compliant wrapper for Amazon Athena, utilizing the official JDBC driver via JPype. It provides a way to interact with Athena from Python using standard database connection patterns. The library is currently at version 3.0.1 and frequently updates to support the latest Athena JDBC driver and port features from its pure Python counterpart, PyAthena.

Warnings

Install

Imports

Quickstart

Connects to Amazon Athena using default AWS credentials and executes a simple query. The `S3OutputLocation` is mandatory for Athena query results and should be configured. Credentials can be passed explicitly but are often resolved automatically by the `DefaultAWSCredentialsProviderChain`.

import os
from pyathenajdbc import connect

# Ensure AWS credentials (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# and region (AWS_REGION) are set via environment variables or boto3 config.

# S3OutputLocation is required for Athena query results.
# It's recommended to set it via an environment variable.
# Example: export AWS_ATHENA_S3_OUTPUT_LOCATION='s3://your-athena-query-results-bucket/'
s3_output_location = os.environ.get('AWS_ATHENA_S3_OUTPUT_LOCATION', '')

if not s3_output_location:
    raise ValueError("AWS_ATHENA_S3_OUTPUT_LOCATION environment variable must be set.")

try:
    conn = connect(
        AwsRegion=os.environ.get('AWS_REGION', 'us-east-1'),
        Schema='default', # Your Athena database name
        S3OutputLocation=s3_output_location,
        # User=os.environ.get('AWS_ACCESS_KEY_ID'), # Optional if using default credential chain
        # Password=os.environ.get('AWS_SECRET_ACCESS_KEY') # Optional if using default credential chain
    )

    with conn.cursor() as cursor:
        cursor.execute("SELECT 1 as one_value")
        row = cursor.fetchone()
        print(f"Result from SELECT 1: {row}")

        cursor.execute("SHOW TABLES")
        tables = cursor.fetchall()
        print(f"Tables in 'default' schema: {tables}")

finally:
    if 'conn' in locals() and conn:
        conn.close()

view raw JSON →