2

I have a hive table as

create table mySource(
    col_1   map<string, string>,
    col_2   map<string, string>
)

here is how a record might look like

col_1                col_2
{"a":1, "b":"2"}     {"c":3, "d":"4"}

my target table looks like this

create table myTarget(
        my_col   map<string, string>
    )

now i want to combine the two columns from mySource into a single map and feed it to my target table. Basically i want to write something like

insert into myTarget
    select
        some_method(col_1, col_2) as my_col
    from mySource;

is there a built in method in hive that can do this ? I tried a few things with collect_set but got lots of errors

leftjoin
  • 36,950
  • 8
  • 57
  • 116
AbtPst
  • 7,778
  • 17
  • 91
  • 172

1 Answers1

3

The solution using only built-in methods. Explode both maps, UNION ALL results, collect array of key:value, concatenate array with ',', convert string to map using str_to_map:

with mytable as (--Use your table instead of this
select 
map('a','1', 'b','2') as col_1, map('c','3', 'd','4') as col_2
)

select str_to_map(concat_ws(',',collect_set(concat(key,':',val)))) as mymap
from
(
select m1.key, m1.val 
  from mytable
       lateral view explode(col_1) m1 as key, val
union all
select m2.key, m2.val 
  from mytable
       lateral view explode(col_2) m2 as key, val
)s       
;

Result:

mymap

{"a":"1","b":"2","c":"3","d":"4"}  

With brickhouse library it would be much easier:

ADD JAR /path/to/jar/brickhouse-0.7.1.jar;
CREATE TEMPORARY FUNCTION COMBINE AS 'brickhouse.udf.collect.CombineUDF';

select combine(col_1, col_2) as mymap from mytable;
leftjoin
  • 36,950
  • 8
  • 57
  • 116