hive 函数

数学函数
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->-4floor(-10.2)-11
ceil(DOUBLE a), ceiling(DOUBLE a)求其不小于小给定实数的最小整数如:ceil(6) = ceil(6.1)= ceil(6.9) = 6ceil(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类型,如果转换失败将返回NULLselect 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加上daysselect date_add(‘2008-12-3’, 1)2008-12-04
date_sub(string startdate, int days)从开始时间startdate减去daysselect 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)按指定格式返回时间dateselect 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,否则返回valueselect 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 end1
isnull( a )如果a为null就返回true,否则返回falseisnull(null)true
isnotnull ( a )如果a为非null就返回true,否则返回falseisnotnull(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则必须指定第三个参数keyselect 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次字符串strselect 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值