如何访问分区 Athena 表的子目录中的数据

3no*_*oot 2 aws-glue aws-glue-data-catalog

我有一个 Athena 表,每天都有一个分区,其中实际文件按小时位于“子目录”中,如下所示:

s3://my-bucket/data/2019/06/27/00/00001.json
s3://my-bucket/data/2019/06/27/00/00002.json
s3://my-bucket/data/2019/06/27/01/00001.json
s3://my-bucket/data/2019/06/27/01/00002.json
Run Code Online (Sandbox Code Playgroud)

Athena 能够毫无问题地查询此表并找到我的数据,但在使用 AWS Glue 时,它​​似乎无法找到此数据。

ALTER TABLE mytable ADD 
PARTITION (year=2019, month=06, day=27) LOCATION 's3://my-bucket/data/2019/06/27/01';

select day, count(*)
from mytable
group by day;

day .   count
27 .    145431
Run Code Online (Sandbox Code Playgroud)

我已经尝试将分区的位置更改为以斜杠 ( s3://my-bucket/data/2019/06/27/01/)结尾,但这没有帮助。

以下是 Glue 中的分区属性。我希望 storedAsSubDirectories 设置会告诉它迭代子目录,但情况似乎并非如此:

{
    "StorageDescriptor": {
        "cols": {
            "FieldSchema": [
                {
                    "name": "userid",
                    "type": "string",
                    "comment": ""
                },
                {
                    "name": "labels",
                    "type": "array<string>",
                    "comment": ""
                }
            ]
        },
        "location": "s3://my-bucket/data/2019/06/27/01/",
        "inputFormat": "org.apache.hadoop.mapred.TextInputFormat",
        "outputFormat": "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat",
        "compressed": "false",
        "numBuckets": "0",
        "SerDeInfo": {
            "name": "JsonSerDe",
            "serializationLib": "org.openx.data.jsonserde.JsonSerDe",
            "parameters": {
                "serialization.format": "1"
            }
        },
        "bucketCols": [],
        "sortCols": [],
        "parameters": {},
        "SkewedInfo": {
            "skewedColNames": [],
            "skewedColValues": [],
            "skewedColValueLocationMaps": {}
        },
        "storedAsSubDirectories": "true"
    },
    "parameters": {}
}
Run Code Online (Sandbox Code Playgroud)

当 Glue 针对同一个分区/表运行时,它会找到 0 行。

但是,如果所有数据文件都出现在分区的根“目录”中(即s3://my-bucket/data/2019/06/27/00001.json),那么Athena和Glue都可以找到数据。

Glue 无法找到数据文件的原因是什么?我不希望每小时创建一个分区,因为这意味着每年有 8700 个分区(Athena 每个表有 20,000 个分区的限制)。

3no*_*oot 5

显然,在 create_dynamic_frame 上有一个未记录的附加选项用于“递归”: additional_options = {"recurse": True}

例子:

athena_datasource = glueContext.create_dynamic_frame.from_catalog(database = target_database, table_name = target_table, push_down_predicate = "(year=='2019' and month=='06' and day=='27')", transformation_ctx = "athena_datasource", additional_options = {"recurse": True})

我刚刚使用此选项测试了我的 Glue 作业,并且可以确认它现在可以找到所有 s3 文件。