温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

hive常用内部函数有哪些

发布时间:2021-12-10 14:24:35 来源:亿速云 阅读:255 作者:小新 栏目:大数据

这篇文章主要介绍了hive常用内部函数有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1、随机函数rand()

格式:rand([int seed])返回:double-- 取0-1的随机值select rand();-- 指定随机函数的种子seed,该随机会返回一个固定值select rand(100);

2、切分函数split()

格式:split(str,spliter)返回:array-- 获取随机数*100,然后再取整。小数点.需要转义select split(rand()*100,'\\.')[0];

3、字符串截取函数substring() 或 substr()

格式:substring(str,start,length)      substr(str,start,length)返回:string-- 获取随机数*100,然后再从0位置开始,取2位字符串。select substring(rand()*100,0,2);-- 获取随机数*100,然后再从0位置开始,取2位字符串。select substr(rand()*100,0,2);

4、判断函数if()

格式:if(condition,true,false)返回:true或者flase部分的值-- 查询s_tmp表,如果s.sex等于1,则是男,否则是女selects.id,s.name,if(s.sex = 1,'男','女')from s_tmp s;-- if嵌套查询selects.id,s.name,if(s.id = 1,'男',if(s.id = 2,'女','妖'))from s_tmp s;

5、选择函数case when

类似于java中的swith。比if函数更加的具有扩展性。格式:case 值when 1 then ''...elseend返回:then或者else后的值格式2:casewhen 值=then ''...elseend返回:then或者else后的值-- 查询s_tmp中的s.sex,如果为1,则是男,为2则是女,其它为妖selects.id,s.name,case s.sexwhen 1 then '男'when 2 then '女'else '妖'endfrom s_tmp s;-- 查询s_tmp中的s.sex,如果为1,则是男,为2则是女,其它为妖selects.id,s.name,casewhen s.sex=then '男'when s.sex=then '女'else '妖'endfrom s_tmp s;

6、正在替换函数regexp_replace()

格式:regexp_replace(str,old_string,new_str)   #old_string支持通配符返回:string-- 将.png替换为.jpgselect regexp_replace('1.png','.png','.jpg');-- 将s.name的名字为zhangsan的替换为lisiselects.id,regexp_replace(s.name,'zhangsan','lisi')from s_tmp s;

7、类型转换 函数cast()

格式: cast(x as type)返回:type类型-- 将1.0转换成int类型select cast(1.0 as int);-- 将随机数*100,转换成int类型select cast(rand()*100 as int);

8、四舍五入函数round()

格式:round(double,保留位数)返回:double-- 随机数*100,然后四舍五入取值。没有保留位数默认四舍五入取整,比如0.0 或者 1.0select round(rand()*100);-- 随机数*100,然后保留两位小数,四舍五入取值select round(rand()*100,2);

9、连接函数concat() 或者 concat_ws()

格式:concat(str1,str2...) 或者 concat_ws(split_str,str1,str2....)返回:string-- 将字符串1,2拼接起来select concat("1","2");-- 将字符串1,2拼接起来,并使用|来进行分割select concat_ws("|","1","3","2");-- 将id,name,sex使用|进行拼接selectconcat_ws('|',cast(s.id as string),s.name,cast(s.sex as string))from s_tmp s;

10、字符串长度函数length()

格式:length(str) 返回:int-- 获取name的长度selectlength(s.name)from s_tmp s;

感谢你能够认真阅读完这篇文章,希望小编分享的“hive常用内部函数有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI