Hive built-in functions
function view
show functions;
desc function functionName;
date function
1) Current system time functions: current_date(), current_timestamp(), unix_timestamp()
-- 函数1:current_date();
当前系统日期 格式:"yyyy-MM-dd"
-- 函数2:current_timestamp();
当前系统时间戳: 格式:"yyyy-MM-dd HH:mm:ss.ms"
-- 函数3:unix_timestamp();
当前系统时间戳 格式:距离1970年1月1日0点的秒数。
2) Date to timestamp function: unix_timestamp()
格式:unix_timestamp【死谈破】([date[,pattern]])
案例:
select unix_timestamp('1970-01-01 0:0:0'); -- 传入的日期时间是东八区的时间, 返回值是相对于子午线的时间来说的
select unix_timestamp('1970-01-01 8:0:0');
select unix_timestamp('0:0:0 1970-01-01',"HH:mm:ss yyyy-MM-dd");
select unix_timestamp(current_date());
3) Timestamp to date function: from_unixtime
语法:from_unixtime(unix_time[,pattern])
案例:
select from_unixtime(1574092800);
select from_unixtime(1574096401,'yyyyMMdd');
select from_unixtime(1574096401,'yyyy-MM-dd HH:mm:ss');
select from_unixtime(0,'yyyy-MM-dd HH:mm:ss');
select from_unixtime(-28800,'yyyy-MM-dd HH:mm:ss');
4) Calculate the time difference function: datediff(), months_between()
格式:datediff(date1, date2) - Returns the number of days between date1 and date2
select datediff('2019-11-20','2019-11-01');
格式:months_between(date1, date2) - returns number of months between dates date1 and date2
select months_between('2019-11-20','2019-11-01');
select months_between('2019-10-30','2019-11-30');
select months_between('2019-10-31','2019-11-30');
select months_between('2019-11-00','2019-11-30');
5) Date and time component functions: year(), month(), day(), hour(), minute(), second()
案例:
select year(current_date);
select month(current_date);
select day(current_date);
select year(current_timestamp);
select month(current_timestamp);
select day(current_timestamp);
select hour(current_timestamp);
select minute(current_timestamp);
select second(current_timestamp);
select dayofmonth(current_date);
select weekofyear(current_date)
6) Date positioning function: last_day(), next_day()
--月末:
select last_day(current_date)
--下周
select next_day(current_date,'thursday');
7) Date addition and subtraction functions: date_add(), date_sub(), add_months()
格式:
date_add(start_date, num_days)
date_sub(start_date, num_days)
案例:
select date_add(current_date,1);
select date_sub(current_date,90);
select add_months(current_date,1);
Positioning case:
--当月第1天:
select date_sub(current_date,dayofmonth(current_date)-1)
--下个月第1天:
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1)
- String to date: to_date()
(字符串必须为:yyyy-MM-dd格式)
select to_date('2017-01-01 12:12:12');
9) Date to string (formatting) function: date_format
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_date(),'yyyyMMdd');
select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss');
string functions
lower--(转小写)
select lower('ABC');
upper--(转大写)
select upper('abc');
length--(字符串长度,字符数)
select length('abc');
concat--(字符串拼接)
select concat("A", 'B');
concat_ws --(指定分隔符)
select concat_ws('-','a' ,'b','c');
substr(字符串,什么位置开始,到什么位置结束(相当于是长度))
substring(字符串,什么位置开始,到什么位置结束(相当于是长度))
select substring('abcdefg',0,2); --> 'ab'
split(str,regex)--切分字符串,返回数组。
select split("a-b-c-d-e-f","-");
type conversion function
cast(value as type) -- 类型转换
select cast('123' as int)+1;
math function
round --四舍五入((42.3 =>42))
select round(42.3);
ceil --向上取整(42.3 =>43)
select ceil(42.3);
floor --向下取整(42.3 =>42)
select floor(42.3);
For more exciting content of big data, welcome to search " Qianfeng Education " at station B or scan the code to get a full set of materials
Introduction to Big Data