在日常中hive中最常用的函数:
1.截取字符串:
substr(“你要截取的内容”,开始下标,字段的个数)
select substr(‘abc123456’,3);
select substr(‘abc123456’,3,6);
2.cast 类型转化 == Inteerger.paseInt()
select cast(你想转化的那个字段 as 你需要的那个类型)
select cast(age as int) a1
3.数学运算函数
select round(小数) 四舍五入 取整
select round(小数,保留的小数位个数) 四舍五入
select ceil(小数) 向上取整
select floor(小数) 向下取整
select abs(小数) 取绝对值 (时间可以使用)
4.拼接字符串 Java 中转换字符串 null+“”;
select concat(“你要修改的内容”,“你要添加的内容”);
select concat(name,“–我爱你”) from t_movie;
select concat_ws(“.”,“192”,“168”,“33”,“44”); 192.168.22.44
5.长度
select length(“小明的!a2”)
6.切分
select split(“xiaoming,20,男”,“,”); 返回数组;
7.转大写
upper(string str)
8.转小写
lower(string str)
9.时间----》时间戳
select unix_timestamp(“2019-01-01 01::23::56”,“yyyy-MM-dd HH::mm::ss”);
select unix_timestamp(“618-01-01 01::23::56”,“yyyy-MM-dd HH::mm::ss”);
select unix_timestamp(“2019-01-01 01::23::56:: 203”,“yyyy-MM-dd HH::mm::ss:: SSS”);
10.时间戳—》时间
select from_unixtime(1457680909,“yyyy-MM-dd”);
select from_unixtime(unix_timestamp());
11.条件控制函数: case when
30~60 低速
61~100 中速
101~150 高速
152~ 超速
case
when speed>=30 and speed<=60 then “低速”
when …
end
建立表:
create table t_youyou(id int,name string,age int)
row format delimited fields terminated by ‘,’;
导入数据:
load data local inpath ‘/root/ren.txt’ into table t_person;
select id,name,age,
(case
when age>=18 and age <=25 then “互联网新秀!!!”
when age>=30 and age <=40 then “互联网大佬!!!”
else “互联网老腊肉!!!”
end) info
from t_preson;
12.判断if if(判断体,true,false)
array_contains(“文本内容”,“你要查找的字段”)
name actors times
select name,actors,times,
if(array_contains(actors,“吴京”),“好片儿”,“烂片儿”)
from t_movie;
13.行转列需求:
假如有以下数据:
1,zhangsan,化学:物理:数学:语文
2,lisi,化学:数学:生物:生理:卫生
3,wangwu,化学:语文:英语:体育:生物
4,zhaoliu,数学:物理:化学:英语
转换为:
1,zhangsan,化学
1,zhangsan,物理
1,zhangsan,数学
1,zhangsan,语文
2,lisi,化学
2,lisi,数学
2,lisi,生物
2,lisi,生理
2,lisi,卫生
创建表:
create table t_subject(id int,name string,subjects array)
row format delimited fields terminated by ‘,’
collection items terminated by “:”;
load data local inpath ‘/root/sub.txt’ into table t_subject;
//1.炸裂函数 explode(数组跟集合) ===for
select explode(subjects)subject from t_subject;
//2.行转列函数:表生成函数 lateral view
select id,name,explode(subjects)from t_subject; 天真的想法
select id,name,tmp.sub
from t_subject lateral view explode(subjects) tmp as sub;