大数据技术之Hive(Hive数据类型)


大数据技术之Hive(Hive数据类型)



Hive数据类型


1.1 基本数据类型

基本数据类型对于Hive的String类型相当于数据库的varchar类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储2GB的字符数。

1.2 集合数据类型
集合数据类型Hive有三种复杂数据类型ARRAY、MAP 和 STRUCT。ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。
复合类型
1.2.1array数组类型
示例:array类型的应用
假如有如下数据需要用hive的表去映射:

战狼2,吴京:吴刚:龙母,2017-08-16

三生三世十里桃花,刘亦菲:痒痒,2017-08-20

设想:如果主演信息用一个数组来映射比较方便

建表:

create table t_movie(moive_name string,actors array<string>,first_show date)
row format delimited fields terminated by ','
collection items terminated by ':';

导入数据:

load data local inpath '/opt/movie.dat' into table t_movie;

查询:

select * from t_movie;
select moive_name,actors[0] from t_movie;
select movie_name,actors from t_movie where array_contains(actors,'吴刚');
select movie_name,size(actors) from t_movie;

1.2.2 Map类型
maps:MAP<primitive_type, data_type>
假如有以下数据:
1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28
2,lisi,father:mayun#mother:huangyi#brother:guanyu,22
3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29
4,mayun,father:mayongzhen#mother:angelababy,26

可以用一个map类型来对上述数据中的家庭成员进行描述

建表语句:

create table t_person(id int,name string,family_members map<string,string>,age int)
row format delimited fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';

导入数据:

load data local inpath '/opt/person.dat' into table t_person;

查询

select * from t_person;

取map字段的指定key的值

select id,name,family_members['father'] as father from t_person;

注 : as father 为设置一个别名

取map字段的所有key

select id,name,map_keys(family_members) as relation from t_person;

取map字段的所有value

select id,name,map_values(family_members) from t_person;
select id,name,map_values(family_members)[0] from t_person;

综合:查询有brother的用户信息

select id,name,brother
from
(select id,name,family_members['brother'] as brother from t_person) tmp
where brother is not null;

1.2.3 struct类型
structs:STRUCT<col_name : data_type, …>
假如有如下数据:
1,zhangsan,18:male:beijing
2,lisi,28:female:shanghai

其中的用户信息包含:年龄:整数,性别:字符串,地址:字符串
设想用一个字段来描述整个用户信息,可以采用struct

建表:

create table t_person_struct(id int,name string,info struct<age:int,sex:string,addr:string>)
row format delimited fields terminated by ','
collection items terminated by ':';

导入数据:

load data local inpath '/opt/person_struct.dat' into table t_person_struct;

查询

select * from t_person_struct;
select id,name,info.age from t_person_struct;
select id,name,info.sex from t_person_struct;

1.3 时间类型


TIMESTAMP(时间戳) (包含年月日时分秒的一种封装)
DATE(日期)(只包含年月日)

示例,假如有以下数据文件:
1,zhangsan,1985-06-30
2,lisi,1986-07-10
3,wangwu,1985-08-09

那么,就可以建一个表来对数据进行映射

create table t_customer(id int,name string,birthday date)
row format delimited fields terminated by ',';

然后导入数据

load data local inpath '/opt/customer.dat' into table t_customer;

然后,就可以正确查询

select * from t_customer;

时间类型***

2.类型转化


Hive的原子数据类型是可以进行隐式转换的,类似于Java的类型转换,例如某表达式使用INT类型,TINYINT会自动转换为INT类型,但是Hive不会进行反向转化,例如,某表达式使用TINYINT类型,INT不会自动转换为TINYINT类型,它会返回错误,除非使用CAST操作。
1.隐式类型转换规则如下
(1)任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换成INT,INT可以转换成BIGINT。
(2)所有整数类型、FLOAT和STRING类型都可以隐式地转换成DOUBLE。
(3)TINYINT、SMALLINT、INT都可以转换为FLOAT。
(4)BOOLEAN类型不可以转换为任何其它的类型。
2.可以使用CAST操作显示进行数据类型转换
例如CAST(‘1’ AS INT)将把字符串’1’ 转换成整数1;如果强制类型转换失败,如执行CAST(‘X’ AS INT),表达式返回空值 NULL。

参考网址:
https://www.cppentry.com/bencandy.php?fid=117&id=184683

发布了11 篇原创文章 · 获赞 2 · 访问量 1806

猜你喜欢

转载自blog.csdn.net/weixin_45553177/article/details/104276338
今日推荐