如何在hive0.13中更新表?

9wbgstp7  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(407)

我的Hive版本是0.13。我有两张table, table_1 以及
table_2 table_1 包含:

  1. customer_id | items | price | updated_date
  2. ------------+-------+-------+-------------
  3. 10 | watch | 1000 | 20170626
  4. 11 | bat | 400 | 20170625
  5. ``` `table_2` 包含:

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

  1. 我想更新 `table_2` 如果 `customer_id` 已经存在于其中,如果不存在,则应附加到 `table_2` .
  2. 由于hive0.13不支持更新,我尝试使用join,但失败了。
hjqgdpho

hjqgdpho1#

你可以用 row_number 或者 full join . 这是一个使用 row_number :

  1. insert overwrite table_1
  2. select customer_id, items, price, updated_date
  3. from
  4. (
  5. select customer_id, items, price, updated_date,
  6. row_number() over(partition by customer_id order by new_flag desc) rn
  7. from
  8. (
  9. select customer_id, items, price, updated_date, 0 as new_flag
  10. from table_1
  11. union all
  12. select customer_id, items, price, updated_date, 1 as new_flag
  13. from table_2
  14. ) all_data
  15. )s where rn=1;

另请参阅此答案,以便使用 FULL JOIN : https://stackoverflow.com/a/37744071/2700344

展开查看全部

相关问题