Hive复合数据类型array,map,struct的使用_Life is for sharing的博客-CSDN博客
HIVE数据类型
毕竟HIVE穿着SQL的外壳,肯定支持诸如Mysql这种RDBMS的数据类型,如int
,varchar
,但是它还具有非常多自有的数据类型,包括复杂的数据类型(数组,Map等)也是支持的!
数字类型,日期类型,String类型,Boolean类型我们都是比较熟悉的,也比较简单,就不讲解了。演示一下复杂数据类型:
arrays
1 | hive (hive)> create table arraytest (id int,course array<string>) |
说明:
row format delimited fields terminated by','
是指定列与列之间的分隔符,此处为”,”collection items terminated by':'
是指定集合内元素之间的分隔符,此处为”:”
因此我们要导入到hive中的数据应该是形如:
1 | 1,math:chinese |
数据加载到数据库
1 | hive> load data local inpath '/Users/fengxuechao/WorkSpace/software/hive_data/arraytest.txt' into table arraytest; |
查询所有数据:
1 | hive> select * from arraytest; |
查询数组指定索引的所有数据:
1 | hive> select course[1] from arraytest; |
maps
创建表
1 | hive> create table maptest(name string,score map<string,float>) |
数据
1 | '小明',math:96|chinese:95 |
数据加载到数据库
1 | hive> load data local inpath '/Users/fengxuechao/WorkSpace/software/hive_data/maptest.txt' into table maptest; |
查询所有数据:
1 | hive> select * from maptest; |
查询数组指定key的所有数据:
1 | hive> select name,score['math'] from maptest; |
structs
创建表
1 | hive> create table struct_test(name string,course struct<course:string,score:int>) |
数据
1 | 小明,math:79 |
数据加载到数据库
1 | hive> load data local inpath '/Users/fengxuechao/WorkSpace/software/hive_data/struct_test.txt' into table struct_test; |
查询所有数据:
1 | hive> select * from struct_test; |