2 回答

TA貢獻1846條經驗 獲得超7個贊
在 python 中使用以下代碼來獲取您正在尋找的數據。
import boto3
query = "SELECT * from table_name"
s3_resource = boto3.resource("s3")
s3_client = boto3.client('s3')
DATABASE = 'database_name'
output='s3://output-bucket/output-folder'
athena_client = boto3.client('athena')
# Execution
response = athena_client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': DATABASE
},
ResultConfiguration={
'OutputLocation': output,
}
)
queryId = response['QueryExecutionId']

TA貢獻1847條經驗 獲得超11個贊
我找到了一種使用 awswrangler 直接從 Athena 查詢數據到本地計算機上的 pandas 數據幀的方法。這不需要我們提供 S3 上的輸出位置。
profile_name = 'Dev-AWS'
REGION = 'us-east-1'
#this automatically retrieves credentials from your aws credentials file after you run aws configure on command-line
ACCESS_KEY_ID, SECRET_ACCESS_KEY,SESSION_TOKEN = get_profile_credentials(profile_name)
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN
)
wr.athena.read_sql_query("select * from table_name", database="db_name", boto3_session=session)
或者,如果您不想查詢 Athena,但想讀取整個粘合表,則可以使用:
my_df = wr.athena.read_sql_table(table= 'my_table', database= 'my_db', boto3_session=session)
添加回答
舉報