当我运行 MSCK REPAIR TABLE 查询时,Amazon Athena 会返回分区列表。但是,Athena 无法将分区添加到 AWS Glue Data Catalog 中的表中。
简述
以下常见原因会导致 Athena 无法向 Data Catalog 中的表添加分区:
- AWS Identity and Access Management (IAM) 用户或角色没有允许 glue:BatchCreatePartition 操作的策略。
- Amazon Simple Storage Service (Amazon S3) 路径采用驼峰式大小写而不是小写。
- 分区采用的格式不是 Hive 格式:year=2023/month=01/day=01。而是非 Hive 格式:/2023/01/01/。
解决方法
在 IAM 策略中允许 glue:BatchCreatePartition
查看附加到用于运行 MSCK REPAIR TABLE 的用户或角色的 IAM 策略。当您将 Data Catalog 与 Athena 一起使用时,IAM 策略必须允许 glue:BatchCreatePartition 操作。如果策略不允许此操作,Athena 则无法向元存储添加分区。有关允许 glue:BatchCreatePartition 操作的 IAM 策略的示例,请参阅 AWS 托管式策略: AmazonAthenaFullAccess。
将 Amazon S3 路径改为小写
要 MSCK REPAIR TABLE 将分区添加到 Data Catalog,Amazon S3 路径名必须为小写。
例如,如果 Amazon S3 路径采用驼峰式大小写 userId,以下分区不会被添加到 Data Catalog:
- s3://awsdoc-example-bucket/path/userId=1/
- s3://awsdoc-example-bucket/path/userId=2/
- s3://awsdoc-example-bucket/path/userId=3/
要解决此问题,使用小写的 userid:
- s3://awsdoc-example-bucket/path/userid=1/
- s3://awsdoc-example-bucket/path/userid=2/
- s3://awsdoc-example-bucket/path/userid=3/
运行 ALTER TABLE ADD PARTITION 命令
运行 ALTER TABLE ADD PARTITION 命令,向 Hive 式和非 Hive 式分区数据的粘合表添加分区。
非 Hive 式分区数据需要 LOCATION 子句。确保为包含分区数据的前缀提供完整的 Amazon S3 路径:
ALTER TABLE orders ADD
PARTITION (year = '2023', month = '01', day ='01')
LOCATION 's3://mystorage/path/to/test/'
PARTITION (year = '2023', month = '01', day ='02')
LOCATION 's3://mystorage/path/to/test/';
相关信息
在 Athena 中对数据分区
Amazon Athena 的操作、资源和条件键
AWS Glue 的操作、资源和条件键