【问题标题】:How to Convert date field in yyyy-MM-dd-hh.mm.ss” to Timestamp field(yyyy-MM-dd hh:mm:ss) in Hive)如何将 yyyy-MM-dd-hh.mm.ss 中的日期字段转换为 Hive 中的时间戳字段(yyyy-MM-dd hh:mm:ss))
【发布时间】:2018-01-17 00:15:59
【问题描述】:

我有一个来自 db2 源的格式为 yyyy-MM-dd-hh.mm.ss 的日期字段。我想加载到配置单元并转换为时间戳。

我如何实现它?

【问题讨论】:

  • 我尝试使用这个 concat(concat(substr(datefield,1,10),' '),substr(datefield,12,26)) 它工作正常......但是当我使用 CAST (concat(concat(substr(datefield,1,10),' '),substr(datefield,12,26)) AS TIMESTAMP) 给我NULL...任何帮助表示赞赏。
  • 编辑您的问题并将您的代码放在那里
  • 已经是hive的时间戳格式了,保持数据类型为时间戳就可以了。请参阅此文档中的时间戳:cwiki.apache.org/confluence/display/Hive/…

标签: hive unix-timestamp


【解决方案1】:

您的源数据库在小时、分钟和秒之间有dot。 Hive 在它们之间支持:,例如:yyyy-MM-dd HH:mm:ss

参考:Hive Date Functions

select
cast(
  concat(
    substr('2015-07-22-09.00.32',1,10), ' ', 
    substr('2015-07-22-09.00.32',12,2), ':', 
    substr('2015-07-22-09.00.32',15,2), ':', 
    substr('2015-07-22-09.00.32',18,2)
  ) AS TIMESTAMP
)
;

【讨论】:

  • 是的...我试过了,但是 CAST as TIMESTAMP 给出的是 Null 值。
【解决方案2】:

您可以使用unix_timestampfrom_unixtime 的组合来代替您当前使用的substr 方法。

select cast( 
           from_unixtime(
                unix_timestamp('2017-08-31-12:24:48' , 'yyyy-MM-dd-HH:mm:ss') 
           )
        as timestamp
);

+------------------------+--+
|          _c0           |
+------------------------+--+
| 2017-08-31 12:24:48.0  |
+------------------------+--+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2013-10-19
    相关资源
    最近更新 更多