1. 程式人生 > >hive筆記:時間格式的統一

hive筆記:時間格式的統一

問題 圖片 類型 參數 inf cli 統一 切換 sele

一、string類型,年月日部分包含的時間統一格式:

原數據格式(時間字段為string類型) 取數時間和格式的語法
2018-11-01 00:12:49.0 substr(regexp_replace(created_at,‘-‘,‘‘),0,8)>=‘20181101‘
month=201809,day=01 concat(month,day)>= ‘20180901‘
dt=181101 concat(‘20‘,a.dt)>=‘20181101’

二、日期函數(時間戳)以及各種格式的時間截取,轉換方法

1.from_unixtime(bigint unixtime[, string format]):將是將戳轉化為日期

將時間的秒值轉換成format格式(format可為“yyyy-MM-dd hh:mm:ss”,“yyyy-MM-dd hh”,“yyyy-MM-dd hh:mm”等等)如from_unixtime(1250111000,"yyyy-MM-dd") 得到2009-03-12

(1)時間戳為13位的情況:

hive中from_unixtime可以將一個時間戳轉為時間格式,如: hive> select from_unixtime(1445391280,‘yyyy-MM-dd HH:mm:ss‘); 2015-10-21 09:34:40 問題: 其中第一個參數為bigint型數據,一般是10位的,遇到13位的時間戳,需要去掉最後三位才行,但是bigint型數據不支持直接算數運算,也不支持字符串截取
如,13位時間戳直接轉換 hive> select from_unixtime(1445391280000,‘yyyy-MM-dd HH:mm:ss‘); 47772-08-17 01:46:40 兩種方法處理此問題: a.一種是將bigint型數據先轉成double型計算之後再轉成bigint型, hive> select from_unixtime(cast(cast(1445391280000 as double)/1000 as bigint),‘yyyy-MM-dd HH:mm:ss‘); 2015-10-21 09:34:40 b.另一種是將bigint型數據轉成string型,截取之後再轉回bigint型。
hive> select from_unixtime(cast(substr(cast(1445391280 as string),1,10) as bigint),‘yyyy-MM-dd HH:mm:ss‘); 2015-10-21 09:34:40

(2) 案例:時間戳為13位的情況

%jdbc(hive) select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-mm-dd‘) as dt1, from_unixtime(cast(substr(pc.ttl,0,10) as int),‘yy-MM-dd HH:mm:ss‘) as dt2, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyyy-mm-dd‘) as dt3, from_unixtime(cast(substr(pc.ttl,0,10) as int),‘yyyy-MM-dd HH:mm:ss‘) as dt4 from xxxx pc 技術分享圖片

(3)yy-MM-dd和yyMMdd時分秒的劃取方法(註意本表中的ttl為string類型)

a.yyMMdd:

%jdbc(hive) select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyMMdd HH:mm:ss‘) as dt1, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyMMdd HH:mm‘) as dt2, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyMMdd HH‘) as dt3, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyMMdd‘) as dt4, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yyMM‘) as dt3, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy‘) as dt4 from xxxx pc 技術分享圖片

b.yy-MM-dd:

%jdbc(hive) select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-MM-dd HH:mm:ss‘) as dt1, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-MM-dd HH:mm‘) as dt2, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-MM-dd HH‘) as dt3, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-MM-dd‘) as dt4, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy-MM‘) as dt5, from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), ‘yy‘) as dt6 from xxxx pc 技術分享圖片

2.unix_timestamp獲取當前UNIX時間戳函數:(將日期轉化為時間戳)

(1) unix_timestamp()

返回值: bigint 說明: 獲得當前時區的UNIX時間戳

(2) unix_timestamp(string date)

返回值: bigint 說明: 轉換格式為“yyyy-MM-dd HH:mm:ss“的日期到UNIX時間戳。如果轉化失敗,則返回0。

(3)unix_timestamp(string date, string pattern)

返回值: bigint 說明: 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0。

(4)案例如下(yy-MM-dd和yyMMdd兩種時間格式):最好使用unix_timestamp(string date, string pattern)轉化,表明時間格式

a.yyMMdd

%jdbc(hive) select a.dt as time,unix_timestamp(a.dt) as time1, unix_timestamp(a.dt,‘yyMMdd‘) as time2, concat(‘20‘,a.dt) as dt0,unix_timestamp(concat(‘20‘,a.dt)) as dt1, unix_timestamp(concat(‘20‘,a.dt),‘yyyyMMdd‘) as dt2 from track.click a where concat(‘20‘,a.dt)>=‘20181101‘ and concat(‘20‘,a.dt)<=‘20181103‘ limit 100 技術分享圖片

b.yyyy-MM-dd

%jdbc(hive) select created_at, unix_timestamp(created_at) created_at1, substr(created_at,1,10)as dt0, unix_timestamp(substr(created_at,1,10))dt1, unix_timestamp(substr(created_at,1,10),‘yyyy-MM-dd‘) dt2 from trial_sdk.device where created_at>=‘2018-11-01‘ and created_at<=‘2018-11-03‘ limit 100 技術分享圖片

3.yymmdd和yy-mm-dd日期的切換

方法1: from_unixtime+ unix_timestamp

a.20171205轉成2017-12-05 select from_unixtime(unix_timestamp(‘20171205‘,‘yyyymmdd‘),‘yyyy-mm-dd‘) from dual; b.2017-12-05轉成20171205 select from_unixtime(unix_timestamp(‘2017-12-05‘,‘yyyy-mm-dd‘),‘yyyymmdd‘) from dual; 如:from_unixtime(unix_timestamp(ts),‘yyMMdd‘) 其中ts類型為timestamp,2018-10-11 04:05:29.028

方法2: substr + concat

a.20171205轉成2017-12-05 select concat(substr(‘20171205‘,1,4),‘-‘,substr(‘20171205‘,5,2),‘-‘,substr(‘20171205‘,7,2)) from dual; b.2017-12-05轉成20171205 select concat(substr(‘2017-12-05‘,1,4),substr(‘2017-12-05‘,6,2),substr(‘2017-12-05‘,9,2)) from dual;

技術分享圖片

hive筆記:時間格式的統一