| 数学函数 | |||
|---|---|---|---|
| round(DOUBLE a) | .返回对a四舍五入的BIGINT值 | select round(10.235) | 10 |
| round(DOUBLE a, INT d) | 返回DOUBLE型d的保留n位小数的DOUBLW型的近似值 | select round(5.635,2) | 5.64 |
| floor(DOUBLE a) | 向下取整,最数轴上最接近要求的值的左边的值 如:6.10->6 -3.4->-4 | floor(-10.2) | -11 |
| ceil(DOUBLE a), ceiling(DOUBLE a) | 求其不小于小给定实数的最小整数如:ceil(6) = ceil(6.1)= ceil(6.9) = 6 | ceil(10.2) | 11 |
| rand(), rand(INT seed) | 每行返回一个DOUBLE型随机数seed是随机因子 | rand() | 0.45345312 |
| exp(DOUBLE a), exp(DECIMAL a) | 返回e的a幂次方, a可为小数 | exp(3) | 2.718 |
| pow(DOUBLE a, DOUBLE p), power(DOUBLE a, DOUBLE p) | 计算a的p次幂 | select pow (3,4) | 81 |
| sqrt(DOUBLE a), sqrt(DECIMAL a) | 计算a的平方根 | select sqrt (-10) | Null |
| abs(DOUBLE a) | 计算a的绝对值 | abs(-4) | 4 |
| pmod(INT a, INT b), pmod(DOUBLE a, DOUBLE b) | a对b取模 取余数 | pmod(7,5) | 2 |
| 类型转换 | |||
| cast(expr as ) | 将expr转换成type类型 如:cast(“1” as BIGINT) 将字符串1转换成了BIGINT类型,如果转换失败将返回NULL | select cast(“1” as Int) | 1 |
| 日期 | |||
| from_unixtime(bigint unixtime[, string format]) | 将时间的秒值转换成format格式 | select from_unixtime(unix_timestamp(),“yyyy-MM-dd HH:mm:ss”) | 2020-07-13 14:43:16 |
| unix_timestamp() | 获取本地时区下的时间戳 | ||
| unix_timestamp(string date) | **将格式为yyyy-MM-dd HH:mm:ss的时间字符串转换成时间戳 ** | select unix_timestamp(‘2020-07-13 14:43:16’) | |
| to_date(string timestamp) | 返回时间字符串的日期部分 | to_date(“1970-1-1 23:23:23”) | 1970-01-01 |
| year(string date) | 返回时间字符串的年份部分 | select year(‘1989-10-02 2:4:3’) | 1989 |
| quarter(date/timestamp/string) | **返回当前时间属性哪个季度 ** | quarter(‘2020-04-08’) | 2 |
| month(string date) | 返回时间字符串的月份部分 | select month(‘1989-10-02 2:4:3’) | 10 |
| day(string date) dayofmonth(date) | 返回时间字符串的天 | select day(‘1989-10-02 2:4:3’) | 2 |
| hour(string date) | 返回时间字符串的小时 | select hour(‘1989-10-02 2:4:3’) | 2 |
| minute(string date) | 返回时间字符串的分钟 | select minute(‘1989-10-02 2:4:3’) | 4 |
| second(string date) | 返回时间字符串的秒 | select second(‘1989-10-02 2:4:3’) | 3 |
| weekofyear(string date) | 返回时间字符串位于一年中的第几个周内 | select weekofyear(current_date()) | 29 |
| datediff(string enddate, string startdate) | 计算开始时间startdate到结束时间enddate相差的天数 | select datediff(current_date(),‘2020-5-13’) | 61 |
| date_add(string startdate, int days) | 从开始时间startdate加上days | select date_add(‘2008-12-3’, 1) | 2008-12-04 |
| date_sub(string startdate, int days) | 从开始时间startdate减去days | select date_sub(‘2008-12-3’, 1) | 2008-12-02 |
| current_date | 返回当前时间日期 | select current_date() | 2020-07-13 |
| current_timestamp | 返回当前时间 | select current_timestamp() | 2020-07-13 19:49:00.154 |
| add_months(string start_date, int num_months) | 返回当前时间下再增加num_months个月的日期 | add_months(current_date(),-3) | 2020-04-13 |
| last_day(string date) | 返回这个月的最后一天的日期,忽略时分秒部分(HH:mm:ss) | last_day(current_date()) | 2020-07-31 |
| dayofweek (current_date()) | 当前 本周第几天 | dayofweek (current_date()) | 4 |
| datediff(next_day(current_date(),‘su’),current_date() | 当前 本周星期几 | select 7-datediff(next_day(current_date(),‘SU’),current_date()) | 3 |
| next_day(string start_date, string day_of_week) | 返回当前时间的下一个星期X所对应的日期 | select next_day(‘2015-01-14’, ‘TU’) | 2015-01-20 |
| trunc(string date, string format) | .返回时间的最开始年份或月份 | select trunc(current_date(),‘YY’) | 2020-01-01 |
| months_between(date1, date2) | 返回date1与date2之间相差的月份 | select months_between(current_date (),‘2020-05-10’) | 2.09677419 |
| date_format(date/timestamp/string ts, string fmt) | 按指定格式返回时间date | select date_format(current_date(),‘MM.dd’) | 07.13 |
| 条件函数 | |||
| if(boolean testCondition, T valueTrue, T valueFalseOrNull) | 如果第一个表达式为true就返回第二个表达式,否则返回第三个表达式 | select if(true,‘aaa’,‘bbb’) | aaa |
| nvl(T value, T default_value) | 如果value值为NULL就返回default_value,否则返回value | select nvl(null,10)select nvl(null,10) | 10 |
| COALESCE(T v1, T v2, …) | **返回第一非null的值,如果全部都为NULL就返回NULL ** | select coalesce(null,‘aaa’,‘bbb’) | aaa |
| CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END | **如果a=ture就返回b,c= ture就返回d,否则返回e ** | select case when ‘1990-1-1’<‘2000-1-1’ then 1 else 0 end | 1 |
| isnull( a ) | 如果a为null就返回true,否则返回false | isnull(null) | true |
| isnotnull ( a ) | 如果a为非null就返回true,否则返回false | isnotnull(null) | False |
| 字符函数 | |||
| ascii(string str) | 返回str中首个ASCII字符串的整数值 | ascii(‘a’) | 97 |
| concat(string|binary A, string|binary B…) | 对二进制字节码或字符串按次序进行拼接 | select concat(‘a’,‘b’,‘c’) | abc |
| concat_ws(string SEP, string A, string B…) | 与concat()类似,但使用指定的分隔符喜进行分隔 | select concat_ws(’,’,‘a’,‘b’,‘c’) | a,b,c |
| find_in_set(string str, string strList) | 返回以逗号分隔的字符串中str出现的位置,如果参数str为逗号或查找失败将返回0,如果任一参数为NULL将返回NULL回 | select find_in_set(‘ad’, ‘asd,ad,asd,adadad,sds’) | 2 |
| format_number(number x, int d) | 将数值X转换成"#,###,###.##"格式字符串,并保留d位小数,如果d为0,将进行四舍五入且不保留小数 | select format_number(123525235.1241,2) | 123,525,235.12 |
| get_json_object(string json_string, string path) | 从指定路径上的JSON字符串抽取出JSON对象,并返回这个对象的JSON格式,如果输入的JSON是非法的将返回NULL,注意此路径上JSON字符串只能由数字 字母 下划线组成且不能有大写字母和特殊字符,且key不能由数字开头,这是由于Hive对列名的限制 | select get_json_object(’{“userid”:“1”,“username”:“zs”}’,"$.username") | zs |
| instr(string str, string substr) | 查找字符串str中子字符串substr出现的位置,如果查找失败将返回0,如果任一参数为Null将返回null,注意位置为从1开始的 | select instr(“abcdef”,“ef”) | 5 |
| length(string A) | 返回字符串的长度 | ||
| locate(string substr, string str[, int pos]) | 查找字符串str中的pos位置后字符串substr第一次出现的位置 | locate(‘ef’,‘abcefdef’,1) | 4 |
| lower(string A) lcase(string A) | 将字符串A的所有字母转换成小写字母 | ||
| lpad(string str, int len, string pad) | 从左边开始对字符串str使用字符串pad填充,最终len长度为止,如果字符串str本身长度比len大的话,将去掉多余的部分 | lpad(“asdasd”,8,“aaaaa”) | aaasdasd |
| ltrim(string A) | 去掉字符串A前面的空格 | ||
| parse_url(string urlString, string partToExtract [, string keyToExtract]) | 返回从URL中抽取指定部分的内容,参数url是URL字符串,而参数partToExtract是要抽取的部分,这个参数包含(HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO,例如:parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1’, ‘HOST’) =‘facebook.com’,如果参数partToExtract值为QUERY则必须指定第三个参数key | select parse_url(‘http://baidu.com/path/p1.php?name=1&pwd=123’,‘PROTOCOL’). select parse_url(‘http://baidu.com/path/p1.php?name=1&pwd=123’,‘HOST’) select parse_url(‘http://baidu.com/path/p1.php?name=1&pwd=123’,‘QUERY’) select parse_url(‘http://baidu.com/path/p1.php?name=1&pwd=123’,‘QUERY’,‘pwd’) | http baidu.com name=1&pwd=123 123 |
| regexp_extract(string subject, string pattern, int index) | 抽取字符串subject中符合正则表达式pattern的第index个部分的子字符串,注意些预定义字符的使用,如第二个参数如果使用’\s’将被匹配到s,’\s’才是匹配空格 | ```select regexp_extract(‘holle,world’,’(\w+),(\w+)’,2); | world |
| regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT) | 按照Java正则表达式PATTERN将字符串INTIAL_STRING中符合条件的部分成REPLACEMENT所指定的字符串,如里REPLACEMENT这空的话,抽符合正则的部分将被去掉 | select regexp_replace(‘holle,world’,‘o’,‘e’); | helle,werld |
| repeat(string str, int n) | 重复输出n次字符串str | select repeat(‘abc’,3); | abcabcabc |
| reverse(string A) | 反转字符串 | ||
| rpad(string str, int len, string pad) | 从右边开始对字符串str使用字符串pad填充,最终len长度为止,如果字符串str本身长度比len大的话,将去掉多余的部分 | ||
| rtrim(string A) | 去掉字符串后面出现的空格 | ||
| split(string str, string pat) | 按照正则表达式pat来分割字符串str,并将分割后的数组字符串的形式返回 | select split('abc|cde,def kyy','[\\|, ]'); | [“abc”,“cde”,“def”,“kyy”] |
| substr(string|binary A, int start) substring(string|binary A, int start) | 对于字符串A,从start位置开始截取字符串并返回 | select substr(‘hello,world’,2); | ello,world |
| translate(string|char|varchar input, string|char|varchar from, string|char|varchar to) | 将input出现在from中的字符串替换成to中的字符串 | select translate(‘hello,world’,‘world’,‘cc’); | hec,cc |
| trim(string A) | 将字符串A前后出现的空格去掉 | ||
| upper(string A) ucase(string A) | 将字符串A中的字母转换成大写字母 | ||
| initcap(string A) | 将字符串A转换第一个字母大写其余字母的字符串 | select initcap(‘abc def’); | Abc Def |
| 聚合函数 | |||
| count(*), count(expr), count(DISTINCT expr[, expr…]) | 统计总行数,包括含有NULL值的行统计提供非NULL的expr表达式值的行数统计提供非NULL且去重后的expr表达式值的行数 | ||
| sum(col), sum(DISTINCT col) | sum(col),表示求指定列的和,sum(DISTINCT col)表示求去重后的列的和 | ||
| avg(col), avg(DISTINCT col) | avg(col),表示求指定列的平均值,avg(DISTINCT col)表示求去重后的列的平均值 | ||
| min(col) | 求指定列的最小值 | ||
| max(col) | 求指定列的最大值 | ||
| 表生成函数 | |||
| explode(array<TYPE> a) | 对于a中的每个元素,将生成一行且包含该元素 | select explode(split(“abc,cdf,sda”,"[, ]")); | abc cdf sda |
hive 函数
最新推荐文章于 2024-03-27 18:35:55 发布
1576

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



