Hive函数总结
hive有哪些函数?
hive (test_db)> show functions;
查看具体的一个函数
hive (test_db)> desc function concat;
查看具体的一个函数并举例
hive (test_db)> desc function extended concat;
内置函数
简单函数
数学函数
nvl()
round(double d, int n) --返回保留n位小数的近似d值
floor(double d) --向左取整
ceil(double d) --向右取整
rand(int seed) --返回随机数,seed是随机因子
bin(int d) --计算二进制值d的string值
日期函数
current_date() --返回当前日期
date_add() --加减日期
next_day() --取当前天的下个周一
last_day() --取当月最后一天
date_add(next_day(‘2019-02-12’,‘MO’),-7) --取当前周的周一
year(date) --返回日期date的年,类型为int如year(‘2019-01-01’)=2019
month(date) --返回日期date的月,类型为int,如month(‘2019-01-01’)=1
day(date) --返回日期date的天,类型为int,如day(‘2019-01-01’)=1
date_format() --日期格式化
to_date(string timestamp) --返回时间字符串中的日期部分
weekofyear(date1) --返回日期date1位于该年第几周。如weekofyear(‘2019-03-06’)=10
datediff(date1,date2)
返回日期date1与date2相差的天数,如datediff(‘2019-03-06’,‘2019-03-05’)=1
months_between(date1,date2)
返回date1与date2相差月份,如months_between(‘2019-03-06’,‘2019-01-01’)=2
add_months(date1,int1) --返回date1加上int1个月的日期,int1可为负数。
如add_months(‘2019-02-11’,-1)=‘2019-01-11’
trunc(date1,string1):返回日期最开始年份或月份。string1可为年(YYYY/YY/YEAR)或月(MONTH/MON/MM)。如trunc(‘2019-03-06’,‘MM’)=‘2019-03-01’,trunc(‘2019-03-06’,‘YYYY’)=‘2019-01-01’
unix_timestamp():返回当前时间的unix时间戳,可指定日期格式。如unix_timestamp(‘2019-03-06’,‘yyyy-mm-dd’)=1546704180
from_unixtime():返回unix时间戳的日期,可指定格式。如select from_unixtime(unix_timestamp(‘2019-03-06’,‘yyyy-mm-dd’),‘yyyymmdd’)=‘20190306’
条件函数
isnull(a) --若a为null则返回true,否则返回false
if(boolean,t1,t2) --若布尔值成立,则返回t1,反正返回t2。如if(1>2,100,200)返回200
case when boolean then t1 else t2 end --若布尔值成立,则t1,否则t2,可加多重判断
coalesce(v0,v1,v2) --返回参数中的第一个非空值,若所有值均为null,则返回null。
如coalesce(null,1,2)返回1
字符串函数
length(string1) --返回字符串长度
concat(string1,string2) --返回拼接string1及string2后的字符串
concat_ws(sep,string1,string2) --返回按指定分隔符拼接的字符串
lower(string1):返回小写字符串,同lcase(string1)。upper()/ucase():返回大写字符串
trim(string1):去字符串左右空格,ltrim(string1):去字符串左空格。rtrim(string1):去字符串右空格
repeat(string1,int1):返回重复string1字符串int1次后的字符串
reverse(string1):返回string1反转后的字符串。如reverse(‘abc’)返回’cba’
rpad(string1,len1,pad1):以pad1字符右填充string1字符串,至len1长度。如rpad(‘abc’,5,‘1’)返回’abc11’。lpad():左填充
split(string1,pat1):以pat1正则分隔字符串string1,返回数组。如split(‘a,b,c’,’,’)返回[“a”,“b”,“c”]
substr(string1,index1,int1):以index位置起截取int1个字符。如substr(‘abcde’,1,2)返回’ab’
聚合函数
count()统计行数
sum()列求和
avg()
max()列最大值
min()列最小值
集合函数
collect_set() --不同行转为一个集合,可按序号取值