无法修复 Amazon Athena 中的分区

Failure to repair partitions in Amazon Athena

我最近一直在从事一个项目,该项目涉及使用 Glue 托管服务在 Amazon S3 中抓取数据。这成功创建了一个 Metastore,我可以在 Glue 控制台上看到它。

我通过控制台手动解决了模式中的错误,例如STRING --> TIMESTAMP、BIGINT --> STRING 等。分区似乎是从 S3 路径中的密钥自动创建的。即我在 s3 中的对象键是这样的:

s3://mybucket/YYYMM/object.csv

Glue 通过密钥的 YYYMM(例如 201711)部分成功对数据进行分区。

当我 运行 查询时,出现以下错误 HIVE_PARTITION_SCHEMA_MISMATCH 这表明虽然 table 架构已更新,但分区架构尚未

查看文档我发现...https://docs.aws.amazon.com/athena/latest/ug/glue-best-practices.html#schema-syncing

具体

There are a few ways to fix this issue. First, if the data was accidentally added, you can remove the data files that cause the difference in schema, drop the partition, and re-crawl the data. Second, you can drop the individual partition and then run MSCK REPAIR within Athena to re-create the partition using the table's schema. This second option works only if you are confident that the schema applied will continue to read the data correctly.

所以我尝试了第二个选项 运行。

ALTER TABLE mydb.mytable DROP PARTITION (partition_0=201711), PARTITION (partition_0=201712)
MSCK REPAIR TABLE mydb.mytable

删除分区似乎是成功的,但是 运行修复tables 产量

Partitions not in metastore: mytable:201711 mytable:201712

而且我无法取回任何数据。手动读取分区好像也不行。

例如

ALTER TABLE mydb.mytable ADD
    PARTITION (partition_0=201711) LOCATION 's3://bucket/201711',

给出错误line 2:2: missing 'column' at 'partition' (service: amazonathena; status code: 400; error code: invalidrequestexception;

如有任何帮助,我们将不胜感激

万一以后有人遇到这个问题,我在这个问题中找到了问题的答案。

因此,将我的存储桶中的密钥格式从

更改为

s3://mybucket/YYYMM/object.csv 至:

s3://mybucket/date=YYYMM/object.csv

然后是运行

ALTER TABLE mydb.mytable DROP PARTITION (partition_0=201711), PARTITION (partition_0=201712)
MSCK REPAIR TABLE mydb.mytable

重新添加了我丢失的分区。