【问题标题】:Performing Date Math on Hive Partition Columns在 Hive 分区列上执行日期数学
【发布时间】:2021-01-27 21:27:43
【问题描述】:
我的数据以标准 Hive 格式按天分区:
/year=2020/month=10/day=01
/year=2020/month=10/day=02
/year=2020/month=10/day=03
/year=2020/month=10/day=04
...
我想使用 Amazon Athena(IE:Presto)查询过去 60 天的所有数据。我希望此查询使用分区列(year、month、day),以便只扫描必要的分区文件。假设我无法更改文件分区格式,那么解决此问题的最佳方法是什么?
【问题讨论】:
标签:
hive
presto
amazon-athena
【解决方案1】:
您不必使用year、month、day 作为表的分区键。您可以拥有一个名为 date 的分区键并像这样添加分区:
ALTER TABLE the_table ADD
PARTITION (`date` = '2020-10-01') LOCATION 's3://the-bucket/data/year=2020/month=10/day=01'
PARTITION (`date` = '2020-10-02') LOCATION 's3://the-bucket/data/year=2020/month=10/day=02'
...
通过此设置,您甚至可以将分区键的类型设置为date:
PARTITIONED BY (`date` date)
现在您有一个带有date 列的表,其类型为DATE,您可以使用任何date and time functions 对其进行计算。
使用此设置您无法使用 MSCK REPAIR TABLE 加载分区,但无论如何您都不应该这样做 - 这非常缓慢且效率低下,而且确实只有当您有一对夫妇时才会这样做要加载到新表中的分区数。
【解决方案2】:
Theo 提出的另一种方法是使用以下语法,例如:
select ... from my_table where year||month||day between '2020630' and '20201010'
当 year、month 和 day 列的格式为字符串时,此方法有效。跨月查询特别有用。