如何在配置单元中获取给定时区的当前时间

How to get current time for a given timezone in hive

我在 Hive 中有 2 个表。

表 1 包含

timelocation
2015-03-04 15:00 Chicago
2015-03-04 15:00 Denver
2015-03-04 15:00 Honolulu

表 2 包含

ID                       Description
America/Chicago          CENTRAL STANDARD TIME                              
America/Denver           MOUNTAIN  STANDARD TIME                            
Pacific/Honolulu         HAWAII-ALEUTIAN STANDARD TIME                       

对于表 1 中的记录说“2015-03-04 15:00 芝加哥”,我需要在表 2 中查找相应的芝加哥记录。它应该读取芝加哥的 ID 和描述以及 return 芝加哥当前的中央标准时间,即“2015-05-04 09:11”。

同样,对于丹佛,它必须 return 山区标准时间;对于火奴鲁鲁,它必须 return 夏威夷-阿留申标准时间。

预期输出为

timelocation
2015-05-04 09:11
2015-05-04 08:11
2015-05-04 04:11

我该怎么做?

我真的不想为您写这个查询,但希望这会为您指明正确的方向。您需要加入 tbl1 <=> tbl2。从每个 table 中的相应列中提取城市; split() 功能会帮助你。然后你可以找到一个漂亮的函数 here called from_utc_timestamp() that takes a time stamp (assumed to be UTC) and converts it to a given time zone. You'll also need to convert the column Description to its time zone abbreviation. You can find those here。祝你好运!

试试这个 to_utc_timestamp('2015-01-01 00:00:00','PST')

returns 2015-01-01 08:00:00

您需要使用以下查询加入 table1table2

SELECT T1.timelocation,T2.ID,  T2.Description, to_utc_timestamp(SUBSTRING(T1.timelocation,1,16),T2.ID) AS newtime
FROM table 1 T1 INNER JOIN table2 T2 ON SUBSTRING(T1.timelocation,17)= SUBSTRING(regexp_extract(T2.ID,'/\w*',2),2) 

这里正则表达式函数的输出假设 America/Chicago 将是 /Chicago ,因此我在输出上做一个子字符串以提取 Chicago 并匹配 timelocation列。

有关配置单元正则表达式函数的更多信息 regexp_extract(string subject, string pattern, int index) 检查 @Hive Doc