蜂巢中的日期格式转换 [英] Date Format Conversion in Hive

查看:106
本文介绍了蜂巢中的日期格式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的sql / hive。首先,我使用以下方式将一个txt文件加载到hive中:

  drop table if exists Tran_data; 
创建表Tran_data(tran_time string,
度假字符串,结算双)
ROW FORMAT DELIMITED FIELDS TERMINATED BY'\t'LINES TERMINATED BY'\\\
';
将数据本地inpath'C:\Users\me\Documents\transaction_data.txt'加载到表Tran_Data中;

txt文件中的变量 tran_time 像这样:10-APR-2014 15:01。加载此Tran_data表后,我尝试将 tran_time 转换为标准格式,以便我可以使用 tran_time 作为加入键。所需的日期格式为'yyyymmdd'。我搜索了在线资源,发现: unix_timestamp(substr(tran_time,1,11),'dd-MMM-yyyy')



所以基本上,我这样做: unix_timestamp('10 -APR-2014','dd-MMM-yyyy')。但是,输出为NULL。



所以我的问题是:如何将日期格式转换为标准格式,然后进一步将其转换为yyyymmdd '格式?

解决方案

  from_unixtime(unix_timestamp('20150101','yyyyMMdd' 'yyyy-MM-dd')


I'm very new to sql/hive. At first, I loaded a txt file into hive using:

drop table if exists Tran_data;
create table Tran_data(tran_time string, 
resort string, settled double)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Load data local inpath 'C:\Users\me\Documents\transaction_data.txt' into table Tran_Data;

The variable tran_time in the txt file is like this:10-APR-2014 15:01. After loading this Tran_data table, I tried to convert tran_time to a "standard" format so that I can join this table to another table using tran_time as the join key. The date format desired is 'yyyymmdd'. I searched online resources, and found this: unix_timestamp(substr(tran_time,1,11),'dd-MMM-yyyy')

So essentially, I'm doing this: unix_timestamp('10-APR-2014','dd-MMM-yyyy'). However, the output is "NULL".

So my question is: how to convert the date format to a "standard" format, and then further convert it to 'yyyymmdd' format?

解决方案

from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd') 

这篇关于蜂巢中的日期格式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆