hive 使用 map 类型字段

2021-07-13 14:36:56 浏览数 (1)

创建一个map类型字段

代码语言:javascript复制
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方式插入

代码语言:javascript复制
insert into test3(field2)values(str_to_map("name:zhangsan,age:25")),(str_to_map("name:lisi,age:23"));

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

代码语言:javascript复制
load data local inpath  '/Users/zhangsheng/hive/note/hive/test.txt' into table test3;

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

代码语言:javascript复制
select * from test3;
select field2["name"] as name,field2["age"] as age from test3;
select * from test3 where field2["age"] > 25;

用 map() 函数

代码语言:javascript复制
select map("pay_order_cnt", 10, "pay_order_amt", 1000)

0 人点赞