【问题标题】:get latest data from hive table with multiple partition columns从具有多个分区列的配置单元表中获取最新数据
【发布时间】:2020-04-27 16:26:06
【问题描述】:

我有一个具有以下结构的配置单元表

ID string,
Value string,
year int,
month int,
day int,
hour int,
minute int

此表每 15 分钟刷新一次,并按年/月/日/小时/分钟列进行分区。请在下面找到分区示例。

year=2019/month=12/day=29/hour=19/minute=15
year=2019/month=12/day=30/hour=00/minute=45
year=2019/month=12/day=30/hour=08/minute=45
year=2019/month=12/day=30/hour=09/minute=30
year=2019/month=12/day=30/hour=09/minute=45

我只想从表中选择最新的分区数据。我尝试对这些分区列使用 max() 语句,但由于数据量很大,因此效率不高。 请告诉我,如何使用 hive sql 以方便的方式获取数据。

【问题讨论】:

    标签: performance hive hiveql partition hive-partitions


    【解决方案1】:

    如果最新的分区总是在当前日期,那么你可以过滤当前日期的分区并使用rank()来查找具有最新小时、分钟的记录:

    select * --list columns here
    from
    (
    select s.*, rank() over(order by hour desc, minute desc) rnk
      from your_table s
     where s.year=year(current_date)   --filter current day (better pass variables calculated if possible)
       and s.month=lpad(month(current_date),2,0) 
       and s.day=lpad(day(current_date),2,0)
       -- and s.hour=lpad(hour(current_timestamp),2,0) --consider also adding this
    ) s 
    where rnk=1 --latest hour, minute
    

    如果最新的分区不一定等于 current_date 那么你可以使用rank() over (order by s.year desc, s.month desc, s.day desc, hour desc, minute desc),如果没有过滤日期,这将扫描所有的表,效率不高。

    如果你可以在 shell 中计算分区过滤器并作为参数传递,它将表现最好。见代码中的 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多