hive日期操作

该片文章为转载,整理了两篇博客,主要是方便我的查找,里面内容经过检验。
1.unix_timestamp()
返回当前时区的unix时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp() from hive_sum limit 1;
1465875016

2.from_unixtime(bigint unixtime[,string format])
时间戳转日期函数
返回类型:string
hive (tmp)> select from_unixtime(unix_timestamp(),’yyyyMMdd’) from hive_sum limit 1;
20160614

3.unix_timestamp(string date)
返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
hive (tmp)> select unix_timestamp(‘2016-06-01’) from hive_sum limit 1;
NULL
hive (tmp)> select unix_timestamp(‘2016-06-01 00:00:00’) from hive_sum limit 1;
1464710400

4.unix_timestamp(string date,string pattern)
返回指定日期格式的时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp(‘2016-06-01’,’yyyyMMdd’) from hive_sum limit 1;
1449331200
hive (tmp)> select unix_timestamp(‘2016-06-01’,’yyyy-MM-dd’) from hive_sum limit 1;
1464710400

5.to_date(string date)
返回时间字段中的日期部分
返回类型:string
hive (tmp)> select to_date(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016-06-01

6.year(string date)
返回时间字段中的年
返回类型:int
hive (tmp)> select year(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016

7.month(string date)
返回时间字段中的月
返回类型:int
hive (tmp)> select month(‘2016-06-01’) from hive_sum limit 1;
6

8.day(string date)
返回时间字段中的天
返回类型:int
hive (tmp)> select day(‘2016-06-01’) from hive_sum limit 1;
1

9.weekofyear(string date)
返回时间字段是本年的第多少周
返回类型:int
hive (tmp)> select weekofyear(‘2016-06-01’) from hive_sum limit 1;
22

10.datediff(string enddate,string begindate)
返回enddate与begindate之间的时间差的天数
返回类型:int
hive (tmp)> select datediff(‘2016-06-01’,’2016-05-01’) from hive_sum limit 1;
31

11.date_add(string date,int days)
返回date增加days天后的日期
返回类型:string
hive (tmp)> select date_add(‘2016-06-01’,15) from hive_sum limit 1;
2016-06-16

12.date_sub(string date,int days)
返回date减少days天后的日期
返回类型:string
hive (tmp)> select date_sub(‘2016-06-01’,15) from hive_sum limit 1;
2016-05-17

案例:

UNIX时间戳转日期: from_unixtime

日期转UNIX时间戳,指定格式日期转UNIX 时间戳,获取当前UNIX时间戳: unix_timestamp

说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到 UNIX 时间戳。如果转化失败,则返回 0。

select
from_unixtime(1323308943),
from_unixtime(1323308943,‘yyyyMMdd’),
unix_timestamp(),
unix_timestamp(‘2017-12-07 16:01:03’),
unix_timestamp(‘20171207 16-01-03’,‘yyyyMMdd HH-mm-ss’)
from
order_detail limit 1;
±---------------------±----------±------------±------------±------------±-+
| _c0 | _c1 | _c2 | _c3 | _c4 |
±---------------------±----------±------------±------------±------------±-+
| 2011-12-08 09:49:03 | 20111208 | 1489029488 | 1512633663 | 1512633663 |
±---------------------±----------±------------±------------±------------±-+

日期时间转日期:to_date 日期转年:year 日期转月:month 日期转天:day 日期转小时:hour 日期转分钟:minute 日期转秒:second

select
to_date(‘2016-12-08 10:03:01’),
year(‘2016-12-08 10:03:01’),
month(‘2016-12-08’),
day(‘2016-12-08 10:03:01’),
hour(‘2016-12-08 10:03:01’),
minute(‘2016-12-08 10:03:01’),
second(‘2016-12-08 10:03:01’)
from
order_detail limit 1;
±------------±------±-----±-----±-----±-----±-----±-+
| _c0 | _c1 | _c2 | _c3 | _c4 | _c5 | _c6 |
±------------±------±-----±-----±-----±-----±-----±-+
| 2016-12-08 | 2016 | 12 | 8 | 10 | 3 | 1 |
±------------±------±-----±-----±-----±-----±-----±-+

日期转周:weekofyear 日期比较:datediff

select
weekofyear(‘2016-12-08 10:03:01’),
datediff(‘2016-12-08’,‘2016-11-27’)
from order_detail limit 1;

±-----±-----±-+
| _c0 | _c1 |
±-----±-----±-+
| 49 | 11 |
±-----±-----±-+

日期增加: date_add 日期减少: date_sub

select date_add(‘2016-12-08’,10),date_add(‘2016-12-08’,-10),
date_sub(‘2016-12-08’,-10),date_sub(‘2016-12-08’,10) from order_detail limit 1;

±------------±------------±------------±------------±-+
| _c0 | _c1 | _c2 | _c3 |
±------------±------------±------------±------------±-+
| 2016-12-18 | 2016-11-28 | 2016-12-18 | 2016-11-28 |
±------------±------------±------------±------------±-+

select
date_add(‘20161208’,10),
from_unixtime(unix_timestamp(date_add(‘2016-12-08’,10)),‘yyyyMMdd’),
from_unixtime(unix_timestamp(date_add(‘2016-12-08’,10),‘yyyy-MM-dd’),‘yyyyMMdd’)
from order_detail limit 1;

±------±------±----------±-+
| _c0 | _c1 | _c2 |
±------±------±----------±-+
| NULL | NULL | 20161218 |
±------±------±----------±-+

### Hive 中实现日期自增的操作Hive 中,可以借助 `DATE_ADD` 或者 `SEQUENCE` 函数来实现日期的自增操作。以下是详细的说明以及 SQL 实现方式。 #### 使用 DATE_ADD 函数 Hive 提供了一个内置函数 `DATE_ADD`,用于向指定日期增加一定天数。其语法如下: ```sql DATE_ADD(start_date, num_days) ``` 其中: - `start_date` 是起始日期。 - `num_days` 表示要增加的天数,正整数表示向前加,负整数表示向后减。 通过循环调用该函数,可以在查询中生成一系列连续的日期[^1]。 #### 示例代码 以下是一个简单的例子,展示如何基于某个初始日期生成一组自增的日期序列: ```sql SELECT start_date, DATE_ADD(start_date, n.n) AS incremented_date FROM ( SELECT '2023-10-10' AS start_date -- 初始日期 ) init LATERAL VIEW POSEXPLODE(SPLIT(REPEAT('a', 10), ',')) n AS pos; -- 这里生成了从0到9共10个数字 ``` 在这个例子中,我们使用了 `POSEXPLODE` 和字符串分割技巧生成了一组序号 (n),并将其作为偏移量传递给 `DATE_ADD` 来计算新的日期值。 #### 使用 SEQUENCE 函数 如果只需要生成一段固定的日期范围,则可以直接采用 `SEQUENCE` 函数配合 `EXPLODE` 转化成单行记录形式。此方法更加简洁明了一些情况适用性强于手动构建增量逻辑。 ```sql SELECT EXPLODE( SEQUENCE(TO_DATE('2023-10-10'), TO_DATE('2023-10-20'), INTERVAL 1 DAY) ) AS generated_dates; ``` 这里定义了两个端点 `'2023-10-10'` 至 `'2023-10-20'` ,并通过设置步长为一天完成整个区间内的遍历输出[^4]。 以上两种方案均能有效解决 hive 环境下日期型字段按需扩展需求;具体选用哪种取决于实际应用场景和个人偏好等因素影响下的权衡考量结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值