Q:

Adding the values contained in two tables as maps in Hive

Lets say there are two tables
Table 1

XYZ | {a:1,b:2,c:3}

Table 2

XYZ | {a:2,b:6,c:1}

I want a hive query which can add the values contained in the maps and provide the resulting map as follows. 
Merged Table

XYZ | {a:3,b:8,c:4}

A:

Your problem can be solved using below method in hive:
Consider I have 2 hive tables as sample_map1 and sample_map2. 
select * from sample_map1;
OK
col1  col2
XYZ   {"a":10,"b":20,"c":30}

select * from sample_map2;
OK
col1  col2
XYZ   {"a":1,"b":2,"c":3}

Query:
select a.col1,str_to_map(concat_ws(',',collect_list(concat(a.exp_col2_k,':',a.sum_col2))))
from(
select m.col1,exp_col2_k as exp_col2_k,sum(exp_col2_v) as sum_col2
from
(select m1.* 
from sample_map1 m1 join sample_map2 m2
on(m1.col1=m2.col1)
union all
select m2.* 
from sample_map1 m1 join sample_map2 m2
on(m1.col1=m2.col1)
)m
lateral view explode(m.col2)ex as exp_col2_k,exp_col2_v
group by m.col1,exp_col2_k
)a
group by col1;

Explanation for the above query:
1.Here lateral view explode has been used to explode the map column,which explodes the map into separate rows as key-value pairs. 
2.Then sum function is used to find the sum of value column. 
3.Used concat function to form a key value pair (key:value). 
4.Next collect_list function is used to collect the key-value pairs as array. 
5.Then concat_ws function is used to convert the array into string with comma separator. 
6.At last str_to_map function is used to convert the string to map. 

Hope this solution helps you. 
Thanks

