如何更新 Hive table 行

How to update Hive table rows

我有一个 table 看起来像这样:

 id  | Col2 | Col3 | Text
--------------------------
  1  | ...  | ...  | "abc"   
  2  | ...  | ...  | "def 
  3  | ...  | ...  | "ghi"  
  4  | ...  | ...  | "jkl" 

另一个 table 看起来像这样:

 id  | Text
-------------
  1  | "qwe"
  2  | "rty"

我想以这样的 table 结束:

 id  | Col2 | Col3 | Text
--------------------------
  1  | ...  | ...  | "qwe"   
  2  | ...  | ...  | "rty" 
  3  | ...  | ...  | "ghi"  
  4  | ...  | ...  | "jkl" 

其中保留了 col2 和 col3 的原始值。本质上,我想使用 table 2 中的值来更新 table 1 的值,其中 ids 相同。

我试过了:

SELECT
    A.id,
    col1, 
    col2, 
    A.text
FROM table1 AS A
LEFT JOIN (
    SELECT
        id,
        text
    FROM table2
) AS B
    ON A.product_id = B.product_id

但这只是返回原来的table。有没有办法在 Presto/Hive 中实现我想要的?

您正在加载来自 table A 的文本,如果您想要更新值,它应该来自 table BNVL(B.text, A.Text) table B 如果不存在则保持原样(参见代码中的注释)

INSERT OVERWRITE table1 
SELECT
    A.id,
    col1, 
    col2, 
    NVL(B.text, A.Text) as Text  -- Take Text from table B, if not exists, leave as is (A.Text)
FROM table1 AS A
LEFT JOIN B ON A.product_id = B.product_id

您可以使用 coalesce(B.text, A.Text) 而不是 NVL,正如@PiotrFindeisen 提到的,它在 Presto 和 Hive 上也能正常工作。