创建一个map类型字段

create table test3(field2 map<string,string>) 
row format delimited fields terminated by ','
collection items terminated by "|"
map keys terminated by ":";

map keys terminated by ":"表示键值对之间用":"来分割

str_to_map 方式

同样的,先使用sql方式插入

insert into test3(field2)values(str_to_map("name:zhangsan,age:25")),(str_to_map("name:lisi,age:23"));

再导入文件,文件内容如下:

load data local inpath  '/Users/zhangsheng/hive/note/hive/test.txt' into table test3;

map的访问通过map[key]的形式进行,测试一下

select * from test3;
select field2["name"] as name,field2["age"] as age from test3;
select * from test3 where field2["age"] > 25;

用 map() 函数

select map("pay_order_cnt", 10, "pay_order_amt", 1000)