tinyint对应java数据类型_进阶 Hive 复杂数据类型

47c6bb482132b2d4799a820366ec14b0.gif

6f4ec2f253dfb81836f24a480a1c78ca.png

分享嘉宾:bigdatazkx

编辑整理:仙子紫霞

出品平台:数据仓库与Python大数据

正文

1. hive的数据类型

Hive的内置数据类型可以分为两大类:(1)、基础数据类型;(2)、复杂数据类型

2. hive基本数据类型

基础数据类型包括:

TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY,TIMESTAMP,DECIMAL,CHAR,VARCHAR,DATE。

f9e8bcc34de0ef25d0339ccfcde80861.png

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

著作权归作者所有,热情欢迎广大技术人员投稿,加v:iom111128,备注:投稿


【巨变】云原生数仓,破茧而出

2020-09-21

008ee4e67f4b9854e9329cdffcc2e757.png

面试 | Kafka常见面试问题总结

2020-09-21

e4d267378082051b718718d7291f48bf.png

基于Spark 推荐系统特征工程

2020-09-17

67d3aa6b0b09ac3064b95d3b468e238c.png

Flink系列 - 实时数仓之热门商品统计-TopN(三)

2020-09-16

14128ac323754b2b0031a0bf8640ad84.png

欢迎关注置顶公众号

入群请联系小助手:iom1128『紫霞仙子』

4bce3e7a3c60c90ce874213e0d816b01.png  

关注不迷路~ 各种福利、资源定期分享

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值