Merge option in Hive with Example

Posted on by Sumit Kumar

Wow! Merge in Hive ? Yes , after the successful release of hive 2.2.X merge is also possible in hive now. Today I will walk you through one simple example that will clear merge concept in hive.

Merge option in Hive with Example

What is Merge option in hive:-

With Merge option we can perform record level insert,update and delete in hive table.

For example suppose we have one table(final table) and incremental data is keep coming from source table using sqoop . Now our requirement is , if we get new record, we have to insert into final table or if we get record that is already available in final table , we have to update the final table record with updated value that is coming from source table.

Above example is called upserts .It is combination of update and insert.

To support Merge option in hive we have to create bucketed table in ORC format and need to enable ACID property as below:–

SET hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
SET hive.enforce.bucketing=true;
SET hive.exec.dynamic.partition.mode=nonstrict;

create table customer_name
(id int, name string, state string)
clustered by (id) into 2 buckets stored as orc

Lets insert some records in customer_name table:–

INSERT INTO customer_name VALUES (1, 'sumit', 'bihar'),(2, 'amit', 'bihar');

Create incremental table(all_updates) and insert one records.

INSERT INTO customer_name VALUES (1, 'prakash', 'panjab');

Below Merge query will update customer_name record with ID 1 with the value of table all_updates record.

Option 1:-

merge into customer_name
using ( select * from all_updates) sub
on =
when matched then update set name =, state = sub.state
when not matched then insert values (,, sub.state);

Option 2:-

merge into customer_name
using all_updates on =
when matched then update set,
when not matched then insert

After executing merge query , final table (customer_name) will have updated value.

Hope this simple example will help you to understand Merge in hive.

About ATI Base Author: Sumit Kumar

Author of Hadoop tutorial View all posts by Sumit Kumar →

Got Something To Say:

Your email address will not be published. Required fields are marked *



Planet Polaris

Website Support
website support at Planet Polaris