Hive中日期处理
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
作者:一刀Q
链接:http://www.jianshu.com/p/e30395941f9c
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Hive中日期处理的更多相关文章
- Hive中日期函数总结
--Hive中日期函数总结: --1.时间戳函数 --日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 select unix_timestamp(); --获得当前时区 ...
- hive时间日期函数及典型场景应用
1.hive取得当前日期时间: 1.1) 取得当前日期: select current_date(); 1.2) 取得当前日期时间: select current_timestamp(); 1.3) ...
- hive中时间操作(二)
转:https://blog.csdn.net/qq646748739/article/details/77997276 --Hive中日期函数总结:--1.时间戳函数--日期转时间戳:从1970-0 ...
- Hive中自定义函数
Hive的自定义的函数的步骤: 1°.自定义UDF extends org.apache.hadoop.hive.ql.exec.UDF 2°.需要实现evaluate函数,evaluate函数支持重 ...
- 关于sparksql操作hive,读取本地csv文件并以parquet的形式装入hive中
说明:spark版本:2.2.0 hive版本:1.2.1 需求: 有本地csv格式的一个文件,格式为${当天日期}visit.txt,例如20180707visit.txt,现在需要将其通过spar ...
- 漫谈数据仓库之拉链表(原理、设计以及在Hive中的实现)
本文将会谈一谈在数据仓库中拉链表相关的内容,包括它的原理.设计.以及在我们大数据场景下的实现方式. 全文由下面几个部分组成: 先分享一下拉链表的用途.什么是拉链表. 通过一些小的使用场景来对拉链表做近 ...
- sqoop 从oracle导数据到hive中,date型数据时分秒截断问题
oracle数据库中Date类型倒入到hive中出现时分秒截断问题解决方案 1.问题描述: 用sqoop将oracle数据表倒入到hive中,oracle中Date型数据会出现时分秒截断问题,只保留了 ...
- hive中时间操作(一)
转:https://blog.csdn.net/u012474716/article/details/78925319/ hive中常用的时间为时间戳和日期格式之间的转换 常用的函数为: to_dat ...
- 【hive 日期函数】Hive常用日期函数整理
1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12');输出:2015-04-02122.from_unixtime:转化unix时间戳到当 ...
随机推荐
- Android开发-ADT Bundle安装
此次安装是在Android Studio的机器上.安装Eclipse是因为目前很多Android程序是Eclipse开发的,要想快速看到运行效果就是安装Eclipse. 1.所有的资源都在:http: ...
- ORM框架greenDao 2 (用于了解旧版本的使用方法,目前最新版本为3.2.2,使用注释的方式来生成)
摘要: Android中对SQLite数据库使用,是一件非常频繁的事情.现今,也有非常多的SQLite处理的开源框架,其中最著名的greenDao,它以占用资源少,处理效率高等特点,成为优秀的ORM框 ...
- mysql术语
事务 概念:在关系数据库中,一个事物可以是一条sql语句,一组sql语句或整个程序. 特性:事物应该具有4个特性:原子性.一致性.隔离性.持久性.统称为ACID特性. 原子性(A)一个不可分割的工作单 ...
- 基于JQuery 的消息提示框效果代码
提示框效果 一下是封装到 Jquery.L.Message.js 中的JS文件内容 var returnurl = ''; var messagebox_timer; $.fn.messagebox ...
- HTML子页面保存关闭并刷新父页面
1.思路是子页面保存后,后台传递成功的js到前台. 2.js的原理是——子页面调用父页面的刷新 子页面 function Refresh() { window.parent.Re ...
- S.O.L.I.D 是面向对象设计(OOD)和面向对象编程(OOP)中的几个重要编码原则
注:以下图片均来自<如何向妻子解释OOD>译文链接:http://www.cnblogs.com/niyw/archive/2011/01/25/1940603.html < ...
- STL学习笔记(转,还是比较全的)
STL简介 1 概况 2 1.1 STL是什么 2 1.2 为什么我们需要学习STL 2 1.3 初识STL 2 1.4 STL 的组成 5 2 容器 6 2.1 基本容器——向量(vector) 6 ...
- iOS-不用微信SDK唤起微信支付
作者:TianBai 原文链接:http://www.jianshu.com/p/8930b4496023 要想知道微信SDK是如何调起微信客户端,那么咱们先看看微信SDK到底做了什么 前期准备 接入 ...
- Python 常用 PEP8 编码规范
Python 常用 PEP8 编码规范 代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPLE: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- PKUSC2013 BUG集锦
如果今年考试真的是这个难度,那比的就是速度和准确度了…… a A:不明觉厉 B:推公式后不明觉厉 C:树的HASH D:不明觉厉 E:QAQ 复制代码'-'忘改'+' WA×1, F:QAQ 请输出 ...