【问题标题】:Convert string to date with date and time together in Hive在 Hive 中将字符串转换为带有日期和时间的日期
【发布时间】:2020-02-12 19:26:31
【问题描述】:

我需要将字符串转换为日期时间(日期和时间一起)。

我试试这个:

cast(to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'))) as date) as date_data_chamada

时区:巴西

但是这种方式只返回日期,像这样:2019-03-03,我需要:2019-03-03 16:43:05

谢谢!

完整代码:

INSERT INTO p_b.este PARTITION (dt_originacao_fcdr)
SELECT
tp_registro_fcdr,
seq_registro_fcdr,
tp_cdr_fcdr,
dt_atendimento_fcdr,
data_atendimento_completa_fcdr,
cast(from_unixtime(unix_timestamp(data_atendimento_completa_fcdr, 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")as timestamp) as date_data_atendimento_fcdr,
hr_atendimento_fcdr,
duracao_atend_fcdr,
hr_originacao_fcdr,
duracao_total_fcdr,
duracao_chamada_tarifada_fcdr,
st_chamada_fcdr,
fim_sel_orig_fcdr
FROM p_b.norm;

【问题讨论】:

    标签: datetime hive type-conversion hiveql


    【解决方案1】:

    删除date 转换和to_date 函数,因为您期望时间戳!

    Example:

    hive> select from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss") as date_data_chamada;
    

    RESULT:

    2019-03-03 16:43:05
    

    如果您使用 to_datecast('string' as date),则 hive 结果仅 date(yyyy-MM-dd)

    例如:

    hive> select to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")) as date_data_chamada;
    
    --2019-03-03
    

    【讨论】:

    • 但是 date_data_chamada 的数据类型是日期,使用时间戳是字符串。我对吗?即使使用时间戳,仍然只返回日期。 (2019-03-03)
    • 谢谢,@shu。但我正在做你告诉我的事情,没有任何改变。我的回报:data_atendimento_completa_fcdr 20190303131615(我正在尝试转换的列) date_data_atendimento_fcdr 2019-03-03(返回您的代码)
    • 使用插入功能?因为,选择它工作......插入剂量(我的情况)
    • 正如聊天中所讨论的,final table 中定义的类型是date 类型,并且在将数据插入 table hive 时将仅插入yyyy-MM-dd,在更改为timestamp 类型后修复了问题!
    • 我需要两个日期之间的差异(时间戳),我应该使用什么类型来返回?前任。时间戳_1 - 时间戳_2 = 差异
    【解决方案2】:

    将第二个参数格式字符串传递给from_unixtime。注意返回的类型是string

    from_unixtime(unix_timestamp('20190303164305','yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多