1.Array
1.创建表
create table 表名(id int,other Array<string>) row format delimited fileds terminated by '\t' collection items terminated by ',' ;
2.加载数据
load data local 'filepath' into table 表名;
3.查看表
select * from 表名;
4.查看表结构
desc 表名;
5.查看数组【下标由0开始】
select 字段 数组字段[0] from 表名; #仅仅查看数据的第一个元素
如:
select id other[0] from hive1;
6.查看数组元素数量
select 字段 size(数组字段)from 表名; #使用size() 函数查看数组元素数量
2.Map
key value
1.创建表
create table 表名(字段 字段类型,Map字段 map<key类型,value类型>) row format delimited fields terminated by '\t' collection items terminated by '#';
数据类型:
1 ZH,A:a#B:b#C:c
2 HE,a:A#b:B#c:C
2.加载数据
load data local inpath 'filepath into table 表名;
3.查看表
select * from 表名;
1 ZH {"A":"a","B":"b","C":"c"}
2 HE {"a":"A","b":"B","c":"C"}
4.获取Map中的key
select 字段,map_keys(Map字段) from 表名; #使用map_keys()函数
结果: 1 {"A","B","C"}
5.查看Map中的value
select 字段 Map字段['key1'],Map字段['key2'] from 表名;
如:
select id,member['A'],member['B'] from hive1;
3.Struct
1.创建表
create table 表名(字段 字段类型,struct字段 struct<字段1:字段类型,字段1:字段类型,...>) row format delimited fields terminated by '#' collection items terminated by ':';
数据类型:
192.168.1.1#2H:50
192.167.1.2#5R:20
2.加载数据
load data local inpath 'filepath' into table 表名;
3.查看表
select * from 表名;
192.168.1.1 {"字段1":"2H","字段2":"50"}
192.167.1.2 {"字段1":"5R","字段2":"20"}
4.获取struct
select 字段,struct.字段1,struct.字段2 from 表名;
如:
select ip member.userinfo, userinfo.age from hive1;
4.json
1.创建表
create table 表名(json 数据类型);
{“movie”:“2189”,“rate”:“4”,“time”:“0730”}
{“movie”:“2185”,“rate”:“3”,“time”:“0720”}
2.加载数据
3.拆分json
select json_tuple(json ‘movie’,‘rate’,‘time’) from 表名;
结果:2189 4 0730
2185 3 0720