wderose / PyAthena

PyAthena is a Python DB API 2.0 (PEP 249) compliant client for Amazon Athena.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image

image

image

image

image

PyAthena

PyAthena is a Python DB API 2.0 (PEP 249) compliant client for Amazon Athena.

Requirements

  • Python
    • CPython 2,7, 3.5, 3.6, 3.7

Installation

Extra packages:

Package Install command Version
Pandas pip install PyAthena[Pandas] >=0.24.0
SQLAlchemy pip install PyAthena[SQLAlchemy] >=1.0.0, <2.0.0

Usage

Basic usage

Cursor iteration

Query with parameter

Supported DB API paramstyle is only PyFormat. PyFormat only supports named placeholders with old % operator style and parameters specify dictionary format.

if % character is contained in your query, it must be escaped with %% like the following:

SQLAlchemy

Install SQLAlchemy with pip install "SQLAlchemy>=1.0.0, <2.0.0" or pip install PyAthena[SQLAlchemy]. Supported SQLAlchemy is 1.0.0 or higher and less than 2.0.0.

The connection string has the following format:

If you do not specify aws_access_key_id and aws_secret_access_key using instance profile or boto3 configuration file:

NOTE: s3_staging_dir requires quote. If aws_access_key_id, aws_secret_access_key and other parameter contain special characters, quote is also required.

Pandas

Minimal example for Pandas DataFrame:

As Pandas DataFrame:

If you want to use Pandas DataFrame object directly, you can use PandasCursor.

AsynchronousCursor

AsynchronousCursor is a simple implementation using the concurrent.futures package. Python 2.7 uses backport of the concurrent.futures package. This cursor is not DB API 2.0 (PEP 249) compliant.

You can use the AsynchronousCursor by specifying the cursor_class with the connect method or connection object.

It can also be used by specifying the cursor class when calling the connection object's cursor method.

The default number of workers is 5 or cpu number * 5. If you want to change the number of workers you can specify like the following.

The execute method of the AsynchronousCursor returns the tuple of the query ID and the future object.

The return value of the future object is an AthenaResultSet object. This object has an interface that can fetch and iterate query results similar to synchronous cursors. It also has information on the result of query execution.

A query ID is required to cancel a query with the AsynchronousCursor.

NOTE: The cancel method of the future object does not cancel the query.

PandasCursor

PandasCursor directly handles the CSV file of the query execution result output to S3. This cursor is to download the CSV file after executing the query, and then loaded into DataFrame object. Performance is better than fetching data with a cursor.

You can use the PandasCursor by specifying the cursor_class with the connect method or connection object.

It can also be used by specifying the cursor class when calling the connection object's cursor method.

The as_pandas method returns a DataFrame object.

Support fetch and iterate query results.

The DATE and TIMESTAMP of Athena's data type are returned as pandas.Timestamp type.

Execution information of the query can also be retrieved.

NOTE: PandasCursor handles the CSV file on memory. Pay attention to the memory capacity.

AsyncPandasCursor

AsyncPandasCursor is an AsyncCursor that can handle Pandas DataFrame. This cursor directly handles the CSV of query results output to S3 in the same way as PandasCursor.

You can use the AsyncPandasCursor by specifying the cursor_class with the connect method or connection object.

It can also be used by specifying the cursor class when calling the connection object's cursor method.

The default number of workers is 5 or cpu number * 5. If you want to change the number of workers you can specify like the following.

The execute method of the AsynchronousPandasCursor returns the tuple of the query ID and the future object.

The return value of the future object is an AthenaPandasResultSet object. This object has an interface similar to AthenaResultSetObject.

This object also has an as_pandas method that returns a DataFrame object similar to the PandasCursor.

The DATE and TIMESTAMP of Athena's data type are returned as pandas.Timestamp type.

As with AsynchronousCursor, you need a query ID to cancel a query.

Quickly re-run queries

You can attempt to re-use the results from a previously run query to help save time and money in the cases where your underlying data isn't changing. Set the cache_size parameter of cursor.execute() to a number larger than 0 to enable cacheing.

Results will only be re-used if the query strings match exactly, and the query was a DML statement (the assumption being that you always want to re-run queries like CREATE TABLE and DROP TABLE).

The S3 staging directory is not checked, so it's possible that the location of the results is not in your provided s3_staging_dir.

Credentials

Support Boto3 credentials.

Additional environment variable:

Testing

Depends on the following environment variables:

Run test

Run test multiple Python versions

About

PyAthena is a Python DB API 2.0 (PEP 249) compliant client for Amazon Athena.

License:MIT License


Languages

Language:Python 99.3%Language:Shell 0.6%Language:TSQL 0.1%