복붙노트

[HADOOP] Hive 쿼리의 시간적 조인 (시간적으로 근접한 이벤트)

HADOOP

Hive 쿼리의 시간적 조인 (시간적으로 근접한 이벤트)

내가 알아내는 데 어려움을 겪고있는 하이브 쿼리에 대한 필요성이 있습니다.

나는 다음과 같은 시계열을 가지고있다.

time                          source    word1   word2    ...etc
2012-02-01 23:43:16.9988243    0001      2B3B    FAF0
2012-02-01 23:43:16.9993561    0002      2326    ABAA
2012-02-01 23:43:16.9998879    0002      2327    ABAA

하나의 소스에있는 레코드가 해당 레코드 외에도 특정 조건을 충족 시키면 다른 조건 세트를 충족시키는 두 번째 소스로부터 한 번에 근접한 하나 이상의 레코드를 반환해야한다는 쿼리가 필요합니다.

내 시도는 지금까지 다음과 같이 보입니다.

SELECT time
    FROM messages C
    JOIN messages D on
       D.time
           BETWEEN C.time - INTERVAL '0.001' SECOND
           AND     C.time + INTERVAL '0.001' SECOND

    WHERE C.source = '0001'
    AND   D.Source = '0002'
    AND   C.word1 = '2B3B'
    AND   D.word2 = 'ABAA'

위의 예제 데이터에서 첫 번째와 두 번째 레코드를 반환해야합니다 (시간이 0.001 초보다 짧기 때문에 세 번째 레코드를 반환하지 않아야 함).

그러나 쿼리가 작동하지 않습니다. 오류 메시지는 다음과 같습니다.

FAILED: SemanticException '0.001' encountered with 0 children

해결법

  1. ==============================

    1.이것은 순진한 해결책 일 것입니다.

    이것은 순진한 해결책 일 것입니다.

    select  *
    
    from                    messages c
            cross join      messages m 
    
    where   m.time  between c.time - interval '0.001' second
                    and     c.time + interval '0.001' second
    
        and c.word1 = '2B3B'
        and m.word2 = 'ABAA'
    
    ;
    
    +----------------------------+--------+-------+-------+----------------------------+--------+-------+-------+
    |            time            | source | word1 | word2 |            time            | source | word1 | word2 |
    +----------------------------+--------+-------+-------+----------------------------+--------+-------+-------+
    | 2012-02-01 23:43:16.998824 |   0001 | 2B3B  | FAF0  | 2012-02-01 23:43:16.999356 |   0002 |  2326 | ABAA  |
    +----------------------------+--------+-------+-------+----------------------------+--------+-------+-------+
    

    이것은 좋은 성능을 가진 솔루션입니다.

    select  *
    
    from                    messages c
    
            join            messages m
    
            on              floor (cast(c.time as decimal(37,7)) / (2 * 0.001))   =
                            floor (cast(m.time as decimal(37,7)) / (2 * 0.001))
    
    where   m.time  between c.time - interval '0.001' second
                    and     c.time + interval '0.001' second
    
        and c.word1 = '2B3B'
        and m.word2 = 'ABAA'
    
    union all
    
    select  *
    
    from                    messages c
    
            join            messages m
    
            on              floor ((cast(c.time as decimal(37,7)) + 0.001) / (2 * 0.001))   =
                            floor ((cast(m.time as decimal(37,7)) + 0.001) / (2 * 0.001))
    
    where   floor (cast(c.time as decimal(37,7)) / (2 * 0.001))     <>
            floor (cast(m.time as decimal(37,7)) / (2 * 0.001))
    
        and m.time  between c.time - interval '0.001' second
                    and     c.time + interval '0.001' second
    
        and c.word1 = '2B3B'
        and m.word2 = 'ABAA'
    
    +----------------------------+--------+-------+-------+----------------------------+-------+-------+-------+
    |            time            | source | word1 | word2 |           _col4            | _col5 | _col6 | _col7 |
    +----------------------------+--------+-------+-------+----------------------------+-------+-------+-------+
    | 2012-02-01 23:43:16.998824 |   0001 | 2B3B  | FAF0  | 2012-02-01 23:43:16.999356 |  0002 |  2326 | ABAA  |
    +----------------------------+--------+-------+-------+----------------------------+-------+-------+-------+
    

    이벤트 A와 B는 UNION ALL의 위쪽 부분에 의해 잡힐 것입니다. 이벤트 B와 C는 UNION ALL의 하위 부분에 의해 잡힐 것입니다.

        0        0.002    0.004    0.006    0.008    0.01      
        |        |        |        |        |        |
    -------------------------------------------------------
                          |        |
                          |        |
                              A  B  C
                               |        |
                               |        |
    -------------------------------------------------------
             |        |        |        |        |                
             0.001    0.003    0.005    0.007    0.009
    
  2. from https://stackoverflow.com/questions/45240981/temporal-join-in-hive-query-events-in-close-proximity-in-time by cc-by-sa and MIT license