Hive:从interval_day_time提取毫秒?

时间:2016-12-20 20:34:39

标签: hive milliseconds

我希望从某些日期时间增量中获得毫秒精度。我没有看到Hive的毫秒()函数。

考虑一下:

with t as (
    select (CAST(1481652239798 AS TIMESTAMP) - CAST(1481652228576 as timestamp)) 
    as delta
) 
select delta from t;

0 00:00:11.222000000

我可以使用它,如果我可以将该输出转换为字符串并在句点之后提取部分。

with t as (
    select (CAST(1481652239798 AS TIMESTAMP) - CAST(1481652228576 as timestamp)) 
    as delta
) 
select instr(delta, '.') from t

11 -- correct index of '.'

所以instr()将delta视为一个字符串,但我不能将其子串:

with t as (
    select (CAST(1481652239798 AS TIMESTAMP) - CAST(1481652228576 as timestamp)) 
    as delta
) 
select substr(delta, 11) from t; -- directly supplying instr() leads to a different bug with parsing the query syntax

No matching method for class org.apache.hadoop.hive.ql.udf.UDFSubstr with (interval_day_time, int)

有任何解决方法吗?

1 个答案:

答案 0 :(得分:0)

如果将timestamp对象强制转换为double,则会保留毫秒部分。

请尝试以下方法:

with t as (
    select CAST(1481652239798 AS TIMESTAMP) as ts1,  
           CAST(1481652228576 as timestamp) as ts2             
) 
select ts1,
       ts2,
       (ts1-ts2) as delta, 
       floor((CAST(ts1 AS double)-CAST(ts2 as double))*1000) as delta_ms
from t