0
0

事前準備

  • ライブラリである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']
0
0
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
0
0