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

相关文章

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

掌握Hive函数[2]:从基础到高级应用

目录 高级聚合函数 多进一出 1. 普通聚合 count/sum... 2. collect_list 收集并形成list集合,结果不去重 3. collect_set 收集并形成set集合,结果去重  案例演示 1. 每个月的入职人数以及姓名  炸裂函数  概述  案例演示 1. 数据准备 1)表结构 2)建表语句 3)装载语句 2. 需求 1)需求说明 2)答

【JavaScript】版本号和日期时间的比较

JS使用 ‘>’ 运算符比较两个字符串大小时,会把字符串转换为ASCII码依次比较。 比较标准时间格式可以直接使用 ’ > ’ 比较; (2018-07-20格式)

掌握Go语言中的时间与日期操作

Go语言中的时间与日期操作 在编写程序时,处理时间和日期看似是一项无关紧要的任务,但在需要同步多个任务或从文本文件中读取时间时,它的重要性便凸显出来。Go语言中的time包为我们提供了丰富的时间与日期操作功能。本文将详细介绍如何在Go语言中解析时间与日期字符串、在不同的格式之间进行转换,以及如何按照所需的格式输出时间和日期。 初识time包 在学习如何解析字符串并将其转换为时间或日期之前,我