๐ก Today I Learned ์์ฝ (24ํ์ฐจ)
- boto3์์ Athena ์ฟผ๋ฆฌ ์ํ๋ฅผ ํ์ธํ๋ batch_get_query_execution๊ฐ ์กด์ฌ
- ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉํด์ Athena๊ฐ ์ฟผ๋ฆฌ๋ฅผ ๋๊ณ ์๋์ง ์ํ๋ฅผ ํ์ธํ ์ ์๋ค.
Python ๊ธฐ๋ฐ์ AWS SDK์ธ boto3๋ฅผ ์ฌ์ฉํด์ Athena ์ฟผ๋ฆฌ๋ฅผ ์คํํ ์ ์๋ค.
ํ์ง๋ง boto3๋ก Athena ์ฟผ๋ฆฌ๋ฅผ ์คํํ ๊ฒฝ์ฐ async๋ก ๋์๊ฐ๊ธฐ ๋๋ฌธ์ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ ธ๋ค๊ณ ๋ฐ๋ก ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ ๊ฒ ์๋๋ค. ๊ทธ๋ด ๋ ์ฌ์ฉํ๊ธฐ ์ํ batch_get_query_execution๊ฐ boto3์ ์กด์ฌํ๋ค.
query ์คํํ์ ๋์ ExecutionID๋ฅผ ์๊ณ ์๋ค๋ฉด ์๋์ ๊ฐ์ด batch_get_query_execution์ ๋ฃ์ด์ status ๊ฐ์ ํ์ธํ ์ ์๋ค. ์ฌ๋ฌ๊ฐ์ง ์ ๋ณด๊ฐ return ๋์ง๋ง ์๋ ์ฝ๋ ๊ธฐ์ค์ผ๋ก๋ง ๊ฐ๋ค๋ฉด status ๊ฐ QUEUED / RUNNING / SUCCEEDED / FAILED / CANCELLED ์ค ํ๋๊ฐ return ๋๊ฒ ๋๋ค. ์ด๋ฅผ ํ์ธํ์ฌ ์ฟผ๋ฆฌ ์คํ ํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ํ์ธํ๊ธฐ๊น์ง while ๊ฐ์ ๊ฑธ ์ฌ์ฉํด์ ๋๊ธฐ๋ฅผ ๊ฑธ์ด๋์์๋ ์์ ๊ฒ์ด๋ค.
์์์ QueryExecutionIds๋ An array of query execution IDs. ์ฆ, array๋ก ์ฌ๋ฌ ๊ฐ์ ๋ฃ์ด์ ์กฐํ๋ ๊ฐ๋ฅํ๋ค. ์ต๋ 50๊ฐ๊น์ง ์กฐํ๊ฐ ๊ฐ๋ฅํ ๋ฏ ํ๋ค.
import boto3
athena = boto3.client('athena',
aws_access_key_id='API_KEY',
aws_secret_access_key='SECRET_KEY',
region_name='REGION'
)
response = athena.batch_get_query_execution(QueryExecutionIds=['QueryExecutionIds'])
status = response['QueryExecutions'][0]['Status']['State']
print(status)
#athena ๊ด๋ จ ๋ฆฌ์์น ๋ด์ญ:
๋๊ธ ์์ญ