복붙노트

[HADOOP] Hadoop / Hive 수집 목록 - 반복되는 항목 없음

HADOOP

Hadoop / Hive 수집 목록 - 반복되는 항목 없음

게시물, 하이브 0.12 - Collect_list를 기반으로,이 또는 이와 유사한 기능을 수행하지만 반복 시퀀스없이 UDAF를 구현하는 Java 코드를 찾으려고합니다.

예를 들어, collect_all ()은 시퀀스 A, A, A, B, B, A, C, C를 반환합니다. 시퀀스 A, B, A, C가 반환되도록하고 싶습니다. 연속적으로 반복되는 항목은 제거됩니다.

누구든지 하이브 0.12의 기능을 알고 있거나 자신의 UDAF를 작성 했습니까?

항상 그렇듯이 도움을 주셔서 감사합니다.

해결법

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

    1.나는 비슷한 문제를 잠시 만났다. 나는 full-on UDAF를 작성하지 않으려 고했기 때문에 brickhouse 수집과 내 UDF로 콤보를 만들었습니다. 이 데이터가 있다고 가정 해 보겠습니다.

    나는 비슷한 문제를 잠시 만났다. 나는 full-on UDAF를 작성하지 않으려 고했기 때문에 brickhouse 수집과 내 UDF로 콤보를 만들었습니다. 이 데이터가 있다고 가정 해 보겠습니다.

    id  value
    1   A
    1   A
    1   A
    1   B
    1   B
    1   A
    1   C
    1   C
    1   D
    2   D
    2   D
    2   D
    2   D
    2   F
    2   F
    2   F
    2   A
    2   W
    2   A
    

    내 UDF는

    package com.something;
    
    import java.util.ArrayList;
    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.apache.hadoop.io.Text;
    
    public class RemoveSequentialDuplicates extends UDF {
        public ArrayList<Text> evaluate(ArrayList<Text> arr) {
            ArrayList<Text> newList = new ArrayList<Text>();
            newList.add(arr.get(0));
            for (int i=1; i<arr.size(); i++) {
    
                String front = arr.get(i).toString();
                String back = arr.get(i-1).toString();
    
                if (!back.equals(front)) {
                    newList.add(arr.get(i));
                }
            }
            return newList;
        }
    }
    

    그때 내 질문은

    add jar /path/to/jar/brickhouse-0.7.1.jar;
    add jar /path/to/other/jar/duplicates.jar;
    
    create temporary function remove_seq_dups as 'com.something.RemoveSequentialDuplicates';
    create temporary function collect as 'brickhouse.udf.collect.CollectUDAF';
    
    select id
      , remove_seq_dups(value_array) no_dups
    from (
      select id
        , collect(value) value_array
      from db.table
      group by id ) x
    

    산출

    1   ["A","B","A","C","D"]
    2   ["D","F","A","W","A"]
    

    별도로, 내장 된 collect_list는 그룹화 된 순서대로 목록 요소를 유지할 필요가 없습니다. 벽돌 집 수집 것입니다. 이것이 도움이되기를 바랍니다.

  2. ==============================

    2.너도 이런 식으로하면

    너도 이런 식으로하면

    index  value
    1       A
    2       A
    3       A
    4       B
    5       B
    6       A
    7       c
    8       c
    

    index는 인덱스와 같은 순위 값이나 날짜와 같은 순위 값입니다. 당신의 상황에 문제가 있다고 생각합니다.

    그런 다음 쿼리 :

    select collect_all(value)
    from
      (select index, value 
       from table) a
       left outer join
      (select index, 
         last_value(value) over (order by index row between current row and 1 following) as nextvalue 
       from table) b
      on a.index=b.index
      where value <> nextvalue
    ;
    

    여기서 문제는 다음 값이 없기 때문에 C의 마지막 값을 얻지 못한다는 것입니다. 따라서 add 또는 nextvalue가 null이고 결과가 있어야합니다.

    select collect_all(value)
    from
      (select index, value 
       from table) a
       left outer join
      (select index, 
         last_value(value) over (order by index row between current row and 1 following) as nextvalue 
       from table) b
      on a.index=b.index
      where (value <> nextvalue) or (nextvalue is null)
    ;
    

    이것은 [ "A", "B", "A", "C"]를 산출합니다.

  3. from https://stackoverflow.com/questions/31324764/hadoop-hive-collect-list-without-repeating-items by cc-by-sa and MIT license