hive--内置函数

hive为我们提供了一个很全面的函数库,基本上覆盖了我们的工作中会用的地方

类型转换

cast

//a为要转换的数据b为要转换的类型
select cast(a as b) ;

数学运算函数

select round(5.4) ;   ## 5
select round(5.1345,3) ;  ##5.135
select ceil(5.4); // select ceiling(5.4);   ## 6
select floor(5.4) ;  ## 5
select abs(-5.4) ;  ## 5.4
select greatest(3,5,6);  ## 6 

字符串函数

substr(string, int start)   ## 截取子串
substring(string, int start)
示例:select substr("abcdefg",2);

substr(string, int start, int len) 
substring(string, int start, int len)
示例:select substr("abcdefg",2,3) ;

concat(string A, string B...)  ## 拼接字符串
concat_ws(string SEP, string A, string B...)
示例:select concat("ab","xy") from dual;
select concat_ws(".","192","168","33","44");

length(string A)
示例:select length("192.168.33.44") ;

时间函数


## 取当前时间的毫秒数时间戳 
select unix_timestamp();

## unix时间戳转字符串
from_unixtime(bigint unixtime[, string format])
示例:select from_unixtime(unix_timestamp());
select from_unixtime(unix_timestamp(),"yyyy/MM/dd HH:mm:ss");

## 字符串转unix时间戳
unix_timestamp(string date, string pattern)
示例: select unix_timestamp("2017-08-10 17:50:30");
select unix_timestamp("2017/08/10 17:50:30","yyyy/MM/dd HH:mm:ss");

## 将字符串转成日期date
select to_date("2017-09-17 16:58:32");

表生成函数

行转列函数:explode()

假如有以下数据:
1,zhangsan,化学:物理:数学:语文
2,lisi,化学:数学:生物:生理:卫生
3,wangwu,化学:语文:英语:体育:生物
映射成一张表:
create table t_stu_subject(id int,name string,subjects array<string>)
row format delimited fields terminated by ','
collection items terminated by ':';

使用explode()对数组字段“炸裂”

在这里插入图片描述
然后,我们利用这个explode的结果,来求去重的课程:
select distinct tmp.sub
from
(select explode(subjects) as sub from t_stu_subject) tmp;

表生成函数lateral view

select id,name,tmp.sub
from t_stu_subject lateral view explode(subjects) tmp as sub;
在这里插入图片描述

理解: lateral view 相当于两个表在join
左表:是原表
右表:是explode(某个集合字段)之后产生的表
而且:这个join只在同一行的数据间进行

那样,可以方便做更多的查询:
比如,查询选修了生物课的同学
select a.id,a.name,a.sub from
(select id,name,tmp.sub as sub from t_stu_subject lateral view explode(subjects) tmp as sub) a
where sub=‘生物’;

条件控制函数
sort_array 排序

select sort_array(array(‘2’,‘1’,‘5’)); 1,2,5
size 大小
case when 判断

语法:
case
when 条件 then 结果
else 结果
end
if (条件语句,结果为真,结果为假)

if(age<18,未成年,成年)

窗口分析函数
row_number over 分组排序标记序号

row_number()over(partition by XX order by VV)按XX分区按VV排序

sum()over() 可以实现在窗口中进行逐行累加

sum()over(partition by XX order by VV rows between unbounded preceding and current row)按XX分区按VV排序往前无边界截至到当前行

自定义函数
写好函数打成jar包
在hive中 add jar jar包路径
creat temporary function jar包名 as’类的全路径‘
json解析函数
json_tuple(json,‘k1’,‘k2’…)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值