事前準備
- ライブラリであるboto3をインストールしているか
- .awsフォルダ直下にアクセスキー情報を配置しているか
- configファイルとcredentialsファイルは以下のような記述となる
[あなたの環境]
region = "あなたのリージョン"
output = "あなたの設定する記述フォーマット"
[あなたの環境]
aws_access_key_id="あなたのアクセスキー"
aws_secret_access_key="あなたのシークレットアクセスキー"
コード例
- クエリを記述し、pythonからathena上でクエリを実行する
- 基本的にはクライアントの作成、athenaクライアントの設定、実行したいクエリの記述、クエリの実行という段階に分けられる
- 実行したクエリの確認をするため、クエリの実行IDを取得している
boto3.py
# 各種設定
import boto3
# boto3セッションを使用しクライアントを作成
sessino = boto3.Session(profile_name='あなたの環境', region_name='あなたのリージョン')
# athenaクライアントの設定
client = session.client('athena')
# クエリの設定
query = "select * from 'あなたのテーブル' limit 100;"
# クエリの実行
response = client.start_query_execution(
QueryString = query,
QueryExcutionContext = {
'Database' : 'あなたのデータベース'
},
ResultConfiguration = {
'OutputLocation': 'あなたがアウトプットしたいS3バケット'
}
)
# クエリの実行IDを取得
query_executionid = response['QueryExecutionId']