Hive常用函数

Hive 常用函数

[一起学Hive]之二–Hive函数大全-完整版 – lxw的大数据田地 (lxw1234.com)
hive 常用函数大全

关系运算

常用在where条件中

// 等值比较 = == <=>
// 不等值比较 != <>
// 区间比较: select * from default.students where id between 1500100001 and 1500100010;
// 空值/非空值判断:is null、is not null、nvl()、isnull()
// like、rlike、regexp用法

nvl(a,b) -- 如果a为null,返回b;反之返回a本身

rlike -- 使用正则表达式匹配

regexp -- 和rlike一样

数值计算
取整函数(四舍五入):round
向上取整:ceil
向下取整:floor
取随机数:rand
条件函数
  • if: if(表达式,如果表达式成立的返回值,如果表达式不成立的返回值)
select if(1>0,1,0); 

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

select score,if(score>120,'优秀',if(score>100,'良好',if(score>90,'及格','不及格'))) as pingfen from score limit 20;
  • COALESCE 根据你传入的参数,从左向右依次匹配,直到匹配到第一个非空为止
select COALESCE(null,'1','2'); // 1 从左往右 一次匹配 直到非空为止
select COALESCE('1',null,'2'); // 1
  • case when … then … [when ... then ... , ...] else … end
// 写法一
select  score
        ,case when score>120 then '优秀'
              when score>100 then '良好'
              when score>90 then '及格'
        else '不及格'
        end as pingfen
from default.score limit 20;

// 写法二
select  name
        ,case name when "施笑槐" then "槐ge"
                   when "吕金鹏" then "鹏ge"
                   when "单乐蕊" then "蕊jie"
        else "算了不叫了"
        end as nickname
from default.students limit 10;

注意条件的顺序 先后

日期函数

unix_timestamp() 如果什么都不传默认返回当前时间的时间戳

from_unixtime() -- 时间戳→字符串

unix_timestamp() -- 字符串→时间戳

select from_unixtime(1610611142,'YYYY/MM/dd HH:mm:ss');

select from_unixtime(unix_timestamp(),'YYYY/MM/dd HH:mm:ss');

// '2021年01月14日' -> '2021-01-14'
select from_unixtime(unix_timestamp('2021年01月14日','yyyy年MM月dd日'),'yyyy-MM-dd');

// "04牛2021数加16逼" -> "2021/04/16"
select from_unixtime(unix_timestamp("04牛2021数加16逼","MM牛yyyy数加dd逼"),"yyyy/MM/dd");
字符串函数

concat('abc','cdf) → abcdf

concat_ws('|','abc','cdg') → abc|cdg 可以指定分隔符,并且会自动忽略NULL

substring('2021/01/14',1,4) → 14 HQL中涉及到位置的时候 是从1开始计数

split("abcde,fgh",",") → ["abcde","fgh"] 可以根据下标获取元素

explode() → 将数据扁平化,行转列

get_json_object() → 解析json格式的数据

cast(id as string) → 将id转为string类型

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 concat_ws("|",cast(id as string),name,cast(age as string),gender,clazz) from students limit 10;

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

// '2021/01/14' -> '2021-01-14'
select concat_ws("-",substring('2021/01/14',1,4),substring('2021/01/14',6,2),substring('2021/01/14',9,2));

// 建议使用日期函数去做日期
select from_unixtime(unix_timestamp('2021/01/14','yyyy/MM/dd'),'yyyy-MM-dd');

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

select explode(split("abcde,fgh",",")); // abcde
										//  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
// $ 表示json本身
// $.score[0] 获取score数组中的第一个元素
// $.score[0].score 获取sore数组中的第一个元素中的score
posted @ 2022-02-20 10:00  赤兔胭脂小吕布  阅读(151)  评论(0编辑  收藏  举报