日期函数在hive中很常用,甚至有时候要自定义时间函数。首先hive中存放的数据经常是按时间分区进行存储的,有时候按时间分区的时间就是从日志中国解析出来的时间,或者有时候截取时间进行区间划分。比如我们自定义的时间函数DAYOFWEEK(日期),返回当前日期是星期几。其次因为所有的数据都与时间有关,所有的数据都具有时效性。

1.如下Hive内嵌的所有关于日期时间的相关函数

Return Type

Name(Signature)

Description

string

from_unixtime(bigint unixtime[, string format])

Converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone in the format of "1970-01-01 00:00:00".

将时间的秒值转换成format格式(format可为“yyyy-MM-dd hh:mm:ss”,“yyyy-MM-dd hh”,“yyyy-MM-dd hh:mm”等等)如from_unixtime(1250111000,"yyyy-MM-dd") 得到2009-03-12

bigint

unix_timestamp()

Gets current Unix timestamp in seconds.

获取本地时区下的时间戳

bigint

unix_timestamp(string date)

Converts time string in format yyyy-MM-dd HH:mm:ss to Unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail: unix_timestamp('2009-03-20 11:30:01') = 1237573801

将格式为yyyy-MM-dd HH:mm:ss的时间字符串转换成时间戳  如unix_timestamp('2009-03-20 11:30:01') = 1237573801

bigint

unix_timestamp(string date, string pattern)

Convert time string with given pattern (see [http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html]) to Unix time stamp (in seconds), return 0 if fail: unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400.

将指定时间字符串格式字符串转换成Unix时间戳,如果格式不对返回0 如:unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400

string

to_date(string timestamp)

Returns the date part of a timestamp string: to_date("1970-01-01 00:00:00") = "1970-01-01".

返回时间字符串的日期部分

int

year(string date)

Returns the year part of a date or a timestamp string: year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970.

返回时间字符串的年份部分

int quarter(date/timestamp/string) Returns the quarter of the year for a date, timestamp, or string in the range 1 to 4 (as of Hive 1.3.0). Example: quarter('2015-04-08') = 2.

返回当前时间属性哪个季度 如quarter('2015-04-08') = 2

int

month(string date)

Returns the month part of a date or a timestamp string: month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11.

返回时间字符串的月份部分

int

day(string date) dayofmonth(date)

Returns the day part of a date or a timestamp string: day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1.

返回时间字符串的天

int

hour(string date)

Returns the hour of the timestamp: hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12.

返回时间字符串的小时

int

minute(string date)

Returns the minute of the timestamp.

返回时间字符串的分钟

int

second(string date)

Returns the second of the timestamp.

返回时间字符串的秒

int

weekofyear(string date)

Returns the week number of a timestamp string: weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44.

返回时间字符串位于一年中的第几个周内  如weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44

int

datediff(string enddate, string startdate)

Returns the number of days from startdate to enddate: datediff('2009-03-01', '2009-02-27') = 2.

计算开始时间startdate到结束时间enddate相差的天数

string

date_add(string startdate, int days)

Adds a number of days to startdate: date_add('2008-12-31', 1) = '2009-01-01'.

从开始时间startdate加上days

string

date_sub(string startdate, int days)

Subtracts a number of days to startdate: date_sub('2008-12-31', 1) = '2008-12-30'.

从开始时间startdate减去days

timestamp

from_utc_timestamp(timestamp, string timezone)

Assumes given timestamp is UTC and converts to given timezone (as of Hive 0.8.0). For example, from_utc_timestamp('1970-01-01 08:00:00','PST') returns 1970-01-01 00:00:00.

如果给定的时间戳并非UTC,则将其转化成指定的时区下时间戳

timestamp

to_utc_timestamp(timestamp, string timezone)

Assumes given timestamp is in given timezone and converts to UTC (as of Hive 0.8.0). For example, to_utc_timestamp('1970-01-01 00:00:00','PST') returns 1970-01-01 08:00:00.

如果给定的时间戳指定的时区下时间戳,则将其转化成UTC下的时间戳

date current_date

Returns the current date at the start of query evaluation (as of Hive 1.2.0). All calls of current_date within the same query return the same value.

返回当前时间日期

timestamp current_timestamp

Returns the current timestamp at the start of query evaluation (as of Hive 1.2.0). All calls of current_timestamp within the same query return the same value.

返回当前时间戳

string add_months(string start_date, int num_months)

Returns the date that is num_months after start_date (as of Hive 1.1.0). start_date is a string, date or timestamp. num_months is an integer. The time part of start_date is ignored. If start_date is the last day of the month or if the resulting month has fewer days than the day component of start_date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as start_date.

返回当前时间下再增加num_months个月的日期

string last_day(string date) Returns the last day of the month which the date belongs to (as of Hive 1.1.0). date is a string in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time part of date is ignored.

返回这个月的最后一天的日期,忽略时分秒部分(HH:mm:ss)

string next_day(string start_date, string day_of_week) Returns the first date which is later than start_date and named as day_of_week (as of Hive1.2.0). start_date is a string/date/timestamp. day_of_week is 2 letters, 3 letters or full name of the day of the week (e.g. Mo, tue, FRIDAY). The time part of start_date is ignored. Example: next_day('2015-01-14', 'TU') = 2015-01-20.

返回当前时间的下一个星期X所对应的日期 如:next_day('2015-01-14', 'TU') = 2015-01-20  以2015-01-14为开始时间,其下一个星期二所对应的日期为2015-01-20

string trunc(string date, string format) Returns date truncated to the unit specified by the format (as of Hive 1.2.0). Supported formats: MONTH/MON/MM, YEAR/YYYY/YY. Example: trunc('2015-03-17', 'MM') = 2015-03-01.

返回时间的最开始年份或月份  如trunc("2016-06-26",“MM”)=2016-06-01  trunc("2016-06-26",“YY”)=2016-01-01   注意所支持的格式为MONTH/MON/MM, YEAR/YYYY/YY

double months_between(date1, date2) Returns number of months between dates date1 and date2 (as of Hive 1.2.0). If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise the UDF calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2. date1 and date2 type can be date, timestamp or string in the format 'yyyy-MM-dd' or 'yyyy-MM-dd HH:mm:ss'. The result is rounded to 8 decimal places. Example: months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677

返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1<date2,则返回负,否则返回0.0  如:months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677  1997-02-28 10:30:00与1996-10-30相差3.94959677个月

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

Converts a date/timestamp/string to a value of string in the format specified by the date format fmt (as of Hive 1.2.0). Supported formats are Java SimpleDateFormat formats –https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html. The second argument fmt should be constant. Example: date_format('2015-04-08', 'y') = '2015'.

date_format can be used to implement other UDFs, e.g.:

  • dayname(date) is date_format(date, 'EEEE')
  • dayofyear(date) is date_format(date, 'D')

    按指定格式返回时间date 如:date_format("2016-06-22","MM-dd")=06-22

2.常用的函数演示

1. UNIX时间戳转日期函数: from_unixtime
语法: from_unixtime(bigint unixtime[, string format]) ---格式可选择
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive> select from_unixtime(1520299121 'yyyyMMdd') from lxw_dual;
      20180306

hive> select FROM_UNIXTIME(UNIX_TIMESTAMP())

          2019-03-01 03:30:10
2. 获取当前UNIX时间戳函数: unix_timestamp
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
hive> select unix_timestamp() from lxw_dual;
      1323309615

总结:hive中一般使用from_unixtime和unix_timestamp设置etl时间戳,非常有用的函数。

格式比如:from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') AS etl_create_time  ---时间格式可选可不选

hive > select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') AS etl_create_time from fdm_sor.tmp_aaaaa limit 1;
2019-04-16 21:41:24hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd') AS etl_create_time from fdm_sor.tmp_aaaaa limit 1;
2019-04-16

3. 日期转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') from lxw_dual;
      1323234063
4. 指定格式日期转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 lxw_dual;
      1323234063
5. 日期时间转日期函数: to_date
语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。
举例:
hive> select to_date('2011-12-08 10:03:01') from lxw_dual;
      2011-12-08
6. 日期转年函数: year
语法: year(string date)
返回值: int
说明: 返回日期中的年。
举例:
hive> select year('2011-12-08 10:03:01') from lxw_dual;
        2011
hive> select year('2012-12-08') from lxw_dual;
         2012
7. 日期转月函数: month
语法: month (string date)
返回值: int
说明: 返回日期中的月份。
举例:
hive> select month('2011-12-08 10:03:01') from lxw_dual;
          12
hive> select month('2011-08-08') from lxw_dual;
           8
8. 日期转天函数: day
语法: day (string date)
返回值: int
说明: 返回日期中的天。
举例:
hive> select day('2011-12-08 10:03:01') from lxw_dual;
       8
hive> select day('2011-12-24') from lxw_dual;
      24
9. 日期转小时函数: hour
语法: hour (string date)
返回值: int
说明: 返回日期中的小时。
举例:
hive> select hour('2011-12-08 10:03:01') from lxw_dual;
10
10. 日期转分钟函数: minute
语法: minute (string date)
返回值: int
说明: 返回日期中的分钟。
举例:
hive> select minute('2011-12-08 10:03:01') from lxw_dual;
          3
11. 日期转秒函数: second
语法: second (string date)
返回值: int
说明: 返回日期中的秒。
举例:
hive> select second('2011-12-08 10:03:01') from lxw_dual;
           1
12. 日期转周函数: weekofyear
语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
举例:
hive> select weekofyear('2011-12-08 10:03:01') from lxw_dual;
          49
13. 日期比较函数: datediff,非常有用的函数。
语法: datediff(string enddate, string startdate)
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
hive> select datediff('2012-12-08','2012-05-09') from lxw_dual;
          213
14. 日期增加函数: date_add
语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
hive> select date_add('2012-12-08',10) from lxw_dual;
         2012-12-18
15. 日期减少函数: date_sub
语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive> select date_sub('2012-12-08',10) from lxw_dual;
         2012-11-28

hive内嵌时间日期函数:所有关于时间日期的函数相关推荐

  1. Hive 内嵌模式安装指导

    Hive内嵌模式说明: 在内嵌模式下(通过hive命令启动),CLI.Driver.metastore以及derby服务都运行在一个RunJar进程内. 1 安装环境介绍 1.1 相关组件版本 组件版 ...

  2. 通过枚举法暴力破解6位数以内zip密码压缩包,使用pyzipper包:内嵌生成1-6位数字加密码所有组合函数

    # -*- coding: utf-8 -*-#遍历写出1位数内英文加字母的所有组合 def NumLetterOne():NumLetter='0123456789abcdefghijklmnopq ...

  3. 内嵌模式搭建Hive

    在此之前已经搭建好了一个三台机器的hadoop集群:https://blog.csdn.net/QYHuiiQ/article/details/123055389?spm=1001.2014.3001 ...

  4. html5的交互式微课,内嵌交互式微课的设计与制作

    一.交互式微课概述作了交互式微课.微课的使用最早应用于美国可汗学院的翻转课堂教二.交互式微课的设计学模式,近几年国内学者对于微课的研究逐渐深化,交1.内嵌交互式微课的设计原则互式微课成为当下的研究重点 ...

  5. html嵌入式的脚本语言,Markdown语法及html内嵌

    之前看到很多大佬博客写的特别漂亮,非常羡慕,他们是怎么排版的呢?作为一个小白,学习使我快乐....终于在不断摸索中,找到方法.原来大佬们使用了一个新的语言--Markdown. 1.Markdown介 ...

  6. pandas使用bdate_range函数获取起始时间(start)和结束时间(end)范围内的所有周末日期(weekends day)

    pandas使用bdate_range函数获取起始时间(start)和结束时间(end)范围内的所有周末日期(weekends day) #pandas使用bdate_range函数获取起始时间(st ...

  7. pandas使用bdate_range函数获取起始时间(start)和结束时间(end)范围内的所有工作日日期(business day)

    pandas使用bdate_range函数获取起始时间(start)和结束时间(end)范围内的所有工作日日期(business day) #pandas使用bdate_range函数获取起始时间(s ...

  8. hive中如何把13位转化为时间_【hive常用函数一】日期函数

    使用hive进行数据处理的过程种,遇到不少问题,总结之前的一些开发经验,把之前的一些处理技巧mark一下: 使用通用的哑表dual作为测试表用. 1.时间戳转换 日期格式转换时间戳只能精确到秒(即长度 ...

  9. 深入浅出学Hive:Hive内建操作符与函数开发

    目录: 初始Hive Hive安装与配置 Hive内建操作符与函数开发 Hive JDBC Hive参数 Hive高级编程 Hive QL Hive Shell基本操作 Hive优化 Hive体系结构 ...

最新文章

  1. 汇编语言子程序的汇编及与C程序的连接
  2. 测试php框架漏洞,ThinkPHP框架通杀所有版本的一个SQL注入漏洞详细分析及测试方法...
  3. Go语言中查询SqlServer数据库
  4. SLAM: 图像角点检测的Fast算法(时间阈值实验)
  5. Redis(十一):Redis特殊类型之Bitmap位图
  6. 见微知著,构“见”未来
  7. 最近的问题汇总(至2010/10/6 12:00)
  8. 数据结构与算法 迷宫问题
  9. js基础——cssText
  10. Android 开发者们,如何使用 Python 来扩展 adb 命令?
  11. Oracle USE_LARGE_PAGES初始化参数
  12. 自适应漫画手机APP应用下载页源码
  13. MMKV_MMKV简介
  14. 低烟无卤计算机电缆,驻马店DZRDJYPV低烟无卤计算机电缆
  15. html左侧浮动广告代码,Html+CSS浮动的广告条实现分解
  16. 【scrapy】scrapy按分类爬取豆瓣电影基础信息
  17. reg文件编写方法整理
  18. 最新版CAD都有些啥功能(下)
  19. linux 脚本 等待,在shell脚本中实现无限等待
  20. 《基于GPU加速的计算机视觉编程》学习笔记

热门文章

  1. Keras花卉分类全流程(预处理+训练+预测)
  2. 基于SSM的毕业论文答辩系统
  3. c语言四项特效“疗法”
  4. 【目标定位】基于matlab扩展卡尔曼滤波器多机器人定位【含Matlab源码 2327期】
  5. c#12星座速配代码_十二星座运势接口代码示例分享
  6. 调整光电避障传感器测距的距离
  7. 【前端库】typed.js 打字机效果
  8. 火车票能不能选座_12306选座功能上线 选座位的方法是什么?
  9. NFT年度回顾:市场格局、品牌采用与版税争议
  10. 香港计算机专业硕士学制几年,香港城市大学一年制硕士2021申请条件