Function 如何在配置单元中将unix历元时间转换为日期字符串
我有一个包含时间戳列的日志文件。时间戳采用unix历元时间格式 我想创建一个基于时间戳的分区,包括年、月和日分区 到目前为止,我已经这样做了,但它抛出了一个错误Function 如何在配置单元中将unix历元时间转换为日期字符串,function,hive,database-partitioning,Function,Hive,Database Partitioning,我有一个包含时间戳列的日志文件。时间戳采用unix历元时间格式 我想创建一个基于时间戳的分区,包括年、月和日分区 到目前为止,我已经这样做了,但它抛出了一个错误 PARSE ERROR cannot recognize input '(' in column type 这是我的密码 from ( from raw_data MAP ${PREFIX}raw_data.line USING 's3://scripts/clean.py
PARSE ERROR cannot recognize input '(' in column type
这是我的密码
from (
from raw_data
MAP ${PREFIX}raw_data.line
USING 's3://scripts/clean.py'
AS (timestamp STRING, name STRING)
) map_out
INSERT OVERWRITE TABLE date_base_data_temp PARTITION(year(timestamp), month(timestamp)), day(timestamp)))
select map_out.name;
哦,看起来很难看。尝试在配置单元中使用此功能:
SELECT from_unixtime(unix_timestamp) as new_timestamp from raw_data ...
或者如果时间戳的单位是ms
而不是秒:
SELECT from_unixtime(unix_timestamp DIV 1000) as new_timestamp from raw_data ...
将unix时间戳转换为YYYY-MM-DD HH:MM:SS格式,然后可以使用以下函数获取年、月和日:
SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day ...
在最近发布的Hive和SparkSQL中,可以使用日期和类型转换选项的数据类型。以下内容应该在配置单元和Spark SQL中都可以使用
SELECT cast(from_unixtime(epoch_datetime) as date) from myHiveTable
如果需要将日期转换为自定义格式,请使用以下命令:
select date_format(from_unixtime(epoch_datetime),'yyyyMM') as formatted_date from myHiveTable;
将日期返回为yearMonth,例如201708,将此查询添加到需要将时间戳转换为日期字符串yyyy-MM-dd的列表中,用于字符串分区:
hive> select date_format(from_unixtime(epoch_datetime), 'yyyy-MM-dd') as day from table_name limit 20;
-- If required, remove the millis precision for timestamps
hive> select date_format(from_unixtime(cast(epoch_datetime/1000 as bigint)), 'yyyy-MM-dd') as day from table_name limit 20;
或者,如果您在同一页上看到任何错误,请尝试使用
选择订单id、日期格式(从unixtime(订单日期DIV 1000),'yyy-MM-dd')作为订单日期、订单客户id、订单状态
来自订单谢谢!节省了我很多时间。这正是我要找的!确保
timestamp\u值
(此处unix\u timestamp
)以秒为单位,否则使用from\u unixtime(timestamp\u value DIV 1000)
我得到的时间只有秒,但我还需要毫秒。我该怎么做?@shriyog我有纪元格式的日期。该值为15137082000。请帮助我完成将值转换为日期的查询format@BasilPaul正如我所提到的,您需要将时间戳值转换为秒,然后从unixtime(timestamp\u value DIV 1000)传递-
日期\u格式是不必要的,from_unixtime
可以接收另一个格式参数:从表中选择from_unixtime(历元日期时间,'yyyy-MM-dd')作为日期\u name limit 20代码>@JulianQian你是对的。我使用了你更简洁的版本,效果很好。我想你可能在格式说明符中遗漏了一个y
。
select order_id, date_format(from_unixtime(order_date/1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status
from orders