hive获取当前时间(hive获取当前时间前五分钟)
本篇文章给大家谈谈hive获取当前时间,以及hive获取当前时间前五分钟对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、hive 常用函数
- 2、hive如何获取shell当天日期,并创建分区表
- 3、hive 中怎么unix时间戳转化为"yyyy-mm-dd hh:mm:ss
- 4、hive6:字符串和日期的转换常用函数
- 5、Hive—date函数详解
- 6、Hive内置函数之时间函数
hive 常用函数
查看系统自带的函数
show functions;
显隐空示自带的函数的用法
desc function upper;
3 )详细显示自带的函数的用法
desc function extended upper;
常用日期函数
unix_timestamp:返回当前或指定时间的时间戳
from_unixtime:将时间戳转为日期格式
current_date:当前日期
current_timestamp:当前的日期加时间
to_date:抽取日期部分
year:获取年
month:获取月
day:获取日
hour:获取时
minute:获取分
second:获取秒
weekofyear:当前时间是一年中的第几周
dayofmonth:当前时间是一个月中的第几天
months_between: 两个日期间的月份
add_months:日期加减月
datediff:两个日期相差的天数
date_add:日期加天运哪数
date_sub:日期减天数
last_day:日期的当月的最后一天
常用取整函数
round: 四舍五入
ceil: 向上取整
floor: 向下取整
常用字符串操作函数
upper: 转大写
lower: 转小写
length: 长度
trim: 前后去空格
lpad: 向左补齐,到指定长度
rpad: 向右补齐,到指定长度
regexp_replace: SELECT regexp_replace('100-200', '(\旁携码\d+)', 'num') ;
使用正则表达式匹配目标字符串,匹配成功后替换!
[img]hive如何获取shell当天日期,并创建分区表
方毕碰法一:利用编辑器直接插入控制字符,培数颤以Vi为例。进入Vi:Shell代码收藏代码$visupply-20110101.txt在Vi命令模式下,键入:setlist,设置控制字符可见,成功后Vi会立配败即显示一个行结束标志$。填入Hive表中需要的每列数据,比如我这里需要创建一个...
hive 中怎么unix时间戳转化为"yyyy-mm-dd hh:mm:ss
1.日期函数UNIX时间戳中蔽转日期函数: from_unixtime语法:from_unixtime(bigint unixtime[, stringformat])
返回值: string
说明: 转化UNIX时间戳(从1970-01-0100:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive select from_unixtime(1323308943,'yyyyMMdd') from dual;
20111208
2.获取当前UNIX时间仿举戳函数: unix_timestamp语法: unix_timestamp()
返回值: bigint
说明: 获备培碧得当前时区的UNIX
hive6:字符串和日期的转换常用函数
用到from_unixtime和unix_timestamp两种函数:
from_unixtime:时间戳转日期函数
用法:from_unixtime(bigint unixtime[, stringformat])
返回值: string
substr(from_unixtime(unix_timestamp()),1,10)
结果为:2017-01-03
select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd')
from dw.ceshi_data
结果如下:
2018-09-05
2018-09-05转成20180905
select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyymmdd')
from dw.ceshi_data
结果如下:
20180905
用法:unix_timestamp(string date)
注意:里面格式必须是yyyy-MM-dd HH:mm:ss,如果不是会返回null值
返回值: bigint
from dw.ceshi_data;
结果如下:
1536120063
获取当前日期的时间戳:
select unix_timestamp()
from dw.ceshi_data;
结果如下:
1536126324
hive表中,存放着无法直接识别的字符串格式的时间,如'20170728102031',要计算两个时间相差的秒数。
1、先将字符串调整为hive可以识别的格式,即将形如'谈败20170728102031' 转成 '2017-07-28 10:20:31'。 因为hive的 regexp_replace 不支持子语句,没法一次转换,只能用万能的哗郑 substr 和拼接函数来写了
select concat(substr('20170728102031',1,4),'-',
substr('20170728102031',5,2),'-',
substr('20170728102031',7,2),' ',
substr('20170728102031',9,2),':',
substr('20170728102031',11,2),':',
substr('20170728102031',13,2))
select unix_timestamp(concat(substr('20170728102031',1,4),'-',substr('20170728102031',5,2),'-',
substr('20170728102031',7,2),' ',
substr('20170728102031',9,2),':',
substr('含芦颤20170728102031',11,2),':',
substr('20170728102031',13,2))) - unix_timestamp(concat(substr('20170728112031',1,4),'-',
substr('20170728112031',5,2),'-',
substr('20170728112031',7,2),' ',
substr('20170728112031',9,2),':',
substr('20170728112031',11,2),':',
substr('20170728112031',13,2)))
hive select create_time ,datediff(from_unixtime(unix_timestamp(),’yyyy-MM-dd HH:mm:ss’), create_time) from test;
结果:当前时间是2017-11-16 与create_time的11-10之间差了6天,输出6;
hive select datediff(’2012-12-08′,’2012-05-09′) from dual;
213
日期时间转日期函数: to_date语法: to_date(string timestamp) 返回:string
hive select to_date(’2011-12-08 10:03:01′) from dual;
2011-12-08
2011
以下可以取到 month,hour,minute,second 用法和上面的一样
日期转周函数: weekofyear语法: weekofyear (string date) 返回值: int 说明: 返回日期在当前的周数。
hive select weekofyear(’2011-12-08 10:03:01′) from dual;
49
日期增加函数: date_add语法:
date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期
举例:
hive select date_add(’2012-12-08′,10) from dual;
2012-12-18
日期减少函数: date_sub语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive select date_sub(’2012-12-08′,10) from dual;
2012-11-28
所以我们利用其中的hour和datediff来获取create_time与当前时间的小时差:
hive select create_time,
(hour(from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'))-
hour(create_time)+(datediff(from_unixtime(unix_timestamp(),
'yyyy-MM-dd HH:mm:ss'), create_time))*24) as hour_dValue
Hive—date函数详解
在hive中我们经常需要处理日期数据,hive内置了3个日期函数,其格式只能为 yyyy-MM-dd 格式或者 yyyy-MM-dd HH:mm:ss' 格式
datediff(string enddate,string startdate)
说明 :返回结束日期 enddate 减去开始日期 startdate 的天数
返回值类型 :int
2.返回天数为负数
其中:
create_time 为 table_01 中的时间字段;
current_timestamp 为放回当前时间;
date_add(string startdate,int days)
说明 :返回开始日期startdat增加天数days后的日期, days 可以正负数,若 days0 ,则表示增加days的日期。若 days0 ,则表示减少days的日期。袭斗
返回值类型 :string
其中:
create_time 为 table_01 中的时间字段;
current_timestamp 为渗禅慧放回当前时间;
date_sub(string startdate,int days)
说明 :返回开始日期startdat减去天数days后的日期, days 可以正负数,若 days0 ,则表示减少days的日期。若 days0 ,则表示增加days的日期。
返回值类型 :string
其中:
create_time 为 table_01 中的时间字丛答段;
current_timestamp 为放回当前时间;
Hive内置函数之时间函数
零、生产常用粗搏组合方式
(0.1)离线数仓获取昨天的日期作为分区,格式yyyyMMdd
regexp_replace(date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1) ,'-','')
或者
date_format(date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1),'yyyyMMdd')
一、源码部分
Hive的函数类为:org.apache.hadoop.hive.ql.exec.FunctionRegistry
二、常用时间函数
对于函数,除了知道怎么用,还需要知道返回值是什么类型,这里给出官方文档,文档中给出了函数的返回值类型
官方文档见:
(2.1)from_unixtime(bigint unixtime[, string format])
示例:
select from_unixtime(1591627588); -- 2020-06-08 22:46:28
select from_unixtime(1591627588,'yyyyMMddHHmmss'); -- 20200608224628
(2.2)unix_timestamp()、unix_timestamp(string date)、unix_timestamp(string date, string pattern)
示例:
select unix_timestamp('2020-06-08 22:50:00'); -- 1591627800
select unix_timestamp('20200608225000','yyyyMMddHHmmss'); -- 1591627800
(2.3)to_date(string timestamp)
示例:
SELECT to_date('2009-07-30 04:17:52'); -- 2009-07-30
(2.4)year(string date)、month(string date)、day(string date)、hour(string date)、minute(string date)、second(string date)
这些函数是差不多的,弯脊都是从一个时间字符串中抽取出某个特定的时间字段。具有相同功能的还有extract(field FROM source)函数
示例:
SELECT day('2009-07-29 20:30:40'); -- 29
SELECT minute('2009-07-29 20:30:40'); -- 30
(2.5)date_add(date/timestamp/string startdate, tinyint/smallint/int days)、date_sub(date/timestamp/string startdate, tinyint/smallint/埋凳渗int days)
这两个功能是类似的
示例:
SELECT date_add('2009-07-30 20:50:59', 1); -- 2009-07-31
(2.6)datediff(string enddate, string startdate)
截图中结果是错误的,应该为-1。
示例:
SELECT datediff('2009-06-30', '2009-07-02'); -- -2
SELECT datediff('2009-07-30', '2009-07-28'); -- 2
(2.7)current_date、current_timestamp
这两个函数使用desc function extended 查看会报错
示例:
(2.8)date_format(date/timestamp/string ts, string fmt)
示例:
SELECT date_format('2015-04-08', 'yyyyMMdd'); -- 20150408
关于hive获取当前时间和hive获取当前时间前五分钟的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。