HiveQL函数4—日期函数

1.from_unixtime(bigint unixtime[, string format])

返回值: string
功能:将UNIX时间戳(从1970-01-0100:00:00 UTC到指定时间的秒数)转换为当前时区的时间格式,format可为多种格式,如“yyyy-MM-dd hh:mm:ss”,“yyyy-MM-dd hh”,“yyyy-MM-dd hh:mm”等。
示例:

> select from_unixtime(1567653779,'yyyy-MM-dd hh:mm:ss') as  f1;
+----------------------+
|          f1          |
| 2019-09-05 03:22:59  |
+----------------------+

 > select from_unixtime(1567653779,'yyyy-MM-dd') as  f1;
+-------------+
|     f1      |
+-------------+
| 2019-09-05  |
+-------------+

2.unix_timestamp()

返回值:bigint
功能:获取当前时间的UNIX时间戳
示例:

> select unix_timestamp() as  f1;
+-------------+
|     f1      |
+-------------+
| 1571106584  |
+-------------+

3.unix_timestamp(string date)

返回值:bigint
功能:将格式为yyyy-MM-dd HH:mm:ss的日期转换为UNIX时间戳
示例:

> select unix_timestamp('2019-05-12 18:21:17') as  f1;
+-------------+
|     f1      |
+-------------+
| 1557685277  |
+-------------+

4.unix_timestamp(string date, string pattern)

返回值:bigint
功能:将格式为pattern的日期转换为UNIX时间戳,支持的patterm见 https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html
示例:

> select unix_timestamp('2019-05-12 18','yyyy-MM-dd HH') as  f1;
+-------------+
|     f1      |
+-------------+
| 1557684000  |
+-------------+

> select unix_timestamp('2019.June.30 AD 08:29 AM','yyyy.MMMMM.dd GGG hh:mm a') as  f1;
+-------------+
|     f1      |
+-------------+
| 1561883340  |
+-------------+

5.to_date(string timestamp)

返回值:Hive2.1.0之前为string类型,之后为date类型
功能:返回时间字符串中的日期部分
示例:

> select to_date('2019-05-12 18:21:17') as  f1;
+-------------+
|     f1      |
+-------------+
| 2019-05-12  |
+-------------+

6.year(string date)

返回值:int
功能:返回日期或时间戳字符串中的year部分
示例:

> select year('2019-05-12 18:21:17') as  f1;
+-------+
|  f1   |
+-------+
| 2019  |
+-------+

7.quarter(date/timestamp/string)

返回值:int(范围从1到4)
功能:返回日期或时间戳或者字符串的季度
示例:

> select quarter('2019-07-12 18:21:17') as  f1;
+-----+
| f1  |
+-----+
| 3   |
+-----+

8.month(string date)

返回值:int
功能:返回日期或时间戳字符串中的month部分
示例:

> select month('2019-07-12') as  f1;
+-----+
| f1  |
+-----+
| 7   |
+-----+

9.day(string date) dayofmonth(date)

返回值:int
功能:返回日期或时间戳字符串中的day部分
示例:

> select day('2019-07-12') as  f1;
+-----+
| f1  |
+-----+
| 12  |
+-----+

> select dayofmonth('2019-07-12') as  f1;
+-----+
| f1  |
+-----+
| 12  |
+-----+

10.hour(string date)

返回值:int
功能:返回时间戳字符串中的hour部分
示例:

> select hour('2019-07-12 18:21:17') as  f1;
+-----+
| f1  |
+-----+
| 18  |
+-----+

11.minute(string date)

返回值:int
功能:返回时间戳字符串中的minute部分
示例:

> select minute('2019-07-12 18:21:17') as  f1;
+-----+
| f1  |
+-----+
| 21  |
+-----+

12.second(string date)

返回值:int
功能:返回时间戳字符串中的second部分
示例:

> select second('2019-07-12 18:21:17') as  f1;
+-----+
| f1  |
+-----+
| 17  |
+-----+

13.weekofyear(string date)

返回值:int
功能:返回时间戳字符串的week数,也就是当年的第几周
示例:

> select weekofyear('2019-07-12 18:21:17') as  f1;
+-----+
| f1  |
+-----+
| 28  |
+-----+

14.extract(field FROM source)

返回值:int
功能:Hive2.2.0新增函数。从指定的source中检索天或小时等字段,source必须是一个日期、时间戳、区间或可以转换为日期/时间戳的字符串。字段可以是:day, dayofweek, hour, minute, month, quarter, second, week , year。
示例:

> select extract(month from "2019-07-12") as f1;
+-----+
| f1  |
+-----+
| 7   |
+-----+

>  select extract(dayofweek from "2019-07-12") as f1;
+-----+
| f1  |
+-----+
| 6   |
+-----+

> select extract(hour from "2019-07-12 18:06:07") as f1; 
+-----+
| f1  |
+-----+
| 18  |
+-----+

> select extract(month from interval '1-3' year to month) as f1;
+-----+
| f1  |
+-----+
| 3   |
+-----+

> select extract(minute from interval '3 12:20:30' day to second) as f1;
+-----+
| f1  |
+-----+
| 20  |
+-----+

15.datediff(string enddate, string startdate)

返回值:int
功能:返回startdate到enddate的天数。
示例:

> select datediff('2019-09-15','2019-08-11') as f1;
+-----+
| f1  |
+-----+
| 35  |
+-----+

16.date_add(date/timestamp/string startdate, tinyint/smallint/int days)

返回值:Hive2.1.0之前为string类型,之后为date类型
功能:返回开始日期startdate增加days天后的日期
示例:

> select date_add('2019-07-15',12) as f1;
+-------------+
|     f1      |
+-------------+
| 2019-07-27  |
+-------------+

17.date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

返回值:Hive2.1.0之前为string类型,之后为date类型
功能:返回开始日期startdate减少days天后的日期
示例:

> select date_sub('2019-07-27',12) as f1;
+-------------+
|     f1      |
+-------------+
| 2019-07-15  |
+-------------+

18.from_utc_timestamp({any primitive type} ts, string timezone)

返回值:timestamp(timestamp是一个基本类型,包括 timestamp/date、tinyint/smallint/int/bigint、float/double和decimal)
功能:将UTC的时间转换为指定timezone的时间
示例:

> select from_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') as f1;
+------------------------+
|           f1           |
+------------------------+
| 1970-01-30 08:00:00.0  |
+------------------------+

> select from_utc_timestamp(2592000000,'PDT') as f1;
+------------------------+
|           f1           |
+------------------------+
| 1970-01-31 00:00:00.0  |
+------------------------+

补充:关于GMT、UTC、PST等时间的介绍可以参考https://www.cnblogs.com/timelyxyz/p/4609317.html

19.to_utc_timestamp({any primitive type} ts, string timezone)

返回值:timestamp(timestamp是一个基本类型,包括 timestamp/date、tinyint/smallint/int/bigint、float/double和decimal)
功能:将指定timezone的时间转换为UTC的时间
示例:

> select to_utc_timestamp(2592000000,'HST') as f1;
+------------------------+
|           f1           |
+------------------------+
| 1970-01-31 10:00:00.0  |
+------------------------+

> select to_utc_timestamp('2019-09-12 14:21:19','HST') as f1;
+------------------------+
|           f1           |
+------------------------+
| 2019-09-13 00:21:19.0  |
+------------------------+

20.current_date

返回值:date
功能:返回当前日期
示例:

> select current_date as f1;
+-------------+
|     f1      |
+-------------+
| 2019-10-11  |
+-------------+

21.current_timestamp

返回值:timestamp
功能:返回当前时间戳
示例:

> select current_timestamp as f1;
+--------------------------+
|            f1            |
+--------------------------+
| 2019-10-15 11:56:17.638  |
+--------------------------+

22.add_months(string start_date, int num_months, output_date_format)

返回值:string
功能:返回start_date时间增加num_months个月的日期
示例:

> select add_months('2017-08-31', 5) as f1;
+-------------+
|     f1      |
+-------------+
| 2018-01-31  |
+-------------+

> select add_months('2017-12-31 14:15:16', 2, 'YYYY-MM-dd HH:mm:ss') as f1;
+----------------------+
|          f1          |
+----------------------+
| 2018-02-28 14:15:16  |
+----------------------+

23.last_day(string date)

返回值:string
功能:返回日期中月的最后一天,日期的格式为’yyyy-MM-dd HH:mm:ss’ 或 ‘yyyy-MM-dd’,日期的时间部分会被忽略
示例:

> select last_day('2019-07-12 14:25:16') as f1;
+-------------+
|     f1      |
+-------------+
| 2019-07-31  |
+-------------+

24.next_day(string start_date, string day_of_week)

返回值:string
功能:返回start_date的下一个day_of_week所对应的日期。start_date 是一个 string/date/timestamp。day_of_week是星期X的2个或3个字符或者全名(如 Mo, tue, FRIDAY)。日期的时间部分会被忽略。
示例:

> select next_day('2019-07-12 14:25:16','Friday') as f1;
+-------------+
|     f1      |
+-------------+
| 2019-07-19  |
+-------------+

25.trunc(string date, string format)

返回值:string
功能:返回日期的开始年份和月份,支持的格式包括:MONTH/MON/MM, YEAR/YYYY/YY
示例:

> select trunc('2019-07-12 14:25:16','MM') as f1;
+-------------+
|     f1      |
+-------------+
| 2019-07-01  |
+-------------+

> select trunc('2019-07-12 14:25:16','YY') as f1;
+-------------+
|     f1      |
+-------------+
| 2019-01-01  |
+-------------+

26.months_between(date1, date2)

返回值:double
功能:返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1<date2,则返回负,否则返回0.0。date1和date2的类型可以是date, timestamp或格式为 ‘yyyy-MM-dd’ 或 ‘yyyy-MM-dd HH:mm:ss’ 的字符串。结果四舍五入到小数点后8位。
示例:

> select months_between('2019-07-12 14:25:16','2018-12-5') as f1;
+-------------+
|     f1      |
+-------------+
| 7.24518967  |
+-------------+

> select months_between('2019-07-12 14:25:16','2019-07-12') as f1;
+------+
|  f1  |
+------+
| 0.0  |
+------+

27.date_format(date/timestamp/string ts, string fmt)

返回值:string
功能:按照指定的格式返回时间,支持的fmt见 https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
示例:

> select date_format('2019-07-12 14:25:16','m-d') as f1;
+--------+
|   f1   |
+--------+
| 25-12  |
+--------+

参考

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
https://blog.csdn.net/lichangzai/article/details/19406215
https://www.cnblogs.com/MOBIN/p/5618747.html#4

发布了57 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/CPP_MAYIBO/article/details/104065247
今日推荐