在hive中对日期数据进行处理,毫秒级时间转化为yyyy-MM-dd格式
生活随笔
收集整理的這篇文章主要介紹了
在hive中对日期数据进行处理,毫秒级时间转化为yyyy-MM-dd格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們看到日志的時間是1414381913000 ,我們在需求中需要對每天的數(shù)據(jù)進(jìn)行分析,所以需要對這個日志數(shù)據(jù)進(jìn)行轉(zhuǎn)化格式。
常用時間類型格式轉(zhuǎn)換:
from_unixtime(bigint unixtime,[string format]): 時間戳轉(zhuǎn)日期函數(shù), unix_timestamp([string date]): 轉(zhuǎn)換成時間戳,然后轉(zhuǎn)換格式為“yyyy-MM-dd HH:mm:ss“的日期 到UNIX時間戳。如果轉(zhuǎn)化失敗,則返回0,返回bigint類型 to_date(string timestamp): 將時間戳轉(zhuǎn)換成日期,默認(rèn)格式為2011-12-08 10:03:01 year() : 將時間戳轉(zhuǎn)換成年,默認(rèn)格式為2011-12-08 10:03:01 month() : 將時間戳轉(zhuǎn)換成月,默認(rèn)格式為2011-12-08 10:03:01 hour() : 將時間戳轉(zhuǎn)換成小時,默認(rèn)格式為2011-12-08 10:03:01 day(string date) : 將時間戳轉(zhuǎn)換成天,默認(rèn)格式為2011-12-08 10:03:01 date_diff(string enddate, string startdate) : 日期比較函數(shù),反回結(jié)束日期減去開始日期的 天數(shù) date_sub(string startdate, int days) : 日期減少函數(shù),返回開始日期減少days天后的日期字符 串 date_add(string startdate, int days) : 日期增加函數(shù),返回開始日期增加days天后的日期字符 串 last_day(string date) : 返回該月的最后一天的日期,可忽略時分秒部分(HH:mm:ss)。 last_day(string date)返回string類型的值。 next_day(string date,string x) : 返回下一個星期x的日期(x為前兩英文星期前兩位或者全寫 MONDAY),返回字符串。 current_date() : 獲取當(dāng)天的日期,返回字符串,沒有任何的參數(shù)。 current_timestamp() : 獲取當(dāng)前的時間戳然后就是一頓操作:
放心這些都是實驗品,哈哈
真正的語句:
select from_unixtime(cast(substr(LogTime,0,10) as bigint),"yyyy-MM-dd") from CharLogin limit 1; 0: jdbc:hive2://henu2:10000> select from_unixtime(cast(substr(LogTime,0,10) as bigint),"yyyy-MM-dd") from CharLogin limit 1; +-------------+--+ | _c0 | +-------------+--+ | 2014-10-27 | +-------------+--+解決!!!
對了,如果你是這樣的需求:
select from_unixtime(cast(substr(LogTime,0,10) as bigint),"yyyyMMdd") from CharLogin limit 1; 0: jdbc:hive2://henu2:10000> select from_unixtime(cast(substr(LogTime,0,10) as bigint),"yyyyMMdd") from CharLogin limit 1; +-----------+--+ | _c0 | +-----------+--+ | 20141027 | +-----------+--+?改格式就行了!!!
?
總結(jié)
以上是生活随笔為你收集整理的在hive中对日期数据进行处理,毫秒级时间转化为yyyy-MM-dd格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅析Hadoop的三个作业调度器
- 下一篇: 游戏数仓分析(一)数据准备阶段