Athena 分区位置

Athena partition locations

我可以使用

查看 table 上的所有分区
show partitions my_table

我可以使用

查看分区的位置
describe formatted my_table partition (partition_col='value')

但我有很多分区,如果可以避免的话,我不想解析 describe formatted 的输出。

有没有办法在单个查询中获取所有分区及其位置?

没有内置或一致的方法来获取此信息。

假设您知道您的分区列,您可以通过类似

的查询来获取此信息
select distinct partition_col, "$path" from my_table

获取 table 分区位置的最便宜的方法是使用 Glue API 的 GetPartitions 调用。它将列出所有分区、它们的值和位置。您可以像这样使用 AWS CLI 工具进行尝试:

aws glue get-partitions --region us-somewhere-1 --database-name your_database --table-name the_table

SELECT DISTINCT partition_col, "$path" FROM the_table 那样使用 SQL 可能会很昂贵,因为不幸的是 Athena 扫描了整个 table 以生成输出(它本可以只查看 table 元数据但是该优化似乎还不存在)。

使用 boto3(自版本 1.12.9 起)以下返回完整列表:

glue_client = boto3.client("glue")
glue_paginator = glue_client.get_paginator("get_partitions")
pages_iter = glue_paginator.paginate(
    DatabaseName=db_name, TableName=table_name
)
res = []
for page in pages_iter:
    for partition in page["Partitions"]:
        res.append(
            {
                "Values": partition["Values"],
                "Location": partition["StorageDescriptor"]["Location"],
            }
        )