Hive学习笔记十

Hive的case when语法

  • 方法一:
case 
when tb1.os = 'android' then 'android'
when tb1.os = 'ios' then 'iPhone'
else 'PC'
end as os
  • 方法二:
case tb1.os
when 'android' then 'android'
when 'ios' then 'iPhone'
else 'PC'
end as os

  解释:匹配 tb1表的 os 字段,当os为 'android' 返回 'android',当os为'ios' 返回 'iPhone' ,否则返回 'PC',最后一end结束,把字段名 as 为 os

完整示例:

select user_id,
case location
when '北京' then '1'
else null
end as location_in_beijing
from employees

Hive的dwd层结果表字段规范

  在做数仓开发的时候,我们需要根据业务从stg层多张表中查询数据,把其作为结果数据插入到dwd层的结果表中,在dwd层的表中,我们需要额外加三个字段,分别是自增主键id,是否合法数据 is_valid,和更新时间elt_update_time
  具体代码如下:

  • 创建结果表时:
create external table if not exists dwd.tableXXX(){
id bigint comment '主键',
···
···
···
is_valid bigint comment '0有效,1无效',
etl_update_time timestamp '更新时间'
}
  • 插入数据到结果表时:
insert overwrite table dwd.tableXXX(
select row_number() over(order by 1) as auto_increment_id,
···
···
···
0 is_valid,
form_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') as etl_update_time 
)
作者:落花桂
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/nthforsth/p/15045959.html