hive sql生成数仓分钟维表

目录

一、建表ddl

二、加工格式说明

三、加工sql 

四、示例结果数据


一、建表ddl

create table dim_pub_minute(
date_timestamp bigint comment '时间戳',
date_str       string comment '时间-日期时分秒',
day_str        string comment '日期',
time_str       string comment '时分秒',
hour_str       string comment '小时',
minute_str     string comment '分钟'
) comment '分钟维表'
row format delimited fields terminated by '\t'
stored as textfile
;

二、加工格式说明


select
from_unixtime(unix_timestamp('开始日期-时分秒') + a.pos*60) as date_str
from(
select  posexplode(split(repeat("o", cast((UNIX_TIMESTAMP('结束日期-时分秒')-UNIX_TIMESTAMP('开始日期-时分秒'))/60 as int)), "o"))
) a
;

三、加工sql 

with dim_pub_minute as (
select
     unix_timestamp('2022-11-21 00:01:00') + a.pos*60                as date_timestamp
    ,from_unixtime(unix_timestamp('2022-11-21 00:01:00') + a.pos*60) as date_str
from (select posexplode(split(repeat("o", cast((unix_timestamp('2022-11-21 00:10:00')-unix_timestamp('2022-11-21 00:01:00'))/60 as int)), "o"))) a
)

insert overwrite table dim_pub_minute
select 
     date_timestamp
    ,date_str
    ,substr(date_str,1,10)   as day_str
    ,substr(date_str,12)     as time_str
    ,substr(date_str,12,2)   as hour_str
    ,substr(date_str,15,2)   as minute_str
from dim_pub_minute
;

四、示例结果数据

hive sql生成数仓分钟维表_第1张图片

 

你可能感兴趣的:(数据仓库,hive,sql,hadoop)