数字类型
tinyint------微整数(-128至127)
smallint-------2字节相当于java中的short
int/integer---4字节
bigint-----8字节(相当于java中的long)
float----4字节(浮点型)小数
double--8字节(浮点型精度更高)小数
日期时间类型
timestamp-------时间戳
date-----日期
字符串类型
string
varchar(10)----可变长度字符串
char-----单字符
混杂类型
boolean------布尔值
binary-----用的少
复合类型
array数组类型-----array<string>
排序函数:sort_array(字段/数组)
数据中有复杂数据类型建表
create table t_movie(movie_name string,actors array<string>,first_show date)
row format delimited fields terminated by ','
collection items terminated by ':';----------集合的元素按':'分割
查询:
select movie_name,actors[0],first_show from t_movie;---数组中第一个元素
select movie_name,actors,first_show
from t_movie where array_contains(actors(数组字段),'吴刚'(要查询的值));------数组中的内容
select movie_name
,size(actors) as actor_number ------------数组长度函数
,first_show
from t_movie;
map类型
-- 有如下数据:
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
-- 建表映射上述数据
create table t_family(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 ':';-----------------kv间隔
-- 导入数据
load data local inpath '/root/hivetest/fm.dat' into table t_family;
-- 查出每个人的亲人数量
select id,name,size(family_members) as relations,age
from t_family;
-- 查出所有拥有兄弟的人及他的兄弟是谁
-- 方案1:一句话写完
select id,name,age,family_members['brother']-----family_members字段中key值为brother的
from t_family where array_contains(map_keys(family_members)-------family_members中所有key值,返回数组。map_values所有value的值
,'brother');
-- 方案2:子查询
select id,name,age,family_members['brother']
from
(select id,name,age,map_keys(family_members) as relations,family_members
from t_family) tmp
where array_contains(relations,'brother');
struct类型
假如有以下数据:
1,zhangsan,18:male:深圳
2,lisi,28:female:北京
3,wangwu,38:male:广州
4,赵六,26:female:上海
5,钱琪,35:male:杭州
6,李刚,48:female:南京
*/
-- 建表映射上述数据
drop table if exists t_user;
create table t_user(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 '/root/hivetest/user.dat' into table t_user;
-- 查询每个人的id name和地址
select id,name,info.addr
from t_user;
union类型
多个类型中选其中之一。0.7版本后使用