将 hive 中的时间戳转换为日期

converting timestamps in hive to date

我在配置单元列中有这种格式的数据。

09/05/2015  12:45:00

我正在尝试将其转换为这种格式


20150905, or with semicolons - 2015-09-05

不经过 linux 时间戳的最有效方法是什么?

使用date_format.
如果您的数据是 String/Timestamp/Date 格式,
并且您想要格式为 yyyy-MM-dd 的字符串,然后使用- date_format(date_time_col, 'yyyy-MM-dd')
你想要一个日期格式,然后使用 - cast( date_format(date_time_col, 'yyyy-MM-dd') as date)

编辑 如果你有一个字符串 'MM/dd/yyyy',你需要在将它们发送到 date_format.

之前拆分字符串并连接它们
date_format( 
concat_ws('-', substr(split(date_time_col,'/')[2],1,4),split(date_time_col,'/')[0], split(date_time_col,'/')[1] )
, 'yyyy-MM-dd') 

你可以像这样组合所有 -

case when date_format(date_time_col, 'yyyy-MM-dd') is null 
date_format( 
concat_ws('-', substr(split(date_time_col,'/')[2],1,4),split(date_time_col,'/')[0], split(date_time_col,'/')[1] )
, 'yyyy-MM-dd') 
else date_format(date_time_col, 'yyyy-MM-dd')
end