获取Hive中的当前unix_timestamp

时间:2014-09-19 02:13:38

标签: sql hadoop hive

作为帖子How to select current date in Hive SQL,要获取Hive中的当前日期,可以使用unix_timestamp

但我试过

select unix_timestamp();

只是,

unix_timestamp();

都会给出错误消息

FAILED: ParseException line 1:23 mismatched input '<EOF>' expecting FROM near ')' in from clause
FAILED: ParseException line 1:0 cannot recognize input near 'unix_timestamp' '(' ')'

分别

如何在Hive中正确使用unix_timestamp

2 个答案:

答案 0 :(得分:3)

由于Hive没有公开dual table,你可能想要创建一个单独的行表,并将该表用于那种查询。

然后您就可以执行

之类的查询了
select unix_timestamp() from hive_dual;

解决方法是使用任何现有的表,使用LIMIT 1或TABLESAMPLE子句,但是,根据表的大小,它的效率会降低。

# any_existing_table contains 10 lines
# hive_dual contains 1 line

select unix_timestamp() from any_existing_table LIMIT 1; 
# Time taken: 17.492 seconds, Fetched: 1 row(s)
select unix_timestamp() from any_existing_table TABLESAMPLE(1 ROWS);
# Time taken: 15.273 seconds, Fetched: 1 row(s)

select unix_timestamp() from hive_dual ;
# Time taken: 16.144 seconds, Fetched: 1 row(s)
select unix_timestamp() from hive_dual LIMIT 1; 
# Time taken: 14.086 seconds, Fetched: 1 row(s)
select unix_timestamp() from hive_dual TABLESAMPLE(1 ROWS);
# Time taken: 16.148 seconds, Fetched: 1 row(s)

更新

无需传递任何表名和限制语句。 Hive现在支持select unix_timestamp()

更多详情:

Does Hive have something equivalent to DUAL?

BLOG POST : dual table in hive

答案 1 :(得分:0)

要使用to_date函数来获取时间戳之外的日期。

尝试以下

select to_date(FROM_UNIXTIME(UNIX_TIMESTAMP())) as time from table_name;