配置单元:从间隔\u天\u时间提取毫秒?

mzsu5hc0  于 2021-06-28  发布在  Hive
关注(0)|答案(1)|浏览(388)

我想从一些日期时间增量中得到毫秒精度。我在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)

有解决办法吗?

lsmd5eda

lsmd5eda1#

如果你投下 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

相关问题