How to load key value pairs into a hive table?

Below is my dataset:

Jun name="balaji" id=101

Mar name="kumar" id=102

Created table:

create table sample(month string,name string, id int)
row format delimited fields terminated by 'space' map keys terminated by '=';

Result:

select * from sample;      
JUN name="balaji" NULL
Mar name="kumar" NULL

Expected Result:

JUN balaji 101

Mar kumar 102

Please help me with this.

+4
source share
1 answer

Create tables like this:

create table sample(mnth string,names map<string,string>,ids map<string,int>)
row format delimited fields terminated by ' ' map keys terminated by '=';

Select a query:

  select mnth,names["name"],ids["id"]  from sample;

    result: 
      Jun   "balaji"    101
      Mar   "kumar" 102

If you call select *from a sample:

   Jun  {"name":"\"balaji\""}   {"id":101}
   Mar  {"name":"\"kumar\""}    {"id":102}

To access each value on the map you need to go through how names["name"].

+2
source

All Articles