LoginSignup
3
5

More than 3 years have passed since last update.

[Tips]AthenaをPythonで扱う

Last updated at Posted at 2019-11-21

はじめに

この記事はAthenaへクエリーを投げて、その結果をpandasで分析したい人のための記事です。
特にJupyter notebookで分析をしているときに便利だと思います。

インストール

PyAthenaをインストールします。

pip install PyAthena 

使い方

connect関数を利用します。AWSのキーとAthenaでクエリーを実行した結果を吐き出すS3のpathを指定します。
pd.read_sqlという関数を利用して、実行すると実行結果がpandasの形でゲットできます。

from pyathena import connect
import pandas as pd
aws_access_key_id = 'Your aws access key id'
aws_secret_access_key = 'Your aws secret access key'

conn = connect(aws_access_key_id=aws_access_key_id,
                 aws_secret_access_key=aws_secret_access_key,
                 s3_staging_dir='Your s3 path',
                 region_name='ap-northeast-1')

df = pd.read_sql("SELECT * FROM sample", conn)
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5