【问题标题】:hive: split a row into multiple rows at one minute interval between two dates in hive蜂巢:在蜂巢中的两个日期之间以一分钟的间隔将一行拆分为多行
【发布时间】:2018-01-30 22:04:19
【问题描述】:

我有一个配置单元表“driver_time_stats”,其中包含 slot_id、number_of_drivers、slot_start_time 和 slot_end_time 列。

-----------------------------------------------------------------------
slot_id | number_of_drivers | slot_start_time     | slot_end_time
-----------------------------------------------------------------------
1       | 5                 | 2018-01-01 09:30:00 | 2018-01-01 10:00:00
2       | 8                 | 2018-01-01 10:30:00 | 2018-01-01 11:00:00
-----------------------------------------------------------------------

所需的输出:每行应在 slot_start_time 和 slot_end_time 之间以 1 分钟的间隔拆分为多行。

-----------------------------------------------------------------------
slot_id | number_of_drivers | slot_start_time     | slot_end_time
-----------------------------------------------------------------------
1       | 5                 | 2018-01-01 09:30:00 | 2018-01-01 09:31:00
1       | 5                 | 2018-01-01 09:31:00 | 2018-01-01 09:32:00
.
.
.
1       | 5                 | 2018-01-01 09:59:00 | 2018-01-01 10:00:00

2       | 8                 | 2018-01-01 10:30:00 | 2018-01-01 10:31:00
2       | 8                 | 2018-01-01 10:31:00 | 2018-01-01 10:32:00
.
.
.
2       | 8                 | 2018-01-01 10:59:00 | 2018-01-01 11:00:00

-----------------------------------------------------------------------

我正在使用横​​向视图、posexplode 等功能,但无法做到。有人能帮我一下吗 ?换句话说,我试图在蜂巢中以一分钟的间隔将一条记录切成多条记录。我能够使用 UNNEST 在 presto 中实现它,但我希望 hive 中的解决方案仅作为构建在 hive 上的 ETL。

-纳什

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    好吧,我可以在朋友亨利的帮助下找到答案,在这里发布相同的内容,以便帮助其他人寻找类似问题的解决方案。下面的代码剪断为您提供指导。您可以根据需要对其进行调整。

    select from_unixtime(unix_timestamp(st) + pe.i*60) FROM (
      select '2018-01-12 09:00:00' as st, '2018-01-12 11:30:00' as en) t
      lateral VIEW
      posexplode(split(
      space(cast(floor((unix_timestamp(t.en)-unix_timestamp(t.st))/60) as INT)), ' ')) pe as i, x
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2020-08-26
      • 2015-05-20
      • 2019-12-22
      • 2015-06-17
      相关资源
      最近更新 更多