概述
ARRAY:ARRAY类型是由一系列相同数据类型的元素组成,这些元素可以通过下标来访问。比如有一个ARRAY类型的变量fruits,它是由['apple','orange','mango']组成,那么我们可以通过fruits[1]来访问元素orange,因为ARRAY类型的下标是从0开始的;
操作实例
1、创建表
hive> create table array_test(name string,list array<int>)
> row format delimited fields terminated by ','
> collection items terminated by ':';
OK
2、准备文件
[root@hello110 data]# vi array_test
zhangsan,1:2:3:4:5
lisi,6:7:8:9:10
xiaoming,11:12:13:14:15
xiaoxiao,21:22:23:24:25
dingding,33:34:35:36:37
3、文件导入表
hive> load data local inpath "/data/array_test" into table array_test;
Loading data to table default.array_test
4、查看表
hive> select * from array_test;
OK
zhangsan [1,2,3,4,5]
lisi [6,7,8,9,10]
xiaoming [11,12,13,14,15]
xiaoxiao [21,22,23,24,25]
dingding [33,34,35,36,37]
Time taken: 5.293 seconds, Fetched: 5 row(s)
-------------
hive> select name from array_test;
OK
zhangsan
lisi
xiaoming
xiaoxiao
dingding
Time taken: 4.278 seconds, Fetched: 5 row(s)
-----------------
hive> select name,list[1] from array_test;
OK
zhangsan 2
lisi 7
xiaoming 12
xiaoxiao 22
dingding 34
Time taken: 4.857 seconds, Fetched: 5 row(s)
-----------下标从0开始-------------
hive> select name,list[0] from array_test;
OK
zhangsan 1
lisi 6
xiaoming 11
xiaoxiao 21
dingding 33
Time taken: 4.175 seconds, Fetched: 5 row(s)
---------当序号不存在的时候返回null------
hive> select name,list[-1] from array_test;
OK
zhangsan NULL
lisi NULL
xiaoming NULL
xiaoxiao NULL
dingding NULL
Time taken: 4.195 seconds, Fetched: 5 row(s)
---------------------------------
hive> select name,list[100] from array_test;
OK
zhangsan NULL
lisi NULL
xiaoming NULL
xiaoxiao NULL
dingding NULL
Time taken: 4.256 seconds, Fetched: 5 row(s)
hive>
5、hadoop中文件内容