Hive中日期处理函数

2024-05-24 01:32
文章标签 hive 日期 处理函数

本文主要是介绍Hive中日期处理函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

函数格式返回值说明
from_unixtimefrom_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_timestampunix_timestamp()bigint获得当前时区的UNIX时间戳
hive (temp)> select unix_timestamp() from dual;
1472105939

2.2 日期转UNIX时间戳函数

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

函数格式返回值说明
datediffdatediff(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_adddate_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_subdate_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

 

这篇关于Hive中日期处理函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996986

相关文章

java如何通过Kerberos认证方式连接hive

《java如何通过Kerberos认证方式连接hive》该文主要介绍了如何在数据源管理功能中适配不同数据源(如MySQL、PostgreSQL和Hive),特别是如何在SpringBoot3框架下通过... 目录Java实现Kerberos认证主要方法依赖示例续期连接hive遇到的问题分析解决方式扩展思考总

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

linux下修改系统日期与时间

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

Hive和Hbase的区别

Hive 和 HBase 都是 Hadoop 生态系统中的重要组件,它们都能处理大规模数据,但各自有不同的适用场景和设计理念。以下是两者的主要区别: 1. 数据模型 Hive:Hive 类似于传统的关系型数据库 (RDBMS),以表格形式存储数据。它使用 SQL-like 语言 HiveQL 来查询和处理数据,数据通常是结构化或半结构化的。HBase:HBase 是一个 NoSQL 数据库,基

时间日期与时间戳转换(Linux C)

本文主要学习三个知识点,第一是UTC时间、GMT时间的概念;第二是在Unix环境下UTC时间与时间戳的转换;第三是在C语言中如何修改时区。 本文参考了《UNP》以及 http://blog.csdn.net/foxir/article/details/43916601 http://blog.csdn.net/ljafl9988/article/details/16847935 一、

Java中计算两个日期间隔多少天

String dbtime1 = "2017-02-23";  //第二个日期 String dbtime2 = "2017-02-22";  //第一个日期 //算两个日期间隔多少天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = format.parse(dbtime1); Date dat

实现日期往前或往后或跳转到指定月份或天数

//月份跳转 //初始日期 String yearMonth = "201702"; String yearMonthStr = ""; //往前(负数)或往后(正数) int add = -2; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date source = sdf.parse(yearMonth); Cal

Java:日期类2

SimpleDateFormat日期格式化类 构造 SimpleDateFormat(String pattern); pattern是我们自己制定的日期格式,字母不能改变,但连接符可以改变 yyyy--MM--dd--HH 时间单位字母表示 Y年M月d日H时m分s秒 方法 String format(Date date)将Date对象按照对应格式转成String Date par