[英]how to convert unix epoch time to date string in hive
I have a log file which contains timestamp column.我有一个包含时间戳列的日志文件。 The timestamp is in unix epoch time format.时间戳采用 unix 纪元时间格式。
I want to create a partition based on a timestamp with partitions year, month and day.我想根据带有分区年、月和日的时间戳创建一个分区。
So far I have done this but it is throwing an error.到目前为止,我已经这样做了,但它抛出了一个错误。
PARSE ERROR cannot recognize input '(' in column type
Here is my code.这是我的代码。
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;
Oof, that looks ugly.哎呀,长得丑。 Try using this function in Hive:尝试在 Hive 中使用此功能:
SELECT from_unixtime(unix_timestamp) as new_timestamp from raw_data ...
Or if timestamp is in ms
instead of seconds:或者,如果时间戳以ms
而不是秒为单位:
SELECT from_unixtime(unix_timestamp DIV 1000) as new_timestamp from raw_data ...
That converts a unix timestamp into a YYYY-MM-DD HH:MM:SS format, then you can use the following functions to get the year, month, and day:将 unix 时间戳转换为 YYYY-MM-DD HH:MM:SS 格式,然后您可以使用以下函数来获取年、月和日:
SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day ...
With more recent releases of Hive and SparkSQL, data type of date and type casting options are available.随着 Hive 和 SparkSQL 的更新版本,日期和类型转换选项的数据类型可用。 Following should work in Hive as well as Spark SQL以下应该适用于 Hive 和 Spark SQL
SELECT cast(from_unixtime(epoch_datetime) as date) from myHiveTable
If you need to convert the date in custom format, use this:如果您需要以自定义格式转换日期,请使用以下命令:
select date_format(from_unixtime(epoch_datetime),'yyyyMM') as formatted_date from myHiveTable;
which will return the date as yearMonth eg 201708这将返回日期为 yearMonth 例如 201708
Adding this query to the list where the timestamp needs to be converted to date string yyyy-MM-dd for a string partition:将此查询添加到需要将时间戳转换为字符串分区的日期字符串 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;
select order_id, date_format(from_unixtime(order_date/1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status
from orders
or if you see any error on the same , try to use select order_id, date_format(from_unixtime(order_date DIV 1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status from orders或者如果您看到任何错误,请尝试使用 select order_id, date_format(from_unixtime(order_date DIV 1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status from orders
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.