我以为这很容易......
在 Hive/SparkSQL 中,如何将 unix 时间戳[注 1] 转换为timestamp
数据类型?
(注1:即自1970年1月1日起的秒/毫秒数)
我想from_unixtime()
会这样做,但它返回一个字符串
而不是时间戳。下面的实验说明了这个问题
第0步:准备
select
from_unixtime(1508673584) as fut;
Result:
-----------------------
| fut |
| ------------------- |
| 2017-10-22 11:59:44 |
-----------------------
步骤 1:创建一个表,其中包含结果from_unixtime()
create table test
select
from_unixtime(1508673584) as fut;
步骤 2:检查列的数据类型fut
describe test;
Result:
----------------------------------
| col_name | data_type | comment |
| -------- | --------- | ------- |
| fut | string | <null> |
----------------------------------
我也尝试过这个
select
from_utc_timestamp(1508618794*1000, 'EDT');
根据手册(链接here https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions),这应该有效。因为它
指出:
将 UTC 时间戳*转换为给定时区(从 Hive 0.8.0 开始)。 *
timestamp 是一个原始类型,包括时间戳/日期,
tinyint/smallint/int/bigint、float/double 和decimal。分数
值被视为秒。整数值被视为
毫秒.. 例如 from_utc_timestamp(2592000.0,'PST'),
from_utc_timestamp(2592000000,'PST') 和 from_utc_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