我以为这很容易。。。
在hive/sparksql中,如何将unix时间戳[注1]转换为 timestamp
数据类型?
(注1:即自1970年1月1日起的秒/毫秒数)
我想 from_unixtime()
会这样做,但它会返回一个字符串而不是时间戳。下面的实验说明了这个问题
第0步:准备
select
from_unixtime(1508673584) as fut;
结果:
-----------------------
| fut |
| ------------------- |
| 2017-10-22 11:59:44 |
-----------------------
步骤1:创建一个包含 from_unixtime()
```
create table test
select
from_unixtime(1508673584) as fut;
步骤2:检查列的数据类型 `fut` ```
describe test;
结果:
----------------------------------
| col_name | data_type | comment |
| -------- | --------- | ------- |
| fut | string | <null> |
----------------------------------
我也试过这个
select
from_utc_timestamp(1508618794*1000, 'EDT');
根据手册(链接这里),这应该是可行的。因为它说:
将时间戳*转换为给定的时区(从配置单元0.8.0开始)。*timestamp是一种基本类型,包括timestamp/date、tinyint/smallint/int/bigint、float/double和decimal。分数值被视为秒。整数值被视为毫秒。。e、 g from \u utc \u timestamp(2592000.0,'pst')、from \u utc \u timestamp(2592000000,'pst')和from \u utc \u timestamp(timestamp'1970-01-30 16:00:00','pst')都返回时间戳1970-01-30 08:00:00
然而,我得到了一个错误
Error: org.apache.spark.sql.AnalysisException:
cannot resolve 'from_utc_timestamp((1508618794 * 1000), 'EDT')'
due to data type mismatch:
argument 1 requires timestamp type,
however, '(1508618794 * 1000)' is of int type.; line 2 pos 2;
'Project [unresolvedalias(from_utc_timestamp((1508618794 * 1000), EDT), None)]
+- OneRowRelation$
SQLState: null
ErrorCode: 0
2条答案
按热度按时间gdx19jrr1#
(我在这里自己提供答案。)
答案是使用
cast()
. 这对双方都有效date
以及timestamp
```select
from_unixtime(1508673584) as fut,
cast(from_unixtime(1508673584) as date) as futAsDate,
cast(from_unixtime(1508673584) as timestamp) as futAsTimestamp;
| fut | futAsDate | futAsTimestamp |
| ------------------- | ---------- | --------------------- |
| 2017-10-22 11:59:44 | 2017-10-22 | 2017-10-22 11:59:44.0 |
create table test2
select
from_unixtime(1508673584) as fut,
cast(from_unixtime(1508673584) as date) as futAsDate,
cast(from_unixtime(1508673584) as timestamp) as futAsTimestamp;
describe test2;
| col_name | data_type | comment |
| -------------- | --------- | ------- |
| fut | string | |
| futAsDate | date | |
| futAsTimestamp | timestamp | |
m1m5dgzv2#
create table test as select cast(从\u unixtime(1508673584)作为时间戳)as fut;