2016-12-20 14 views
1

필자는 필자가 가지고있는 일부 datetime 델타에서 밀리 초 정밀도를 얻고 싶습니다. Hive에서 볼 수있는 millisecond() 함수는 없습니다.하이브 : interval_day_time에서 밀리 초를 추출 하시겠습니까?

이 고려 : 나는 문자열로 출력하는 변환 및 기간 다음과 같은 부분을 추출 할 수 있다면

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()는 문자열로 델타를 취급하지만 난 그것을 하위 문자열 수 없습니다

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) 

대안?

+0

내가 하이브를 알고 있지만하지 않는 : 당신은 슬라이스 한 후, 새로운 변수를 문자열로 저장할 수 없습니다? –

+0

이것은 미친입니다. 유닉스 시대부터 단위가 ** 밀리 초 ** 인 숫자로 시작한 다음 이것을 시간 소인으로 변환 한 다음 시차를 계산 한 다음 다시 밀리 초로 돌아가고 싶습니다. 왜 당신은 당신의 원래 숫자를 빼고, 행복하게 지내십니까? –

+0

정확히 당신이하려고하는 것은 저 밖에 있습니다. 그러나 Hive가 타임 스탬프에서 문자열 ([Documentation] (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types#LanguageManualTypes-AllowedImplicitConversions))으로의 암시 적 캐스트를 허용하더라도 분명히 그렇지 않습니다. 그 기능. 나는 이런 이유로 암시 적 캐스트를 싫어한다. – Andrew

답변

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 
관련 문제