Skip to content

Instantly share code, notes, and snippets.

@rajkrrsingh
Last active May 13, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajkrrsingh/7d3460210a7148263b9d87531947304d to your computer and use it in GitHub Desktop.
Save rajkrrsingh/7d3460210a7148263b9d87531947304d to your computer and use it in GitHub Desktop.
quick start guide to test Hive Merge features

Create Non-ACID table

create external table nonacid_n1 (key int, a1 string, value string) stored as orc;
insert into nonacid_n1 values(1, 'a11', 'val1');
insert into nonacid_n1 values(2, 'a12', 'val2');

+-----------------+----------------+-------------------+
| nonacid_n1.key  | nonacid_n1.a1  | nonacid_n1.value  |
+-----------------+----------------+-------------------+
| 1               | a11            | val1              |
| 2               | a12            | val2              |
+-----------------+----------------+-------------------+

Create ACID table

create table acidTable(key int NOT NULL enable, a1 string DEFAULT 'a1', value string) stored as orc tblproperties ("transactional"="true");
insert into acidTable values(1, 'a10','val100');

+----------------+---------------+------------------+
| acidtable.key  | acidtable.a1  | acidtable.value  |
+----------------+---------------+------------------+
| 1              | a10           | val100           |
+----------------+---------------+------------------+

Run Merge Statement

MERGE INTO acidTable as t using nonacid_n1 as s ON t.key = s.key
WHEN MATCHED AND s.key < 3 THEN DELETE
WHEN MATCHED AND s.key > 3 THEN UPDATE set a1 = "DEFAULT"
WHEN NOT MATCHED THEN INSERT VALUES (s.key, s.a1, "DEFAULT");

+----------------+---------------+------------------+
| acidtable.key  | acidtable.a1  | acidtable.value  |
+----------------+---------------+------------------+
| 2              | a12           | DEFAULT          |
+----------------+---------------+------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment