我正在尝试使用 python 在 Athena 上执行查询。
示例代码
client = boto3.client(
'athena',
region_name=region,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)
execution = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': database
},
WorkGroup=workgroup,
ResultConfiguration={
'OutputLocation': S3_OUTPUT_LOCATION
}
)
Run Code Online (Sandbox Code Playgroud)
这是工作代码,但我遇到了一个不寻常的情况。
InvalidRequestException: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: Unable to verify/create output bucket <BUCKET NAME>
Run Code Online (Sandbox Code Playgroud)
我们观察这种情况几天了,每 24 小时 python 脚本都会抛出错误,然后我们在 Athena 控制台(查询编辑器)上执行查询并重新运行 python 脚本。我不明白为什么会发生这种情况,是否存在任何权限问题。
权限: …
我从 AWS Athena 控制台运行查询需要 10 秒。使用PyAthena从Sagemaker运行相同的查询需要 155 秒。PyAthena 是否会减慢速度,或者从 Athena 到 sagemaker 的数据传输是否如此耗时?
我可以做些什么来加快速度?
python amazon-web-services amazon-athena pyathena amazon-sagemaker
这个查询在 Athena 的前端工作正常:
SELECT * FROM analysisdata."iris" limit 10;
Run Code Online (Sandbox Code Playgroud)
我正在使用此 Python 代码通过 Python/pyathena 运行上述查询
from pyathena import connect
cursor = connect(aws_access_key_id='AKI.DELETED.2Q',
aws_secret_access_key='D.DELETED.Al',
s3_staging_dir='s3://Bla887342ac-a3ce-4600-94d0-9092f4a6bd20/Iris',
region_name='eu-west-1').cursor()
cursor.execute("""SELECT * FROM analysisdata.""iris"" limit 10;""")
print(cursor.description)
print(cursor.fetchall())
Run Code Online (Sandbox Code Playgroud)
不幸的是我得到:
pyathena.error.DatabaseError: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: line 1:27: mismatched input '.' expecting {<EOF>, ',', 'WHERE', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'APPROXIMATE', 'JOIN', 'CROSS', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'NATURAL', 'UNION', 'EXCEPT', 'INTERSECT'}
Run Code Online (Sandbox Code Playgroud)
我想是我的介绍:
""" and ""
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
PS:
我试过:
cursor.execute("SELECT * FROM analysisdata.""iris"" limit 10;") …Run Code Online (Sandbox Code Playgroud) 我是 AWS 新手。我有一个用户帐户和两个角色,一个用于生产,一个用于测试。
通常我登录我的帐户并切换到产品角色来运行一些简单的选择查询。
现在我想通过 PyAthena 在 Python 中本地使用 Athena。我已经尝试了 PyAthena 文档中的以下资源:
from pyathena import connect
import pandas as pd
conn = 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')
df = pd.read_sql("SELECT * FROM many_rows", conn)
print(df.head())
Run Code Online (Sandbox Code Playgroud)
但总是出现错误
An error occurred (AccessDeniedException) when calling the StartQueryExecution operation: User: arn:aws:iam::xxxxxx:user/xxxx@xxxxx is not authorized to perform: athena:StartQueryExecution on resource: arn:aws:athena:ap-southeast-2:xxxxx:workgroup/primary
Run Code Online (Sandbox Code Playgroud)
如果我使用我的用户帐户运行相同的查询而不切换角色,这正是我会得到的错误。
我也尝试添加 aprofile name parameter in connect但仍然无法工作,即使环境被正确识别。
有人可以帮助我如何在本地 python 代码中执行“切换”角色步骤吗?
我在更新 Amazon Athena Tables 的 EMR 集群上运行自动 Python 作业。
直到几天前它运行良好(在 python 2.7 和 3.7 上)。这是脚本:
from pyathenajdbc import connect
import yaml
config = yaml.load(open('athena-config.yaml', 'r'))
statements = config['statements']
staging_dir = config['staging_dir']
conn = connect(s3_staging_dir=staging_dir, region_name='eu-west-1')
try:
with conn.cursor() as cursor:
for statement in statements:
cursor.execute(statement)
finally:
conn.close()
Run Code Online (Sandbox Code Playgroud)
athena-config.yaml 有一个暂存目录和一些 Athena 语句。
这是错误:
You are using pip version 9.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Unrecognized option: -server
create_tables.py:5: YAMLLoadWarning: calling …Run Code Online (Sandbox Code Playgroud) 我需要处理存储桶中特定文件夹中特定流的一些数据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())
Run Code Online (Sandbox Code Playgroud)
我一开始不确定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 …Run Code Online (Sandbox Code Playgroud) 哪一个更快pyathena或boto3使用 python 脚本查询 AWS Athena 模式?
目前我正在使用 pyathena 来查询 Athena 模式,但它非常慢,我知道还有 boto3 的另一种选择,但在开始之前需要一些专家的建议。
我需要将Amazon Athena查询结果存储到New Amazon Athena Table中。