(7-7)hive复合数据类型


hive基本数据类型:

(7-7)hive复合数据类型_第1张图片



(7-7)hive复合数据类型_第2张图片



hive复合数据类型:


--------------------------------------------------------------- struct 类型---------------------------------------------------------------------

//创建表
hive> create table student_test(id int,info struct<name:string,age:int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY ':';
OK
Time taken: 1.738 seconds
hive> DESC student_test;
OK
id                      int
info                    struct<name:string,age:int>
Time taken: 0.398 seconds, Fetched: 2 row(s)
hive>

//准备上传的文本数据
[root@i-love-you ~]# more a
12345,baozi:23
54321,awm:22

//加载进去
hive> load data local inpath 'a' into table student_test;
Loading data to table default.student_test
Table default.student_test stats: [numFiles=1, totalSize=28]
OK
Time taken: 6.329 seconds
hive> select * from student_test;
OK
12345   {"name":"baozi","age":23}
54321   {"name":"awm","age":22}
Time taken: 0.846 seconds, Fetched: 2 row(s)
hive>

//按字段查询
hive> select id  from student_test;
OK
12345
54321
Time taken: 0.192 seconds, Fetched: 2 row(s)
hive> select info.name,info.age from student_test;
OK
baozi   23
awm     22
Time taken: 0.116 seconds, Fetched: 2 row(s)
hive>



----------------------------------------------------------------- array 类型 ---------------------------------------------------------------------




//准备上传的文本文件:
[root@i-love-you ~]# more class
class1,1001:1002
class2,1003:1004


//创建表
hive> create table class_test(name string,student_id_list array<int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY ':';
OK
Time taken: 0.191 seconds
hive> load data local inpath "class" into table class_test;
Loading data to table default.class_test
Table default.class_test stats: [numFiles=1, totalSize=34]
OK
Time taken: 0.617 seconds
hive> 



//查询
hive> select * from class_test;
OK
class1  [1001,1002]
class2  [1003,1004]
Time taken: 0.125 seconds, Fetched: 2 row(s)
hive> select name from class_test;
OK
class1
class2
Time taken: 0.154 seconds, Fetched: 2 row(s)
hive> select student_id_list[0] from class_test;
OK
1001
1003
Time taken: 0.151 seconds, Fetched: 2 row(s)
hive> select student_id_list[1] from class_test;
OK
1002
1004
Time taken: 0.104 seconds, Fetched: 2 row(s)
hive> select student_id_list[2] from class_test;
OK
NULL
NULL
Time taken: 0.091 seconds, Fetched: 2 row(s)
hive>




还有map类型、UNION类型





你可能感兴趣的:(7-7hive复合数据类型)