Hive 基本语法操练(四):Hive 复合类型

 hive语法中主要提供了以下复合数据类型:

 1)Structs: structs内部的数据可以通过DOT(.)来存取。例如,表中一列c的类型为STRUCT{a INT; b INT},我们可以通过c.a来访问域a。

 2)Map(K-V对):访问指定域可以通过["指定域名称"]进行。例如,一个Map中M包含了一个group->gid的K-V对,gid的值可以通过M['group']来获取。

 3)Array:array中的数据为相同类型。例如,假如array A中元素['a','b','c'],则A[1]的值为'b'

1、Struct使用

 1) 建表

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: 0.386 seconds

 hive> show tables;

 OK

 student_test

 Time taken: 0.046 seconds, Fetched: 1 row(s)

 'FIELDS TERMINATED BY' :字段与字段之间的分隔符。'COLLECTION ITEMS TERMINATED BY' :一个字段各个item的分隔符。

 2) 导入数据

[hadoop@master ~]$ cd /home/hadoop/test

[hadoop@master test]$ sudo vim test1.txt

1,zhou:30

2,yan:30

3,chen:20

4,li:80

[hadoop@master test]$ ll

total 12

-rw-r--r-- 1 root root  43 May 24 03:40 test1.txt

-rw-r--r-- 1 root root 106 May 18 04:41 test.txt

-rw-r--r-- 1 root root 421 May 18 04:03 user.txt

hive> load data local inpath '/home/hadoop/test/test1.txt' into table student_test;                                                                         

Loading data to table hive.student_test

Table hive.student_test stats: [numFiles=1, totalSize=37]

OK

Time taken: 0.363 seconds

 3) 查询数据

hive> select info.age from student_test;                                                                                                                    
OK
30
30
20
80
Time taken: 0.078 seconds, Fetched: 4 row(s)

2、Array使用

 1) 建表

hive> create table class_test(name string, stu_id_list array<int>) row format delimited fields terminated by ',' collection items terminated by ':';
OK
Time taken: 0.079 seconds

 2) 导入数据

[hadoop@master test]$ sudo vim test2.txt
034,1:2:3:4
035,5:6  
036,7:8:9:10
hive> load data local inpath '/home/hadoop/test/test2.txt' into table class_test;
Loading data to table hive.class_test
Table hive.class_test stats: [numFiles=1, totalSize=33]
OK
Time taken: 0.299 seconds

 3) 查询

hive> select stu_id_list[3] from class_test;
OK
4
NULL
10
Time taken: 0.048 seconds, Fetched: 3 row(s)

3、Map使用

 1) 建表

hive> create table employee(id string, perf map<string, int>) row format delimited fields terminated by '	' collection items terminated by ',' map keys terminated by ':';
OK
Time taken: 0.082 seconds

 ‘MAP KEYS TERMINATED BY’ :key value分隔符

 2) 导入数据

[hadoop@master test]$ sudo vim test3.txt
1       job:80,team:60,person:70
2       job:60,team:80
3       job:90,team:70,person:100
hive> load data local inpath '/home/hadoop/test/test3.txt' into table employee;
Loading data to table default.employee
Table default.employee stats: [numFiles=1, totalSize=72]
OK
Time taken: 0.643 seconds

 3) 查询

hive> select perf['person'] from employee;
OK
70
NULL
100
Time taken: 0.473 seconds, Fetched: 3 row(s)

以上就是博主为大家介绍的这一板块的主要内容,这都是博主自己的学习过程,希望能给大家带来一定的指导作用,有用的还望大家点个支持,如果对你没用也望包涵,有错误烦请指出。如有期待可关注博主以第一时间获取更新哦,谢谢! 

 版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/zimo-jing/p/9079161.html