[hive] 날짜 구하기

hadoop 2016. 2. 26. 19:21



hive에서 날짜를 구하는 방법을 작성한 예시 코드이다.


오늘 날짜를 구하려면 다음과 같이 unix_timestamp를 이용하거나 unix_timestamp와 from_unixtime() 함수를 이용한 방식이 있다. 사람이 읽을 수 있는 함수를 보려면 from_unixtime() 함수를 잘 활용한다.


> select unix_timestamp();

1456481927


> select from_unixtime(unix_timestamp());

2016-02-26 19:21:52

 

YYYYMMDD와 같은 형태로 날짜의 특정 항목만 얻고 싶다면, year(), month()등과 같은 함수를 사용한다. 


unix_timestamp을 사용하는 방식을 사용하면 에러가 발생한다. 


> select year(unix_timestamp()), month(unix_timestamp()), day(unix_timestamp()), hour(unix_timestamp()), minute(unix_timestamp()), second(unix_timestamp());

 


current_timestamp이나 current_date로 현재 날짜를 얻을 수 있다. 두 개의 큰 차이는 current_date는 hour, minute, second 함수를 함께 사용할 수 없다.



> select year(current_timestamp), month(current_timestamp), day(current_timestamp), hour(current_timestamp), minute(current_timestamp), second(current_timestamp);

 2016 2 26 19 21 3

 


> select year(current_date), month(current_date), day(current_date), hour(current_date), minute(current_date), second(current_date);

 2016 2 26 NULL NULL NULL


Posted by '김용환'
,