Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive - month and year from timestamp column

Hi I am trying to extract the month and year part of a timestamp column in hive using the below query

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

The output looks like 2016-05-20 01:08:48

the desired output should be 201605

Appreciate any suggestions.

like image 725
divyanair Avatar asked Jan 10 '17 06:01

divyanair


Video Answer


2 Answers

I'd prefer to use Hive date_format() (as of Hive 1.2.0). It support Java SimpleDateFormat patterns.

date_format() accepts date/timestamp/string. So your final query will be

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

Edit:

SimpleDateFormat acceptable patterns examples.

enter image description here

like image 50
mrsrinivas Avatar answered Oct 21 '22 18:10

mrsrinivas


You can use CONCAT and FROM_UNIXTIME like below:

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

In your query:

SELECT CONCAT(YEAR(FROM_UNIXTIME(upd_gmt_ts)), MONTH(FROM_UNIXTIME(upd_gmt_ts))) AS YEAR_MONTH
FROM abc.test;
like image 34
Pathik Vejani Avatar answered Oct 21 '22 18:10

Pathik Vejani