简体   繁体   English

如何在 hive 中将 unix 纪元时间转换为日期字符串

[英]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.

相关问题 将unix时间转换为GMT +1 - Convert unix time to GMT +1 如何在SQLite3中将日期时间字符串转换为UNIX时间戳? - How can I convert a datetime string to a UNIX timestamp in SQLite3? 将.NET日期/时间格式字符串转换为Javascript日期/时间格式字符串 - Convert .NET date/ time format string to Javascript date/ time format string SAS将字符串日期转换为日期并进行比较 - sas convert string date to date and compare them Python DataFrame:如何将时间(格式化为字符串)转换为秒? - Python DataFrame: How to convert time (formatted as a string) into seconds? 关于如何创建一个接受字符串并返回带有日期和时间的字符串的C函数的想法 - Idea on how to create a C function that takes a character string and returns a character string with date and time 将任意类似日期的字符串转换为Teradata中的日期 - Convert arbitrary date-like string to date in Teradata 如何将使用javascript中的toLocaleString()转换的字符串转换回Date对象? - How can I convert a string converted using toLocaleString() in javascript back to Date Object? 如何将字符串形式的日期(“2012年12月25日”)转换为一组整数(12/25/12)? - How can I convert a date in string form (“Dec 25, 2012”) into an group of integers (12/25/12)? 如何在 javascript 中将日期字符串从 'dd//mm/yy' 转换为 'mm/dd/yy'? - How to convert a date string from 'dd//mm/yy' to 'mm/dd/yy' in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM