Hive - 来自时间戳列的月份和年份

Hive - month and year from timestamp column

您好,我正在尝试使用以下查询提取 hive 中时间戳列的月份和年份部分

select from_unixtime(unix_timestamp(upd_gmt_ts,'yyyyMM')) from  abc.test;

输出看起来像 2016-05-20 01:08:48

期望的输出应该是 201605

感谢任何建议。

您可以像下面这样使用 CONCAT 和 FROM_UNIXTIME

SELECT CONCAT(YEAR(FROM_UNIXTIME(1468215093)), MONTH(FROM_UNIXTIME(1468215093))) AS YEAR_MONTH

在您的查询中:

SELECT CONCAT(YEAR(FROM_UNIXTIME(upd_gmt_ts)), MONTH(FROM_UNIXTIME(upd_gmt_ts))) AS YEAR_MONTH
FROM abc.test;

我更喜欢使用 Hive date_format() (as of Hive 1.2.0). It support Java SimpleDateFormat 模式。

date_format() 接受 date/timestamp/string。所以你的最终查询将是

select date_format(upd_gmt_ts,'yyyyMM') from abc.test;

编辑:

SimpleDateFormat acceptable patterns examples.

请使用以下查询

SELECT YEAR(FROM_UNIXTIME(unix_timestamp()))*100 + MONTH(FROM_UNIXTIME(unix_timestamp()))

要查看 yyyy-mm-dd hh:mm:ss 格式的日期,您可以按如下方式使用:

select to_utc_timestamp(col_name, 'PST') * from table;