Pyathena 模式不存在

Ben*_*enz 2 amazon-web-services amazon-iam pyathena

我需要处理存储桶中特定文件夹中特定流的一些数据S3。我想在 中执行此操作Python。经过一段时间的搜索,我找到了PyAthena我正在寻找的图书馆!

我安装的版本1.8.0PyAthena.

供您参考,我的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 : 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
Run Code Online (Sandbox Code Playgroud)

所以我在 中添加了以下策略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/*/*"
        ]
    }
Run Code Online (Sandbox Code Playgroud)

现在我有这个错误消息:

    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
Run Code Online (Sandbox Code Playgroud)

Jul*_*lio 5

问题在于 select 语句:如果您不指定它,您将使用默认数据库,如果您的环境中没有这样的数据库,它将失败。您应该指出您的数据库和表:

cursor.execute("SELECT * FROM <YOUR_DATABASE>.<YOUR_TABLE>")
Run Code Online (Sandbox Code Playgroud)

或者您也可以在游标函数中使用参数指定数据库名称(或模式名称):

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(schema_name=<YOUR_DATABASE>)
cursor.execute("SELECT * FROM <YOUR_TABLE>")
Run Code Online (Sandbox Code Playgroud)

如果您执行其中一项操作,则不应再出现相同的错误。