我需要处理存储桶中特定文件夹中某个流的一些数据S3
。我想在Python
. 搜索了一段时间后,我找到了PyAthena
正是我要找的图书馆!
我安装1.8.0
了PyAthena
.
供您参考,我的S3
存储桶位于 地区,Paris eu-west-3
我的Athena
数据库位于 地区Francfort eu-central-1
。
我使用了在文档PyAthena Doc中找到的以下代码:
from pyathena import connect
cursor = connect(aws_access_key_id='YOUR_ACCESS_KEY_ID',
aws_secret_access_key='YOUR_SECRET_ACCESS_KEY',
s3_staging_dir='s3://YOUR_S3_BUCKET/path/to/',
region_name='us-west-2').cursor()
cursor.execute("SELECT * FROM one_row")
print(cursor.description)
print(cursor.fetchall())
一开始我不确定region_name
要使用哪个,如果它应该是存储桶Paris
所在的位置,还是数据库所在的位置!!S3
Francfort
Athena
我尝试了这两种方法并按照我收到的错误消息,我最终使用了我的一个S3
桶!但是,我不断收到有关权限的错误Glue
,例如:
pyathena.error.OperationalError: Insufficient permissions to execute the query. Error retrieving table : master in database : default due to : User: arn:aws:iam::<my-account-client-ID>:user/s3-test is not authorized to perform: glue:GetTable on resource: arn:aws:glue:eu-west-3:<my-account-client-ID>:catalog
所以我在中添加了以下策略IAM
:
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"athena:StartQueryExecution",
"athena:GetQueryResultsStream",
"athena:GetQueryResults",
"athena:DeleteNamedQuery",
"athena:GetNamedQuery",
"athena:*",
"athena:ListQueryExecutions",
"athena:ListNamedQueries",
"athena:CreateNamedQuery",
"athena:StopQueryExecution",
"athena:GetQueryExecution",
"athena:BatchGetNamedQuery",
"athena:BatchGetQueryExecution"
],
"Resource": "*"
},
{
"Sid": "VisualEditor3",
"Effect": "Allow",
"Action": [
"glue:GetTable",
"glue:GetTables",
"glue:GetDatabase"
],
"Resource": [
"arn:aws:glue:eu-west-3:<my-account-client-ID>:catalog",
"arn:aws:glue:eu-west-3:<my-account-client-ID>:database/*",
"arn:aws:glue:eu-west-3:<my-account-client-ID>:table/*/*"
]
}
现在我有这个错误信息:
cursor.execute("select * from master")
File "/home/ubuntu/.local/lib/python3.6/site-packages/pyathena/util.py", line 28, in _wrapper
return wrapped(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/pyathena/cursor.py", line 57, in execute
raise OperationalError(query_execution.state_change_reason)
pyathena.error.OperationalError: SYNTAX_ERROR: line 1:15: Schema default does not exist