【问题标题】:Athena partition projection date formattingAthena 分区投影日期格式化
【发布时间】:2021-02-10 02:12:02
【问题描述】:

我想使用 Athena 分区投影来编写过滤日期分区列的查询。问题是我需要将投影的物理数据格式(S3 文件前缀)与用户查询的日期格式不同。

例如,我在 S3 中的物理数据格式将前缀存储为 URL 编码日期:s3://bucket/table_root/landing_time=yyyy-MM-dd%20HH%3Amm%3Ass.S'

但我希望用户能够使用可读日期(无 URL 编码格式)查询 Athena:

select * from table_root where landing_time='2020-01-01 12:00:00.0'

我尝试使用下表设置进行分区投影:

projection.landing_time.range: NOW-2YEARS,NOW
projection.landing_time.type: date
projection.landing_time.interval: 1
projection.landing_time.interval.unit: HOURS
projection.landing_time.format: yyyy-MM-dd'%20'HH'%3A00%3A00.0'
projection.enabled: true

我看到分区投影允许使用表属性进行自定义路径派生:storage.location.template: s3://bucket/table_root/a=${a}/${b}/some_static_subdirectory/${c}/

但我仍然看不到任何方法来自定义日期列的投影格式并允许它与用于运行查询的格式不同。有没有办法做到这一点?

【问题讨论】:

    标签: aws-glue amazon-athena


    【解决方案1】:

    这可以通过添加一个额外的分区列来处理小时并将landing_time 更改为仅处理日期部分来解决。之后,将storage.location.template 更改为包含URL 编码所需的%20%3A 值,这给了我想要的结果。

    projection.landing_time.range: 2020-01-01,NOW
    projection.landing_time.type: date
    projection.landing_time.interval: 1
    projection.landing_time.interval.unit: DAYS
    projection.landing_time.format: yyyy-MM-dd
    projection.hours.type: integer
    projection.hours.range: 0,23
    projection.hours.digits: 2
    projection.enabled: true
    storage.location.template: s3://bucket/table_root/landing_time=${landing_time}%20${hours}%3A00%3A00.0
    

    使用分区投影的 Athena 查询现在可以编写为以下格式:

    select * from table_root where landing_time='2020-01-01' and hours=1; 
    select * from table_root where landing_time='2020-01-01' and hours>2 and hours<10;
    

    与我的 S3 数据前缀匹配的正确格式将被投影到 S3。

    【讨论】:

    【解决方案2】:

    java DateTimeFormatter 允许以格式插入自定义字符串,例如,文本The year is %20 YYYY 将被解析为The year is %20 2021 之类的内容。您可能需要将landing_time=yyyy-MM-dd'%20'HH'%3A00%3A00.0' 指定为格式,而不仅仅是yyyy-MM-dd'%20'HH'%3A00%3A00.0'

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 2023-04-10
      • 1970-01-01
      • 2020-10-15
      • 2021-03-16
      • 2021-05-11
      • 2020-01-13
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多