一、查看hive中函数的使用方法

1.查看内置的函数

hive> show functions;

date格式转换 hive hive date转字符串string_hive


2.查看某一类函数

例如:查看和年相关的函数

hive> show functions like '*year*';

date格式转换 hive hive date转字符串string_hive_02


3.查看函数的使用方法,命令:desc function 函数名,带上关键字extended可查看更详细的使用方法。

hive> desc function avg;

date格式转换 hive hive date转字符串string_大数据_03

hive> desc function extended avg;

date格式转换 hive hive date转字符串string_大数据_04


4.查看官网(https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-Built-inFunctions),了解函数的使用方法。

date格式转换 hive hive date转字符串string_hive_05


二、日期相关的函数

日期时间转日期函数: to_date

语法: to_date(string timestamp)

返回值: string

说明: 返回日期时间字段中的日期部分。

举例:

hive> select to_date(‘2011-12-08 10:03:01’);

2011-12-08

hive> select to_date(‘2011-12-08’);

2011-12-08

UNIX时间戳转日期函数: from_unixtime
语法: from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive> select from_unixtime(1323308943,‘yyyyMMdd’) ;
20111208

获取当前UNIX时间戳函数: unix_timestamp
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
hive> select unix_timestamp() from dual;
1323309615

日期转UNIX时间戳函数: unix_timestamp
语法: unix_timestamp(string date)
返回值: bigint
说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp(‘2011-12-07 13:01:03’) ;
1323234063

指定格式日期转UNIX时间戳函数: unix_timestamp
语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp(‘20111207 13:01:03’,‘yyyyMMdd HH:mm:ss’) from dual;
1323234063

date_format:将日期进行格式化
select date_format(‘2017-01-01’,‘yyyy-MM-dd HH:mm:ss’);
–日期字符串必须满足yyyy-MM-dd格式

日期转年函数: year
语法: year(string date)
返回值: int
说明: 返回日期中的年。
举例:
hive> select year(‘2011-12-08 10:03:01’) from dual;
2011
hive> select year(‘2012-12-08’) from dual;
2012

日期转月函数: month
语法: month (string date)
返回值: int
说明: 返回日期中的月份。
举例:
hive> select month(‘2011-12-08 10:03:01’) from dual;
12
hive> select month(‘2011-08-08’) from dual;
8

日期转天函数: day
语法: day (string date)
返回值: int
说明: 返回日期中的天。
举例:
hive> select day(‘2011-12-08 10:03:01’) from dual;
8
hive> select day(‘2011-12-24’) from dual;
24

日期转小时函数: hour
语法: hour (string date)
返回值: int
说明: 返回日期中的小时。
举例:
hive> select hour(‘2011-12-08 10:03:01’) from dual;
10

日期转分钟函数: minute
语法: minute (string date)
返回值: int
说明: 返回日期中的分钟。
举例:
hive> select minute(‘2011-12-08 10:03:01’) from dual;
3

日期转秒函数: second
语法: second (string date)
返回值: int
说明: 返回日期中的秒。
举例:
hive> select second(‘2011-12-08 10:03:01’) from dual;
1

日期转周函数: weekofyear
语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
举例:
hive> select weekofyear(‘2011-12-08 10:03:01’) ;
49

日期比较函数: datediff
语法: datediff(string enddate, string startdate)
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
hive> select datediff(‘2012-12-08’,‘2012-05-09’) from dual;
213

日期增加函数: date_add
语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
hive> select date_add(‘2012-12-08’,10) from dual;
2012-12-18

日期减少函数: date_sub
语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive> select date_sub(‘2012-12-08’,10) from dual;
2012-11-28

next_day
释义:取该日期的下一个周几的日期
用法:next_day(date,dayofweek)
星期一到星期日的英文(Monday,Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday)
举例:
取5.1后的下一个周五
hive (default)> select next_day(‘2020-05-01’,“Fri”);
2020-05-08

last_day:取当月的最后一天日期
用法:last_day(date)
举例:
取5月的最后一天日期
hive (default)> select last_day(‘2020-05-01’);
2020-05-31

dayofmonth:查询该日期在本月的第多少天
用法:dayofmonth(date)
举例:
5月6号在五月是第多少天
hive (default)> select dayofmonth(‘2020-05-06’);
6

current_date:获取当前日期
举例:
hive (default)> select current_date;
2020-05-14

current_timestamp:获取当前时间
举例:
hive (default)> select current_timestamp;
2020-05-14 10:26:57.613

add_months:日期加一个月
用法:add_months(date,number)
举例:
2020-05-03加一个月
hive (default)> select add_months(‘2020-05-03’,1);
2020-06-03

get_json_object:从一个json串中抽取 指定path下的 json str
$ 根json 对象 .子元素操作符 []数组子元素操作符 *通配符
举例:
select get_json_object(
‘[{“name”:“大郎”,“sex”:“男”,“age”:“25”},{“name”:“西门庆”,“sex”:“男”,“age”:“47”}’, ‘date格式转换 hive hive date转字符串string_hadoop_06.husbud[1].age’);

select regexp_replace(‘hello’,‘l’,‘o’) 用’o’替换’hello’中的’l’