Hive中日期处理

Hive中日期处理

144 
作者  一刀Q 
2016.08.25 15:31  字数 194  阅读 744 评论 0

1、日期函数UNIX时间戳转日期函数:from_unixtime()

函数 格式 返回值 说明
from_unixtime from_unixtime(bigint unixtime[, string format]) string 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
hive (temp)> select from_unixtime(1323308943,'yyyyMMdd') from dual;
20111208
hive (temp)> select from_unixtime(1323308943,'yyyy-MM-dd') from dual;
2011-12-08

2、当前UNIX时间戳函数: unix_timestamp()

2.1 获取当前UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp() bigint 获得当前时区的UNIX时间戳
hive (temp)> select unix_timestamp() from dual;
1472105939

2.2 日期转UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp(string date) bigint 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。转化失败,则返回0
hive (temp)> select unix_timestamp('2016-08-25 13:02:03') from dual;
1472101323

2.3 指定格式日期转UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp(string date, string pattern) bigint 转换格式为"yyyyMMdd HH:mm:ss"的日期到UNIX时间戳。转化失败,则返回0
hive (temp)> select unix_timestamp('20160825 13:02:03','yyyyMMdd HH:mm:ss') from dual;
1472101323

3、日期时间转日期函数: to_date()

函数 格式 返回值 说明
to_date to_date(string timestamp) string 返回日期时间字段中的日期部分
hive (temp)> select to_date('2016-12-08 10:03:01') from dual;
2016-12-08

4、日期转年函数: year()

函数 格式 返回值 说明
year year(string date) int 返回日期中的年
hive (temp)> select year('2016-12-08 10:03:01') from dual;
2016

hive (temp)> select year('2016-12-08') from dual;
2016

5、日期转月函数: month()

函数 格式 返回值 说明
month month(string date) int 返回日期中的月份
hive (temp)> select month('2016-12-08 10:03:01') from dual;
12

hive (temp)> select month('2016-11-08') from dual;
11

6、日期转天函数: day()

函数 格式 返回值 说明
day day(string date) int 返回日期中的天
hive (temp)> select day('2016-12-08 10:03:01') from dual;
8

hive (temp)> select day('2016-11-18') from dual;
18

7、日期转小时函数: hour()

函数 格式 返回值 说明
hour hour(string date) int 返回日期中的小时
hive (temp)> select hour('2016-12-08 10:03:01') from dual;
10

8、日期转分钟函数: minute()

函数 格式 返回值 说明
minute minute(string date) int 返回日期中的分钟
hive (temp)> select minute('2016-12-08 10:03:01') from dual;
3

9、日期转秒函数: second()

函数 格式 返回值 说明
second second(string date) int 返回日期中的秒
hive (temp)> select second('2016-12-08 10:03:01') from dual;
1

10、日期转周函数: weekofyear()

函数 格式 返回值 说明
weekofyear weekofyear(string date) int 返回日期在当前的周数
hive (temp)> select weekofyear('2016-12-08 10:03:01') from dual;
49

11、日期比较函数: datediff(string enddate, string startdate)

函数 格式 返回值 说明
datediff datediff(string enddate, string startdate) int 返回结束日期减去开始日期的天数
hive (temp)> select datediff('2016-12-08','2016-12-02') from dual;
6

12、日期增加函数: date_add(string startdate, int days)

函数 格式 返回值 说明
date_add date_add(string startdate, int days) string 返回开始日期startdate增加days天后的日期
hive (temp)> select date_add('2016-12-08',10) from dual;
2016-12-18

#当前日期为2016-08-25,在此基础上加7天
hive (temp)> select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7) from dual;
2016-09-01

13、日期减少函数:date_sub (string startdate, int days)

函数 格式 返回值 说明
date_sub date_sub(string startdate, int days) string 返回开始日期startdate减少days天后的日期
hive (temp)> select date_sub('2016-12-08',10) from dual;
2016-11-28

#当前日期为2016-08-25,在此基础上减7天
hive (temp)> select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7) from dual;
2016-08-18
转载地址: http://www.jianshu.com/p/e30395941f9c



### 如何在 Hive 中将字符串转换为日期格式 #### 使用 `unix_timestamp` 和 `from_unixtime` 对于特定格式的字符串时间,在将其转换成日期类型之前,可以先利用 `unix_timestamp` 函数解析该字符串到 Unix 时间戳(秒数),再通过 `from_unixtime` 转换成带格式的时间字符串。如果目标是得到标准的日期或时间戳类型的列,则可以在最后一步使用 `CAST` 来完成最终转换。 例如,要处理形如 `'2023-12-4 4:45:12'` 的字符串并希望获得对应的日期对象: ```sql SELECT CAST(from_unixtime(unix_timestamp('2023-12-4 4:45:12', 'yyyy-MM-dd HH:mm:ss')) AS DATE); ``` 这段 SQL 表达式的含义是从给定模式串中提出 UNIX 时间戳,接着用 `from_unixtime()` 把它变回人类可读的形式,而外层包裹着的 `CAST(...AS DATE)` 则负责确保结果被解释为目标数据类型——即日期型[^2]。 #### 直接应用 `date_format` 另外一种方式就是直接采用 `date_format` 函数来改变输入字符串的表现形式而不必经历中间步骤。不过需要注意的是,此方法适用于那些已经接近所需输出格式的情况;当源数据与期望的结果之间存在较大差异时,可能仍需借助前述两步走的方法来进行更精确控制。 比如想要简单调整年月日顺序: ```sql SELECT date_format('2016-08-16', 'yyyyMMdd'); -- 结果将是:20160816 ``` 这里展示了一个较为简单的例子,其中仅涉及到了重新排列已有组件的位置,并未涉及到跨字段的操作或是复杂的时间运算[^4]。 #### 处理带有时区信息的时间字符串 针对包含时区偏移量的信息,像 `'2023-12-4 4:45:12 UTC+08:00'` 这样的情况,同样可以通过指定合适的模板参数让 `unix_timestamp` 正确识别这些附加细节,从而实现准确无误地转换过程[^1]。 ```sql SELECT CAST( from_unixtime( unix_timestamp('2023-12-4 4:45:12 UTC+08:00', 'yyyy-MM-dd HH:mm:ss Z') ) AS TIMESTAMP); ``` 上述查询语句中的 `'Z'` 占位符用于匹配任何有效的时区表示法,使得整个表达能够兼容更多样化的输入场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值