文章目录
一、函数
1.1 函数简介
Hive会将常用的逻辑封装成函数给用户进行使用,类似于Java中的函数。
好处:避免用户反复写逻辑,可以直接拿来使用。
Hive提供了大量的内置函数,按照其特点可大致分为如下几类:单行函数、聚合函数、炸裂函数、窗口函数。
- 以下命令可用于查询所有内置函数的相关信息:
1)查看系统内置函数
show functions;
2)查看内置函数用法
desc function upper;
3)查看内置函数详细信息
desc function extended upper;
1.2 单行函数
单行函数的特点是一进一出,即输入一行,输出一行。
单行函数按照功能可分为如下几类: 日期函数、字符串函数、集合函数、数学函数、流程控制函数等。
1.2.1 算术运算函数
| 运算符 | 描述 |
|---|---|
| A+B | A和B 相加 |
| A-B | A减去B |
| A*B | A和B 相乘 |
| A/B | A除以B |
| A%B | A对B取余 |
| A&B | A和B按位取与 |
| A|B | A和B按位取或 |
| A^B | A和B按位取异或 |
| ~A | A按位取反 |
1.2.2 数值函数
1)round:四舍五入
select round(3.3); 3
2)ceil:向上取整
``select ceil(3.1) ; 4
3)floor:向下取整
select floor(4.8); 4
1.2.3 字符串函数
1)substring:截取字符串
语法一:substring(string A, int start)
返回值:string
说明:返回字符串A从start位置到结尾的字符串
语法二:substring(string A, int start, int len)
返回值:string
说明:返回字符串A从start位置开始,长度为len的字符串
(1)获取第二个字符以后的所有字符
select substring("shanghai",2);
输出: hanghai
(2)从第3个字符开始,向后获取2个字符
select substring("shanghai",3,2);
输出: an
2)replace :替换
语法:replace(string A, string B, string C)
返回值:string
说明:将字符串A中的子字符串B替换为C。
3)regexp_replace:正则替换
语法:regexp_replace(string A, string B, string C)
返回值:string
说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符。
select regexp_replace('100-200', '(\\d+)', 'num')
输出:num-num
4)regexp:正则匹配
语法:字符串 regexp 正则表达式
返回值:boolean
说明:若字符串符合正则表达式,则返回true,否则返回false。
(1)正则匹配成功,输出true
select 'dfsaaaa' regexp 'dfsa+'
输出:true
(2)正则匹配失败,输出false
select 'dfsaaaa' regexp 'dfsb+';
输出:false
5)repeat:重复字符串
语法:repeat(string A, int n)
返回值:string
说明:将字符串A重复n遍。
6)split :字符串切割
语法:split(string str, string pat)
返回值:array
说明:按照正则表达式pat匹配到的内容分割str,分割后的字符串,以数组的形式返回。
7)nvl :替换null值
语法:nvl(A,B)
说明:若A的值不为null,则返回A,否则返回B。
select nvl(null,1);
输出:1
8)concat :拼接字符串
语法:concat(string A, string B, string C, ……)
返回:string
说明:将A,B,C……等字符拼接为一个字符串
select concat('beijing','-','shanghai','-','shenzhen');
输出:beijing-shanghai-shenzhen
9)concat_ws:以指定分隔符拼接字符串或者字符串数组
语法:concat_ws(string A, string…| array(string))
返回值:string
说明:使用分隔符A拼接多个字符串,或者一个数组的所有元素。
select concat_ws('-','beijing','shanghai','shenzhen');
输出:beijing-shanghai-shenzhen
10)get_json_object:解析json字符串
语法:get_json_object(string json_string, string path)
返回值:string
说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。
(1)获取json数组里面的数据
select get_json_object('[{"name":"大海","sex":"男","age":"25"},{"name":"小张","sex":"男","age":"47"}]','$.[0]');
输出:{
"name":"大海","sex":"男","age":"25"}
1.2.4 日期函数
1)unix_timestamp:返回当前或指定时间的时间戳
语法:unix_timestamp()
返回值:bigint
select unix_timestamp('2022/08/08 08-08-08','yyyy/MM/dd HH-mm-ss');
输出:1659946088
-- 说明:-前面是日期后面是指,日期传进来的具体格式
2)from_unixtime:转化UNIX时间戳(从 1970-01-01 00:00:00 UTC 到指定时间的秒数)到当前时区的时间格式
语法:from_unixtime(bigint unixtime[, string format])
返回值:string
select from_unixtime(1659946088);
输出:2022-08-08 08:08:08
3)current_date:当前日期
select current_date;
输出:2024-01-01
4)current_timestamp:当前的日期加时间,并且精确的毫秒
select current_timestamp;
输出:2024-01-01 15:32:22.402
5)month:获取日期中的月
语法:month (string date)
返回值:int
select month('2022-08-08 08:08:08');
输出:8
6)day:获取日期中的日
语法:day (string date)
返回值:int
select day('2022-08-08 08:08:08')
输出:8
7)hour:获取日期中的小时
语法:hour (string date)
返回值:int
select hour('2022-08-08 08:08:08');
输出:8
8)datediff:两个日期相差的天数(结束日期减去开始日期的天数)
语法:datediff(string enddate, string startdate)
返回值:int
select datediff('2021-08-08','2022-10-09');
输出:-427
9)date_add:日期加天数
语法:date_add(string startdate, int days)
返回值:string
说明:返回开始日期 startdate 增加 days 天后的日期
select date_add('2022-08-08',2);
输出:2022-08-10
10)date_sub:日期减天数
语法:date_sub (string startdate, int days)
返回值:string
说明:返回开始日期startdate减少days天后的日期。
select date_sub('2022-08-08',2);
输出:2022-08-06
11)date_format:将标准日期解析成指定格式字符串
select date_format('2022-08-08','yyyy年-MM月-dd日')
输出:2022年-08月-08日
1.2.5 流程控制函数
1)case when:条件判断函数
语法一:case when a then b [when c then d]* [else e] end
返回值:T
说明:如果a为true,则返回b;如果c为true,则返回d;否则返回 e
语法二: case a when b then c [when d then e]* [else f] end
返回值: T
说明:如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f
2)if: 条件判断,类似于Java中三元运算符
语法:if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值:T
说明:当条件testCondition为true时,返回valueTrue;否则返回valueFalseOrNull
(1)条件满足,输出正确
select if(10 > 5,'正确','错误');
输出:正确
(2)条件满足,输出错误
select if(10 < 5,'正确','错误');
输出:错误
1.2.6 集合函数
1)size:集合中元素的个数
select size(friends) from test


6309

被折叠的 条评论
为什么被折叠?



