目录
array数组
array数组和java中数组差不多,形式为[元素1,元素2,元素3,...]
建表语句:
create table table_name(name string, location array<string>) row format delimited fields terminated by '\t' collection items terminated by ',';
collection items terminated by '分隔符' 是设置数组分隔符的语句 ,注意加载数据与sql语句中符号都在英文(或中文)状态下输入
常用函数:
size(数组), 统计数组元素个数
select size(array) from table_name;
array_contains(数组, 数据),查看指定元素在指定数组中是否存在
select * from table_name where array_contains(array, 元素);
Map映射
map与python中的字典类似,表现为{"k1":"v1","k2":"v2",....}
创建语句
create table test_map(name string ,member map<string, string>) row format delimited fields terminated by ',' collection items terminated by '#' map keys terminated by ':';
map keys terminated by ':' 是键值对内部k,v间的分隔符,collection items则和array相似,针对建表语句给出示例txt文件
zhangsan,father:zhangbaba#mother:zhangmama
Lily,father:Bob#mother:Lucy
常用函数:
直接使用member['mother']得到key为mother的value
size(member)获得键值对数量
map_keys(member)返回所有的key,以array形式展示
map_values(member)返回所有value,以array形式展示
struct结构
struct结构与c语言的结构体类似,表现为{name:v1 , age:v2}
创建语句
create table test_struct(id int, info struct<name:string, age:int>) row format delimited fields terminated by '#' collection items terminated by ':';
给出对应的txt数据
1#Lily:18
2#Bob:28
3#Aric:55
取数据使用info.name就可以得到需要的数据