Hive的常用函数

目录

1:关系运算

2:数值计算

3:条件函数

4:日期函数

 5:字符串函数

 6:Hive 中的wordCount


1:关系运算

 等值比较 = == <=>
不等值比较 != <>
区间比较: between   and 
空值/非空值判c  is null 或者 is not null

2:数值计算

取整函数(四舍五入):round
向上取整:ceil
向下取整:floor

3:条件函数

(1: if

 if(表达式,如果表达式成立的返回值,如果表达式不成立的返回值)

select if(1>0,1,0); 
select if(1>0,if(-1>0,-1,1),0);

(2:COALESCE

select COALESCE(null,'1','2'); // 1 从左往右 一次匹配 直到非空为止
select COALESCE('1',null,'2'); // 1

(3:case when

select  score
        ,case when score>120 then '优秀'
              when score>100 then '良好'
              when score>90 then '及格'
        else '不及格'
        end as pingfen
from score limit 20;

4:日期函数

select from_unixtime(1610611142,'YYYY/MM/dd HH:mm:ss');//将时间戳函数按照指定格式进行转换

select from_unixtime(unix_timestamp(),'YYYY/MM/dd HH:mm:ss');//将当前时间按照指定的格式进行转换
select from_unixtime(unix_timestamp("04新年2021快乐16","MM新年yyyy快乐dd"),"yyyy/MM/dd");在对应的位置可以将时间转换为指定的格式

 5:字符串函数

concat('123','456'); // 123456
concat('123','456',null); // NULL

select concat_ws('#','a','b','c'); // a#b#c
select concat_ws('#','a','b','c',NULL); // a#b#c 可以指定分隔符,并且会自动忽略NULL

select substring("abcdefg",1); // abcdefg HQL中涉及到位置的时候 是从1开始计数

select split("abcde,fgh",","); // ["abcde","fgh"]
select split("a,b,c,d,e,f",",")[2]; // c

select explode(split("abcde,fgh",",")); // a  //  fgh

// 解析json格式的数据
select get_json_object('{"name":"zhangsan","age":18,"score":[{"course_name":"math","score":100},{"course_name":"english","score":60}]}',"$.score[0].score");

//100

 6:Hive 中的wordCount

create table words(
    words string
)row format delimited fields terminated by '|';

// 数据
hello,java,hello,java,scala,python
hbase,hadoop,hadoop,hdfs,hive,hive
hbase,hadoop,hadoop,hdfs,hive,hive

select word,count(*) from (select explode(split(words,',')) word from words) a group by a.word;

// 结果
hadoop    4
hbase    2
hdfs    2
hello    2
hive    4
java    2
python    1
scala    1

 


版权声明:本文为weixin_50691399原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。