一、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笔记:时间格式的统一的更多相关文章

  1. python获取hive表时间格式最大分区

    #获取表的最大分区 import boto3 from datetime import datetime,timedelta def get_max_partition(db_name,table_n ...

  2. Hive presto和hive时间格式转换

    1.北京时间格式   to   unix时间格式 数据格式: 2017-11-17 08:28:13 2017-11-17 08:28:10 2017-11-17 08:27:51.343 2017- ...

  3. 【,NetCore】WebApi使用统一时间格式

    1.在Startup中配置统一时间格式 services.AddMvc() .AddJsonOptions(options => { //配置时间序列化格式 options.Serializer ...

  4. JS中常用的几种时间格式处理-【笔记整理】

    //此处整理点平时常用到的时间格式处理方法 ------------------------------------------- //时间格式化函数 Date.prototype.format = ...

  5. Hive 笔记

    DESCRIBE EXTENDED mydb.employees  DESCRIBE EXTENDED mydb.employees DESCRIBE EXTENDED mydb.employees ...

  6. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  7. Newtonsoft.Json 序列化和反序列化 时间格式 [转]

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  8. MySQL时间戳和时间格式转换函数

    MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...

  9. 将与系统时间格式不同的字符串格式化为DATETIME类型

    若系统时间格式为2012/03/05 08:12:12,那么若将("2012-03-05 08:12:12")格式化为时间变量时会报错,在转化之前先将系统时间格式改变再转换就不会报 ...

随机推荐

  1. 最好用的编辑器之一:Vim-Go环境搭建

    本文由Librant发表 如果说在Linux环境下,什么编辑器最好用,如果我说是VIM,估计会有一大部分人嗤之以鼻,怎么可能.VIM可能是他用过众多编辑器最难用的一个.在我司用的是云虚拟机,说实话吧, ...

  2. Ubuntu16.04配置静态IP地址

    ubuntu如何设置静态IP? 设置静态IP 1.编辑/etc/network/interfaces文件: # This file describes the network interfaces a ...

  3. Perl构建和打包自己的模块

    当写好一个或多个模块后,可以将它构建.打包成"tar.gz",以便让别人安装或者上传到CPAN(如果愿意的话).对于模块的使用者来说,也不用再使用use lib 'LIB_PATH ...

  4. MySQL系列详解七:MySQL双主架构演示-技术流ken

    前言 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mys ...

  5. [转]Windows Server 2016 服务器IIS配置

    本文转自:https://blog.csdn.net/corson/article/details/82185407 多余的话就不说了,配置Windows Server 2016服务器具体如下图    ...

  6. 应用TortoiseGit为github账号添加SSH keys,解决pull总是提示输入密码的问题

    每次同步或者上传代码到githun上的代码库时,需要每次都输入用户名和密码,这时我们设置一下SSH key就可以省去这些麻烦了.若果使用TortoiseGit作为github本地管理工具,Tortoi ...

  7. 【Oracle 11gR2】静默安装 db_install.rsp文件详解

    #################################################################### ## Copyright(c) Oracle Corporat ...

  8. Android Material Design控件使用(二)——FloatButton TextInputEditText TextInputLayout 按钮和输入框

    FloatingActionButton 1. 使用FloatingActionButton的情形 FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App ...

  9. Java多线程之wait、notify/notifyAll 详解,用wait 和notifyAll 以及synchronized实现阻塞队列,多线程拓展之ReentrantLock与Condition

    前言:这几天看了很多关于多线程的知识,分享一波.(但是目前接触的项目还未用到过,最多用过线程池,想看线程池 请看我之前的博客) 关于基本的理论等 参考如下: https://www.cnblogs.c ...

  10. Unix awk的流程控制BEGIN和END的讲解

    你可能对Unix比较熟悉,但你可能对Unix awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk还远没达到它应有的知名度. 流程控制语句是任何程序设计语言都不能缺少的部分.任何好的语言都有 ...