目录
一、Hive函数分类
Hive的函数分为两大类:内置函数(Built_in Functions)用户自定义函数UDF(User_Defined),Hive函数总计有100多种,详细内容可前往 官方文档查看。
(1)查看Hive可用函数
命令:show functions;
(2)查看函数具体使用方法
命令:describe function extended 函数名;
二、数值函数(部分)
(1)取整函数(round)
取整:select round() 返回double类型的整数数值部分
select round(3.1415926);
指定精度取整:select round(double a,int b)
select round(3.1415926,4);
(2)随机数函数(rand)
取随机数函数:select rand() 返回一个0-1范围的随机数
select rand();
指定种子取随机数函数:select rand(int seed) 得到一个稳定的随机数序列
select rand(3);
(3)绝对值(abs)
select abs(-3);
(4)Π值
pi()得到小数点后15位精度
select pi();
三、集合函数
ReturnType | Name(signature) | Description |
int | size(Map<k.v>) | 返回map类型的元素个数 |
int | size(Array<T>) | 返回array类型的元素个数 |
array<k> | map_keys(Map<k.v>) | 返回map内的全部key(得到的是array) |
array<v> | map_values(Map<k.v>) | 返回map内的全部value(得到的是array) |
boolean | array_contains(Array<T>,value) | 如果array包含指定value,返回true |
array<t> | sort_array(Array<T>) | 根据数值元素的自然顺序按升序对输入数组进行排序并返回它 |
(1)返回array元素个数
(2)返回map类型元素个数
(3)返回map类型的key![](https://i-blog.csdnimg.cn/blog_migrate/919610b1c4df7363a74589a95f85324d.png)
(4)返回map类型的value![](https://i-blog.csdnimg.cn/blog_migrate/219f349830f8c06860df92c4dd933cd2.png)
(5)查找指定value是否在array中![](https://i-blog.csdnimg.cn/blog_migrate/b69e21504035a9db5d441c7a7a2f46e2.png)
(6)排序![](https://i-blog.csdnimg.cn/blog_migrate/3d0b9dc5cdd1a9b5b2c3c103393da782.png)
四、类型转换函数![](https://i-blog.csdnimg.cn/blog_migrate/1b3c57f32885bac17754ed8f78053337.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b1d593aa920a6cab269da367e3484831.png)
![](https://i-blog.csdnimg.cn/blog_migrate/61b51dcda3a5e6d4997794e959801ce1.png)
五、日期函数
Return Type | Name(Signature) | Description |
timestamp | current_timestamp() | 返回当前时间戳。在同一个查询中对 current _ time 戳的所有调用都返回相同的值。 |
date | current_date | 返回当前日期。在同一个查询中对 current_date 戳的所有调用都返回相同的值。 |
2.1.0版本前返回string,现在版本返回date | to_date(string timestamp) | 时间戳转日期 |
int | year(string date) quarter(date/timestamp/string) month(string date) day(string date) dayofmonth(date) hour(string date) minute(string date) second(string date) weekofyear(string date) | 得到给定时间的:年 得到给定时间的:季度 得到给定时间的:月 得到给定时间的:日 得到给定时间的:当前月份第几天 得到给定时间的:小时 得到给定时间的:分钟 得到给定时间的:秒 得到给定时间的:本年第几周 |
int | datediff(string enddate,string startdate) | 返回 enddate 到 startdate 之间的天数 |
2.1.0版本前返回string,现在版本返回date | date_add(date/timestamp/string startdate, tinyint/smallint/int days) date_sub(date/timestamp/string startdate, tinyint/smallint/int days) | 日期相加 : date_add('2008-12-31', 1) = '2009-01- |
(1)获取当前日期![](https://i-blog.csdnimg.cn/blog_migrate/20541230b13ddaab77cfa167c1e908fc.png)
(2)时间戳![](https://i-blog.csdnimg.cn/blog_migrate/bbc0e93f37144f75791d9116e8e80de2.png)
(3)时间戳转日期![](https://i-blog.csdnimg.cn/blog_migrate/15c1266eb9db0340a6fc03c7d70e35a3.png)
(4)年月日季度
1.获取年
2.获取季度
3.获取小时
4.获取是该月的第几天![](https://i-blog.csdnimg.cn/blog_migrate/0728bee5b8b7f3484acb75f8b93c33f6.png)
5.获取该周是本年的第几周![](https://i-blog.csdnimg.cn/blog_migrate/208f27702ce23befebe1ccc3c4f4d892.png)
(5)日期之间的天数![](https://i-blog.csdnimg.cn/blog_migrate/b572e1b7de5e9cd148b58bda79d207d7.png)
(6)日期相加、相减![](https://i-blog.csdnimg.cn/blog_migrate/415ab3794897f81987e4156035d472ed.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4d767cc12d65bad3cfebc660a5f6d5e5.png)
六、条件函数
Return Type | Name(Signature) | Description |
T | f(boolean testCondition, T valueTrue, T valueFalseOrNull) | 如果 testCondition 为 true ,则返回 valueTrue ,否则返回 valueFalseOrNull 。 |
boolean | isnull( a ) | 如果 a 为 NULL ,则返回 true ,否则返回 false 。 |
boolean | snotnull ( a ) | 如果 a 不为 NULL ,则返回 true ,否则返回 false |
T | nvl(T value, T default_value) | 如果 value 为 null ,则返回 default_value ,否则 value 。 |
T | COALESCE(T v1, T v2, ...) | 返回第一个不是 NULL 的 v ,如果所有 v 都是 NULL ,则返回 NULL 。 |
T | CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END | 当 a = b 时,返回 c; [ 当 a = d 时,返回 e]* ; 否则返回 f 。 |
T | CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END | When a = true, returns b; when c = true, returns d; else returns e. a 可以是表达式,如 1=1 |
T | nullif( a, b ) | 如果 a=b ,则返回 NULL; 否则返回 a 。 等价: CASE WHEN a = b then NULL else a |
void | assert_true(boolean condition) | 如果 boolean_condition 结果不为 True ,则引发异常报错 |
七、字符串函数
Return Type | Name(Signature) | Description |
string | concat(string|binary A, string|binary B...) | 连接字符串 For example, concat('foo', 'bar') results in 'foobar' |
string | concat_ws(string SEP, string A, string B...) | 同concat,但是可以自己定义字符串之间的分隔符(SEP) |
int | length(string A) | 字符串长度 |
string | lower(string A) upper(string a) | 返回从 A 的两端裁剪空格得到的字符串。例如 , trim(‘ foobar ’) 的结果是‘ foobar’ |
string | trim(string A) | 返回从A的两端裁剪空格符得到的字符串。例如,trim(‘ foobar ’)的结果是‘foobar’ |
array | split(string str,string pat) | 按照pat分隔字符串,pat是正则表达式 |
(1)连接字符串![](https://i-blog.csdnimg.cn/blog_migrate/c01e4cd62c4c03ceb20a7e8196a11800.png)
(2)统计长度![](https://i-blog.csdnimg.cn/blog_migrate/d1716bcc7beb2782a33cc1a97eae1b9e.png)
(3)转大小写![](https://i-blog.csdnimg.cn/blog_migrate/ce65a0458e96318528776d2e773ef87e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e7a2ddd7087d89a290a6392eb249e75b.png)
(4)去除首位空格![](https://i-blog.csdnimg.cn/blog_migrate/d9907bbed4b01b16458dcfcb731a9d14.png)
(5)字符串分隔![](https://i-blog.csdnimg.cn/blog_migrate/784c0ae9d449b6e4044dff13ce55b497.png)
八、数据脱敏函数
Return Type | Name(Signature) | Description |
string | mask_hash( string | char | varchar str) | 对字符串进行hash加密(结果为字符串),非字符串加密会得到null |
九、其他函数
Return Type | Name(Signature) | Description |
int | hash(a1[,a2...]) | 返回参数的hash数字(结果为数字) |
string | current_user() | 返回登陆用户 |
string | current_database() | 返回当前选择的数据库 |
string | version() | 返回当前Hive版本 |
string | md5(string/binary) | 返回给定参数的MD5值 |