복붙노트

[HADOOP] Hive 0.13에서 테이블을 업데이트하는 방법은 무엇입니까?

HADOOP

Hive 0.13에서 테이블을 업데이트하는 방법은 무엇입니까?

내 하이브 버전은 0.13입니다. 두 테이블, table_1 및 table_2 있습니다.

table_1에 포함 된 항목 :

customer_id | items | price | updated_date
------------+-------+-------+-------------
10          | watch | 1000  | 20170626
11          | bat   | 400   | 20170625

table_2에 포함 된 항목 :

customer_id | items    | price | updated_date
------------+----------+-------+-------------
10          | computer | 20000 | 20170624

customer_id가 이미 존재한다면 table_2의 레코드를 업데이트하고, 그렇지 않으면 table_2에 추가해야합니다.

Hive 0.13은 업데이트를 지원하지 않기 때문에 조인을 시도했지만 실패합니다.

해결법

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

    1.row_number 또는 전체 조인을 사용할 수 있습니다. 다음은 row_number를 사용하는 예제입니다.

    row_number 또는 전체 조인을 사용할 수 있습니다. 다음은 row_number를 사용하는 예제입니다.

    insert overwrite table_1 
    select customer_id, items, price, updated_date
    from
    (
    select customer_id, items, price, updated_date,
           row_number() over(partition by customer_id order by new_flag desc) rn
    from 
        (
         select customer_id, items, price, updated_date, 0 as new_flag
           from table_1
         union all
         select customer_id, items, price, updated_date, 1 as new_flag
           from table_2
        ) all_data
    )s where rn=1;
    

    FULL JOIN을 사용한 업데이트에 대한 답변도 참조하십시오 : https://stackoverflow.com/a/37744071/2700344

  2. from https://stackoverflow.com/questions/44753544/how-to-update-table-in-hive-0-13 by cc-by-sa and MIT license