Hive既有大多数关系数据库中的基本类型,又有集合这种复杂类型。
基本类型
数据类型 | 大小 | 范围 | 示例 |
---|---|---|---|
TINYINT | 1byte | -128 ~ 127 | 100Y |
SMALLINT | 2byte | -32,768 ~ 32,767 | 100S |
INT/INTEGER | 4byte | -2,147,483,648 ~ 2,147,483,647 | 100 |
BIGINT | 8byte | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | 100L |
FLOAT | 4byte | 单精度浮点数 | 3.1415926 |
DOUBLE | 8byte | 双精度浮点数 | 3.1415926 |
DECIMAL | – | 高精度浮点数 | DECIMAL(9,8) |
BOOLEAN | – | 布尔型,TRUE/FALSE | true |
BINARY | – | 二进制类型 | – |
数字类型
整数类型
-2,147,483,648 ~ 2,147,483,647之间的整数类型默认是INT型,除非指定了格式100Y、100S、100L会自动转换为TINYINT、SMALLINT、BIGINT
浮点数类型
浮点数默认会当作DOUBLE型;
Hive中的DECIMAL基于Java中的BigDecimal,BigDecimal用于表示任意精度的不可修改的十进制数字;
DECIMAL不指定精度时默认为DECIMAL(10,0);
字符串类型
String
string类型可以用单引号(’)或双引号(”)定义,Hive在string中使用C-style。
Varchar
varchar类型由长度定义,范围为1-65355,如果存入的字符串长度超过了定义的长度,超出部分会被截断。尾部的空格也会作为字符串的一部分,影响字符串的比较。
Char
char是固定长度的,最大长度255,而且尾部的空格不影响字符串的比较。
三种类型对尾部空格的区别,参考如下例子,每个字段都插入同样的字符并且在尾部有不同的空格。
create table char_a (c1 char(4),c2 char(5),str1 string,str2 string,var1 varchar(4),var2 varchar(6));
insert into char_a values('ccc ','ccc ','ccc ','ccc ','ccc ','ccc ');
select c1=c2,str1=str2,var1=var2 from char_a;
OK
true false false
Time taken: 1.101 seconds, Fetched: 1 row(s)
日期与时间戳
Timestamps
timestamp表示UTC时间,可以是以秒为单位的整数;带精度的浮点数,最大精确到小数点后9位,纳秒级;java.sql.Timestamp格式的字符串 YYYY-MM-DD hh:mm:ss.fffffffff
Date
Hive中的Date只支持YYYY-MM-DD格式的日期,其余写法都是错误的,如需带上时分秒,请使用timestamp
复杂类型
STRUCT
类似于C、C#语言,Hive中定义的struct类型也可以使用点来访问。从文件加载数据时,文件里的数据分隔符要和建表指定的一致。
CREATE TABLE IF NOT EXISTS person_1 (id int,info struct<name:string,country:string>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY ':'
STORED AS TEXTFILE;
创建一个文本文件test_struct.txt
1,'dd':'jp'
2,'ee':'cn'
3,'gg':'jp'
4,'ff':'cn'
5,'tt':'jp'
导入数据
LOAD DATA LOCAL INPATH '/data/test_struct.txt' OVERWRITE INTO TABLE person_1;
查询数据
hive> select * from person_1;
OK
1 {"name":"'dd'","country":"'jp'"}
2 {"name":"'ee'","country":"'cn'"}
3 {"name":"'gg'","country":"'jp'"}
4 {"name":"'ff'","country":"'cn'"}
5 {"name":"'tt'","country":"'jp'"}
Time taken: 0.046 seconds, Fetched: 5 row(s)
hive> select id,info.name,info.country from person_1 where info.name='dd';
OK
1 dd jp
Time taken: 1.166 seconds, Fetched: 1 row(s)
ARRAY
ARRAY表示一组相同数据类型的集合,下标从零开始,可以用下标访问
CREATE TABLE IF NOT EXISTS array_1 (id int,name array<STRING>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY ':'
STORED AS TEXTFILE;
导入数据
LOAD DATA LOCAL INPATH '/data/test_array.txt' OVERWRITE INTO TABLE array_1;
查询数据
hive> select * from array_1;
OK
1 ["dd","jp"]
2 ["ee","cn"]
3 ["gg","jp"]
4 ["ff","cn"]
5 ["tt","jp"]
Time taken: 0.041 seconds, Fetched: 5 row(s)
hive> select id,name[0],name[1] from array_1 where name[1]='cn';
OK
2 ee cn
4 ff cn
Time taken: 1.124 seconds, Fetched: 2 row(s)
MAP
MAP是一组键值对的组合,可以通过KEY访问VALUE,键值之间同样要在创建表时指定分隔符。
CREATE TABLE IF NOT EXISTS map_1 (id int,name map<STRING,STRING>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY ':'
MAP KEYS TERMINATED BY ':'
STORED AS TEXTFILE;
加载数据
LOAD DATA LOCAL INPATH '/data/test_map.txt' OVERWRITE INTO TABLE map_1;
查询数据
hive> select * from map_1;
OK
1 {"name":"dd","country":"jp"}
2 {"name":"ee","country":"cn"}
3 {"name":"gg","country":"jp"}
4 {"name":"ff","country":"cn"}
5 {"name":"tt","country":"jp"}
Time taken: 0.038 seconds, Fetched: 5 row(s)
select id,info['name'],info['country'] from map_1 where info['country']='cn';
OK
2 ee cn
4 ff cn
Time taken: 1.088 seconds, Fetched: 2 row(s)
UINON TYPES
Hive除了支持STRUCT、ARRAY、MAP这些原生集合类型,还支持集合的组合,不支持集合里再组合多个集合。
简单示例MAP嵌套ARRAY,手动设置集合格式的数据非常麻烦,建议采用INSERT INTO SELECT 形式构造数据再插入UNION表。
创建DUAL表,插入一条记录,用于生成数据
create table dual(d string);
insert into dual values('X');
创建UNION表
CREATE TABLE IF NOT EXISTS uniontype_1
(
id int,
info map<STRING,array<STRING>>
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '-'
MAP KEYS TERMINATED BY ':'
STORED AS TEXTFILE;
插入数据
insert overwrite table uniontype_1
select 1 as id,map('english',array(99,21,33)) as info from dual
union all
select 2 as id,map('english',array(44,33,76)) as info from dual
union all
select 3 as id,map('english',array(76,88,66)) as info from dual;
查询数据
hive> select * from uniontype_1;
OK
3 {"german":[76,88,66]}
2 {"chinese":[44,33,76]}
1 {"english":[99,21,33]}
Time taken: 0.033 seconds, Fetched: 3 row(s)
hive> select * from uniontype_1 where info['english'][2]>30;
OK
1 {"english":[99,21,33]}
Time taken: 1.08 seconds, Fetched: 1 row(s)