进阶 |Hive 复杂数据类型
转载
分享嘉宾:bigdatazkx
编辑整理:仙子紫霞
正文
1. hive的数据类型
Hive的内置数据类型可以分为两大类:(1)、基础数据类型;(2)、复杂数据类型
2. hive基本数据类型
基础数据类型包括:
TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY,TIMESTAMP,DECIMAL,CHAR,VARCHAR,DATE。
3. hive集合类型
集合类型主要包括:array,map,struct等,hive的特性支持集合类型,这特性是关系型数据库所不支持的,利用好集合类型可以有效提升SQL的查询速率。
3.1 集合类型之array(1) 先创建一张表
create table t_array(id int,name string,hobby array<string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
stored as textfile;
load data inpath '/tmp/array.txt' into table t_array;
(2) 准备数据文件 t_array.txt
1,zhangsan,唱歌-跳舞-游泳
2,lisi,打游戏-篮球
(3) 查询数据
select id ,name,hobby[0],hobby[1] from t_array;
+-----+-----------+------+------+
| id | name | _c2 | _c3 |
+-----+-----------+------+------+
| 1 | zhangsan | 唱歌 | 跳舞 |
| 2 | lisi | 打游戏 | 篮球 |
+-----+-----------+------+------+
select id ,name,hobby[0],hobby[1],hobby[2] from t_array;
+-----+-----------+------+------+-------+
| id | name | _c2 | _c3 | _c4 |
+-----+-----------+------+------+-------+
| 1 | zhangsan | 唱歌 | 跳舞 | 游泳 |
| 2 | lisi | 打游戏 | 篮球 | NULL |
+-----+-----------+------+------+-------+
select * from t_array;
+-------------+---------------+-------------------+
| t_array.id | t_array.name | t_array.hobby |
+-------------+---------------+-------------------+
| 1 | zhangsan | ["唱歌","跳舞","游泳"] |
| 2 | lisi | ["打游戏","篮球"] |
+-------------+---------------+-------------------+
2 rows selected (0.114 seconds)
注意:array的访问元素和java中是一样的,这里通过索引来访问。
3.2 集合类型之map
(1) 先创建一张表
create table t_map(id int,name string,hobby map<string,string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
stored as textfile;
#加载数据
load data inpath '/tmp/t_map.txt' into table t_map;
(2) 准备数据文件 t_map.txt
1,zhangsan,唱歌:非常喜欢-跳舞:喜欢-游泳:一般般
2,lisi,打游戏:非常喜欢-篮球:不喜欢
(3) 查询数据
select * from _map;
+-----------+-------------+-------------------------------------+
| t_map.id | t_map.name | t_map.hobby |
+-----------+-------------+-------------------------------------+
| 1 | zhangsan | {"唱歌":"非常喜欢","跳舞":"喜欢","游泳":"一般般"} |
| 2 | lisi | {"打游戏":"非常喜欢","篮球":"不喜欢"} |
+-----------+-------------+-------------------------------------+
2 rows selected (0.103 seconds)
select id,name,hobby['唱歌'] from t_map;
+-----+-----------+-------+
| id | name | _c2 |
+-----+-----------+-------+
| 1 | zhangsan | 非常喜欢 |
| 2 | lisi | NULL |
+-----+-----------+-------+
2 rows selected (0.115 seconds)
select id,name,hobby['唱歌'],hobby['跳舞'] from t_map;
+-----+-----------+-------+-------+
| id | name | _c2 | _c3 |
+-----+-----------+-------+-------+
| 1 | zhangsan | 非常喜欢 | 喜欢 |
| 2 | lisi | NULL | NULL |
+-----+-----------+-------+-------+
注意:map的访问元素中的value和java中是一样的,这里通过key来访问。
3.3 集合类型之struct
(1) 先创建一张表
create table t_struct(id int,name string,address struct<country:string,city:string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
stored as textfile;
#加载数据
load data inpath '/tmp/t_struct.txt' into table t_struct;
(2) 准备数据文件 struct.txt
1,zhangsan,china-beijing
2,lisi,USA-newyork
(3) 查询数据
select * from t_struct;
+--------------+----------------+---------------------------------------+
| t_struct.id | t_struct.name | t_struct.address |
+--------------+----------------+---------------------------------------+
| 1 | zhangsan | {"country":"china","city":"beijing"} |
| 2 | lisi | {"country":"USA","city":"newyork"} |
+--------------+----------------+---------------------------------------+
2 rows selected (0.112 seconds)
select id,name,address.country,address.city from t_struct;
+-----+-----------+----------+----------+
| id | name | country | city |
+-----+-----------+----------+----------+
| 1 | zhangsan | china | beijing |
| 2 | lisi | USA | newyork |
+-----+-----------+----------+----------+
总结:struct访问元素的方式是通过.符号
作者:bigdatazkx