Skip to content

Instantly share code, notes, and snippets.

@kaoru-furuta
Last active December 2, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaoru-furuta/90b28b1b4b3feceb10ee4e984ff1c7f9 to your computer and use it in GitHub Desktop.
Save kaoru-furuta/90b28b1b4b3feceb10ee4e984ff1c7f9 to your computer and use it in GitHub Desktop.
import boto3
from time import sleep
# region 指定は適切に
client = boto3.client('athena', region_name='us-west-2')
# ざっくりとクエリを投げる
response = client.start_query_execution(
QueryString='select * from test_table',
QueryExecutionContext={
'Database': 'test_database'
},
ResultConfiguration={
'OutputLocation': 's3://kaorr-test-bucket/'
}
)
# レスポンスからクエリの ID をゲット
query_id = response['QueryExecutionId']
# クエリ ID を引数に渡して、進捗状況を確認
# ステータスが SUCCEEDED or FAILED or CANCELLED になるまでループ
while True:
response = client.get_query_execution(QueryExecutionId=query_id)
state = response['QueryExecution']['Status']['State']
if state in ['SUCCEEDED', 'FAILED', 'CANCELLED']:
break
else:
print('wait...')
sleep(1)
# クエリ ID を引数に渡して、実行結果を取得
response = client.get_query_results(QueryExecutionId=query_id)
# 実行結果のデータをざっくりと表示
for rows in response['ResultSet']['Rows']:
print([data['VarCharValue'] for data in rows['Data']])
# 実行するとこんな感じ。
#
# python sample.py
# wait...
# wait...
# ['name', 'age']
# ['yamada', '25']
# ['tanaka', '30']
# ['takeda', '35']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment