复杂数据类型
array数组类型
在创建表的时候声明array类型
create table tbl_name (col_name array<string>) row format delimited fields terminated by '\t' collection items terminated by ',';
最后collection items terminated by ','
表示array中的元素通过’,'进行分隔
可以通过select
查询对应的值,下标从0开始
例
select locations[0] from tbl;
tbl表中的locations
列为array类型,表示查询第一个元素
统计数组元素个数,使用size()
select size(locations) from tbl;
查询array中是否包含某个元素,使用array_contains(col_name,查询元素)
函数
select * from tbl where array_contains(locations,"tianjing")
map映射类型
key_value键值对的数据格式
- 字段与字段的分隔符","
- map字段之间的分割符"#"
- map内部的k-v的分割符":"
建表的语句
create table tbl (col_name map<type_key, type_value>)
row format delimited fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';
后面的
row format delimited fields terminated by ',' 字段之间的分隔符
collection items terminated by '#' map之间的分割符
map keys terminated by ':'; 键值对之间的分割符
表示的就是字段之间的分割符
示例
id, name , members, age
1,zhangsan,father:xiaoming#mother:wangwu,28
查询所有记录指定key的value
select id, name ,members['key'] from tbl
取出map中的全部key
select map_key(members) from tbl;
返回的数据类型是一个array
取出map中的全部value
select map_value(members) from tbl;
同样返回一个array
查看map中包含的键值对数量
select size(members) from tbl;
判断一个map是否包含指定的key或者value,用之前array中的array_contains
方法和上面的map_value| map_key
嵌套
select * from tbl where array_contains(map_key(members),"sister");
struct类型
允许在一个列中存放多个子列,每个子类允许设置单独的类型和名称
创建表语句
create table tbl (col_name struct<col_name_1: col_1_type, col_name_2: col_2_type>)
row format delimited fields terminated by '#' 字段之间通过#进行分隔
collection items terminated by ':';
例如
id, info(name,age)
1#张三:11
直接查询info列的话,返回[Object]
,只能显示其为一个对象
需要查询struct内部的对象的话,用info.name
select info.age, info.name from tbl;