Hive常用函数

本文详细介绍SQL中的IF函数、CASE条件判断函数、NVL函数、trim系列函数等,并讲解了正则表达式替换函数、字符串连接函数、日期时间转换函数、左补足与右补足函数及排序函数的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IF函数:if

语法: if(testCondition,valuTrue,valueFalseOrNull)
说明:当条件 testCondition 为True时,返回valueTrue, 否则返回valueFalseOrNull

2322

条件判断函数:CASE

语法: Case A when b THEN c [WHEN d THEN e] * [ELSE f] END
说明:如果a 等于 b,那么返回c,如果a等于d,那么返回e,否则返回f

eg:
 -- 销售类型增加一级部门的prt_distribution_flag_name:直销为 -1,分销为-2
select
case t1.prt_distribution_flag_name When '直销' then '-1' when '分销' then '-2' else '未知' end as parent_dept_id,
t2.distribution_flag_key as dept_id,
t1.*
from
(select
     distinct 
     lvl,
     full_name,     
     prt_distribution_flag_name,
     distribution_flag_name
from dw.ol_power_distribution_type_dim 
where lvl =2 and dt ='20180813') t1 
left join 
dw.kn1_dim_distribution t2 on t1.distribution_flag_name =t2.distribution_flag_name 
and t1.prt_distribution_flag_name = t2.prt_distribution_flag_name

踩坑:Query 20180815_101245_23113_5iy2v failed: line 2:77: All CASE results must be the same type: bigint类型要一致

22222222222222222

NVL

语法:nvl(value,default_value)
说明:如果value是null,返回default_value)

去空格函数:trim,ltrim,rtrim

语法:
trim(A):去除字符串两边的空格
ltrim(A):去除字符串左边的空格
rtrim(A):去除字符串右边的空格

2323232

正则表达式替换函数:regexp_replace

语法:regexp_replace(A,B,C) 说明:将字符串A中的符合java正则表达式B的部分替换成C。
注意:在有些情况下要使用转义字符,类似oracle中的regexp_replace函数

23232

字符串连接函数:concat

语法: concat(str1,str2,str3,…strN)
说明:返回输入字符串连接后的结果,支持任意个输入字符串。

日期时间转日期函数: to_date

语法:to_date(expr)
说明:返回日期时间段内的日期部分。`

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fwtmkAjB-1600857848133)(img/to_date.png)]

左补足函数:lpad

语法:lpad(str,len,pad)
说明:将str用pad进行左补足到len位

2232

右补足函数: rpad

语法:rpad(str,len,pad)
说明:将str用pad进行右补足到len位

23232

排序函数:row_number() Over()

语法:
row_number() over (partition by xxx order by xxx) rank(其中rank为分组的别名,你也可以换个名字比方说换成hahahah)
说明:从 1开始,按照顺序 生成分组内记录的开始。

使用事例

数据提取目标:从tmp_test表中根据col1字段去重,选取clo2最大的那条记录,导入tmp_test_c表。

创建数据表:
    create table tmp_test(col1 string,clo2 string);
添加测试数据:
    insert into table tmp_test
    select 1,'str1' from dual
    union all
    select 2,'str2' from dual
    union all
    select 3,'str3' from dual
    union all
    select 3,'str31' from dual
    union all
    select 3,'str33' from dual
    union all
    select 4,'str41' from dual
    union all
    select 4,'str42' from dual;

查看数据:

232

使用row_number()函数查询数据:

hive (default)> select t.*,row_number() over(partition by col1,clo2 sort by clo2 desc) rn
              > from tmp_test t;

23232

按照数据提取目标提取数据:

hive (default)> select *
              > from
              > (
              > select t.*,row_number() over(distribute by col1 sort by clo2 desc) rn
              > from tmp_test t
              > )tt
              > where tt.rn=1;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eB31FtPQ-1600857848135)(img/row_number3.png)]

获取当前UNIX时间戳函数:unix_timestamp

语法:unix_timestamp()说明:获取当前时区的UNIX时间戳
232323

Unix时间戳转日期函数:from_unixtime()

语法:from_unixtime(bigint unixtime[,string format])
说明:转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式。

23232323232

带分隔符字符串连接函数:concat_ws

语法:concat_ws(SEP,A,B…)
说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符。

23232323232


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

piepis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值